【问题标题】:Pseudo-terminal will not be allocated because stdin is not a terminal ssh bash不会分配伪终端,因为 stdin 不是终端 ssh bash
【发布时间】:2013-10-30 18:09:53
【问题描述】:

当我从 server.txt 列表 ssh 到我的服务器时,这是我的部分代码。

while read server <&3; do   #read server names into the while loop    
serverName=$(uname -n)
 if [[ ! $server =~ [^[:space:]] ]] ; then  #empty line exception
    continue
 fi   
 echo server on list = "$server"
 echo server signed on = "$serverName"
 if [ $serverName == $server ] ; then #makes sure a server doesnt try to ssh to itself
    continue
 fi
    echo "Connecting to - $server"
    ssh "$server"  #SSH login
    echo Connected to "$serverName"
    exec < filelist.txt
    while read updatedfile oldfile; do
    #   echo updatedfile = $updatedfile #use for troubleshooting
    #   echo oldfile = $oldfile   #use for troubleshooting
               if [[ ! $updatedfile =~ [^[:space:]] ]] ; then  #empty line exception
                continue # empty line exception
               fi
               if [[ ! $oldfile =~ [^[:space:]] ]] ; then  #empty line exception
                continue # empty line exception
               fi 
            echo Comparing $updatedfile with $oldfile
            if diff "$updatedfile" "$oldfile" >/dev/null ; then
                echo The files compared are the same. No changes were made.
            else
                echo The files compared are different.
                cp -f -v $oldfile /infanass/dev/admin/backup/`uname -n`_${oldfile##*/}_$(date +%F-%T)
                cp -f -v $updatedfile $oldfile 
            fi          
    done
 done 3</infanass/dev/admin/servers.txt

我不断收到此错误,并且 ssh 并没有真正连接并在服务器上执行其假定为 ssh 的代码。

Pseudo-terminal will not be allocated because stdin is not a terminal

【问题讨论】:

    标签: bash terminal


    【解决方案1】:

    我觉得上面那家伙说的一切都是错的。

    期待吗?

    很简单:

    ssh -i ~/.ssh/bobskey bob@10.10.10.10 << EOF
    echo I am creating a file called Apples in the /tmp folder
    touch /tmp/apples
    exit
    EOF
    

    两个“EOF”之间的所有内容都将在远程服务器中运行。

    标签必须相同。如果您决定将“EOF”替换为“WayneGretzky”,则还必须更改第二个 EOF。

    【讨论】:

    • 您误会了...是的,这些命令在远程服务器上运行,但变量替换发生在客户端,然后命令发送到远程服务器上运行。
    • 明白了,我确实误会了你。但是你应该能够摆脱双引号......我不会依赖期望
    • 我倾向于同意 -i ~/.ssh/bobskey 技术是简化此程序的好方法。
    【解决方案2】:

    您似乎假设当您运行ssh 连接到服务器时,文件中的其余命令将传递给在ssh 中运行的远程shell。他们不是;相反,一旦 ssh 终止并返回控制权,它们将由本地 shell 处理。

    要通过ssh 运行远程命令,您可以执行以下操作:

    • 将要执行的命令写入文件。使用scp将文件复制到远程服务器,并使用ssh user@remote command执行
    • 学一点TCL,用expect
    • 在 heredoc 中编写命令,但要小心变量替换:替换发生在客户端,而不是服务器上。例如,这将输出您的本地主目录,而不是远程:

      ssh remote <<EOF
      echo $HOME
      EOF
      

      要打印远程主目录,您必须使用echo \$HOME

    另外,请记住,如果您想在远程端读取数据文件,例如 filelist.txt,必须明确复制它们。

    【讨论】:

    • 我假设我所要做的就是通过 ssh 连接到服务器。
    猜你喜欢
    • 2011-10-30
    • 2019-08-13
    • 2018-07-16
    • 2014-03-10
    • 1970-01-01
    • 1970-01-01
    • 2011-06-07
    • 2017-10-31
    相关资源
    最近更新 更多