bash shell脚本ssh远程机器并打印top命令的输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20943610/
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
shell script to ssh remote machines and print the output of the top command
提问by user3154564
I want to write a shell script to do the following four things:
我想写一个shell脚本来做以下四件事:
- ssh a remote machine (say hosti)
- print the machine name to a file (top_out)
- print the first few lines of the output of the 'top' command to the same file as in step2
- repeat 1-3 for an other machine
- ssh 远程机器(比如hosti)
- 将机器名称打印到文件 (top_out)
- 将“top”命令输出的前几行打印到与步骤 2 相同的文件中
- 对另一台机器重复 1-3
I tried this:
我试过这个:
#! /bin/bash
for i in 1 2 3 4 5 6 7 8
do
echo "host$i" >> ~/mysh/top_out
ssh host$i "top -n1 -b | head -n 15>> ~/mysh/top_out"
echo "done"
done
The output file that I got had saved the top output for some machines (say like host5-8), but it was blank for the early machinessay like host1-4. If I tried without the line "echo "host$i" >> ~/mysh/top_out", I can get the top output for all the host1-8.
我得到的输出文件已经保存了一些机器(比如host5-8)的最高输出,但是对于早期的机器比如host1-4,它是空白的。如果我尝试不使用“echo "host$i" >> ~/mysh/top_out”这一行,我可以获得所有主机 1-8 的顶部输出。
回答by andrewdotn
When you do
当你做
ssh host$i "top -n1 -b | head -n 15>> ~/mysh/top_out"
you're writing the output to ~/mysh/top_out
on the remote host, not the local machine. The remote host might not be using the same physical home directory as your local machine. If you have NFS or something sharing your home directory on some machines but not all, then you'd see the symptoms you described.
您正在将输出写入~/mysh/top_out
远程主机,而不是本地机器。远程主机可能没有使用与本地机器相同的物理主目录。如果您有 NFS 或在某些机器上共享您的主目录的东西,但不是所有机器,那么您会看到您描述的症状。
Try doing
尝试做
ssh host$i "top -n1 -b | head -n 15" >> ~/mysh/top_out
instead, or to make things slightly cleaner, maybe even
相反,或者让事情变得更干净,甚至可能
#!/bin/bash
for i in $(seq 1 8); do
(echo "host$i"
ssh host$i "top -n1 -b | head -n 15") >> ~/mysh/top_out
echo "done host$i"
done
回答by Victor Palade
you can try an expect script to save the output of each hosts after it connects to it, you can also add more commands to it, p.s. : this assumes you have the same username and password for all hosts :
您可以尝试使用 expect 脚本在连接到每个主机后保存每个主机的输出,您还可以向其添加更多命令,ps:假设您对所有主机具有相同的用户名和密码:
#/usr/bin/expect -f
#write your hosts on a new line inside a file and save it in your workging directory as:
#host 1
#host 2
#host 3
#user '' for password if it contains special chars
#pass arguments to the script as ./script $username '$password' $hosttxt
set user [lindex $argv 0]
set pass [lindex $argv 1]
#pass path to txt file with line separated hosts
set fhost [lindex $argv 2]
#set this to the path where you need to save the output e.g /home/user/output.txt
set wd "/home/$user/log.txt"
#open hosts file for parsing
set fhosts [open $fhost r]
exec clear
#set loguser 1
proc get_top {filename line user pass} {
spawn ssh -l $user $line
expect {
"$ " {}
"(yes/no)? " {
send "yes\r"
expect -re "assword:|assword: "
send "$pass\r"
}
-re "assword:|assword: " {
send "$pass\r"
}
default {
send_user "Login failed\n"
exit 1
}
}
expect "$ " {}
send "top -n1 -b | head -n 15\r"
expect -re "\r\n(.*)\r(.*)(\$ |# )" {
set outcome "$expect_out(1,string)\r"
send "\r"
}
puts $filename "$outcome\n\n-------\n"
}
while {[gets $fhosts line] !=-1} {
set filename [open $wd "a+"]
get_top $filename $line $user $pass
close $filename
}
回答by Chinmay Inamdar
Check if the hosts for which you are not getting the output is showing following error:
检查您未获得输出的主机是否显示以下错误:
TERM environment variable not set.
未设置 TERM 环境变量。
If you are getting this error for some of the hosts you can try following command:
如果您在某些主机上遇到此错误,您可以尝试以下命令:
ssh user@host "screen -r; top" >> file_where_you_want_to_save_output
ssh user@host "screen -r; top" >> file_where_you_want_to_save_output