ssh和运行命令的脚本不起作用


10

下面是脚本。

我想登录几台服务器并检查内核版本。

#!/bin/bash
#input server names line by line in server.txt
cat server.txt | while read line
do
sshpass -p password ssh root@$line << EOF
hostname
uname -r
EOF
done

我希望输出像..

server1_hostname
kernel_version
server2_hostname
kernel_version

等等..

我在server.txt中运行了约80个服务器的脚本

我得到的输出就像.....

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

========================================================================
================================ WARNING ===============================
========================================================================
This system is solely for the use of authorized personnel. Individuals
using this system are subject to having some or all of their activities
monitored and recorded. Anyone using this system expressly consents to
such monitoring and is advised that any unauthorized or improper use of
this system may result in disciplinary action up to and including
termination of employment. Violators may also be subject to civil and/or
criminal penalties.
========================================================================

Warning: no access to tty (Bad file descriptor).
Thus no job control in this shell.
xxxxdev01
2.6.32-431.23.3.el6.x86_64
Pseudo-terminal will not be allocated because stdin is not a terminal.
Pseudo-terminal will not be allocated because stdin is not a terminal.
Pseudo-terminal will not be allocated because stdin is not a terminal.
Pseudo-terminal will not be allocated because stdin is not a terminal.
Pseudo-terminal will not be allocated because stdin is not a terminal.

在这里,我仅获得了1台主机的输出,这也是xxxxdev01ssh标语和其他警告所附带的。

我需要所有其他主机的输出,并且没有ssh banner。


如果您手动使用其中一台服务器并运行该sshpass -p password root@server histname怎么办?
terdon

1
使用ssh -t -t root@...强制使用伪终端。
garethTheRed 2014年

Answers:


11

我无法告诉您为什么无法从hostnameand uname命令获得预期的输出,但是我可以帮助处理多余的文本。

之所以打印“伪终端”行,是ssh因为当命令行上未提供要执行的命令时,默认情况下它将尝试分配TTY。您可以通过在ssh命令中添加“ -T”来避免该消息:

sshpass -p password ssh -T root@$line

“警告:无法访问tty”行来自远程系统上的Shell。csh并将tcsh在某些情况下打印该消息。可能是由.cshrc远程系统上或类似文件中的某些内容触发的,试图访问某些需要TTY的功能。


4

使用以下代码,

#!/bin/bash
#input server names line by line in server.txt
cat server.txt | while read line
do
  sshpass -p password ssh root@$line 'hostname;uname -r'
done

这是我首先尝试的。但这并没有给我预期的输出。
成为Gokul 2014年


1

您的远程命令无法访问stdin。您可以做的是使用bash的“ -s”标志从stdin中读取命令:

从bash手册:

-s        If the -s option is present, or if no arguments remain after
          option processing, then commands are read from the standard 
          input.  This option allows the positional parameters to be set
          when  invoking  an  interactive shell.

所以这应该做你想要的:

#!/bin/bash
#input server names line by line in server.txt
cat server.txt | while read line
do
    sshpass -p password ssh root@$line bash -s << EOF
hostname
uname -r
EOF
done

另请参阅:https : //stackoverflow.com/questions/305035/how-to-use-ssh-to-run-shell-script-on-a-remote-machine


1

这对我很有用:

 # cat hostsname.txt 
operation01  172.20.68.37 5fDviDEwew
ngx-gw01     172.20.68.36 FiPp2UpRyu
gateway01    172.20.68.35 KeMbe57zzb
vehicle01    172.20.68.34 FElJ3ArM0m

# cat hostsname.txt | while read hostname ipaddr passwd; do sshpass -p $passwd /usr/bin/ssh-copy-id $ipaddr;done

注意使用-t -t而不是-T避免错误

因为stdin不是终端,所以不会分配伪终端


1
您应该阅读格式化。您的答案可能很好,但是现在几乎无法理解。
roaima

0

我想这时的ssh会把其余的全部吃光。您可以参考Bash FAQ 89了解详细信息。对于FileDescriptor,以下代码应可以正常工作。

while read line <& 7
do
sshpass -p password ssh root@$line << EOF
hostname
uname -r
EOF
done 7< server.txt

另一种方法是对ssh使用/ dev / null。FileDescriptor可以忽略。`同时读取行;做sshpass -p密码ssh root @ $ line </ dev / null << EOF ....完成<server.txt`
Leon Wang
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.