下面是脚本。
我想登录几台服务器并检查内核版本。
#!/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 台主机的输出,它xxxxdev01
也带有 ssh 横幅和其他警告。
我需要所有其他主机的输出并且没有 ssh 横幅。这里出了什么问题?
答案1
我无法告诉您为什么没有从hostname
和uname
命令中获得预期的输出,但我可以帮助您处理无关的文本。
打印“伪终端”行是ssh
因为当命令行上没有提供要执行的命令时,它会尝试默认分配 TTY。您可以通过在 ssh 命令中添加“-T”来避免该消息:
sshpass -p password ssh -T root@$line
“警告:无法访问 tty”行来自远程系统上的 shell。csh
并tcsh
会在某些情况下打印该消息。它可能是由远程系统上的文件或类似文件中的某些内容触发的.cshrc
,试图访问某些需要 TTY 的功能。
答案2
使用以下代码,
#!/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
答案3
如果您的主机存储如下server.txt
host1.tld
host2.tld
....
你可以
mapfile -t myhosts < server.txt; for host in "${myhosts[@]}"; do ssh username@"$host" 'hostname;uname -r'; done
答案4
这对我来说非常有用:
# 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 不是终端