将不会分配伪终端,因为 stdin 不是终端 ssh bash

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17662255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:55:34  来源:igfitidea点击:

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

bashterminal

提问by mkrouse

okay heres part of my code when I ssh to my servers from my server.txt list.

好的,当我从 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

I keep on getting this error and the ssh doesn't actually connect and perform the code on the server its suppose to be ssh'd on.

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

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

回答by Joni

You seem to assume that when you run sshto connect to a server, the rest of the commands in the file are passed to the remote shell running in ssh. They are not; instead they will be processed by the local shell once sshterminates and returns control to it.

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

To run remote commands through sshthere are a couple of things you can do:

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

  • Write the commands you want to execute to a file. Copy the file to the remote server using scp, and execute it with ssh user@remote command
  • Learn a bit of TCL and use expect
  • Write the commands in a heredoc, but be careful with variable substitution: substitution happens in the client, not on the server. For example this will output your local home directory, not the remote:

    ssh remote <<EOF
    echo $HOME
    EOF
    

    To make it print the remote home directory you have to use echo \$HOME.

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

    ssh remote <<EOF
    echo $HOME
    EOF
    

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

Also, remember that data files such as filelist.txthave to be explicitly copied if you want to read them on the remote side.

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

回答by user172643

I feel like everything the guy above just said is so wrong.

我觉得上面那个人刚才说的一切都是错误的。

Expect?

预计?

It's simple:

这很简单:

ssh -i ~/.ssh/bobskey [email protected] << EOF
echo I am creating a file called Apples in the /tmp folder
touch /tmp/apples
exit
EOF

Everything in between the 2 "EOF"s will be run in the remote server.

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

The tags need to be the same. If you decide to replace "EOF" with "WayneGretzky", you must change the 2nd EOF also.

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