我编写了一个脚本,通过 SSH 循环连接到远程服务器并运行命令,它在最后一个服务器上运行后挂起。这是我的循环脚本 -
cat server | while read serName
do
cat cmmnds| while read line
do
result=`ssh -f $serName $line`
echo -n ",$result" >> test.csv
done
echo " " >> test.csv
done
这是最后如何出错并且必须 control+C 退出
Cannot fork into background without a command to execute.
ssh: illegal option -- d
usage: ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
[-D [bind_address:]port] [-e escape_char] [-F configfile]
[-I pkcs11] [-i identity_file]
[-L [bind_address:]port:host:hostport]
[-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
[-R [bind_address:]port:host:hostport] [-S ctl_path]
[-W host:port] [-w local_tun[:remote_tun]]
[user@]hostname [command]
Cannot fork into background without a command to execute.
OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb 2013
Cannot fork into background without a command to execute.
有什么线索吗?
答案1
ssh
您可能不想使用诸如 之类的工具尝试以这种方式执行此操作pssh
。本 U&L 问答题为:在多个主机上执行命令,但如果成功则只打印命令?。
在您的情况下,您可以server
直接将 & 命令文件放入pssh
如下所示:
$ pssh -h server -i -I < commands.txt
例子
这里我构建了以下两个文件:
$ cat servers
skinner
mulder
$ cat commands.txt
date
echo 'hi'
现在当我运行它时:
$ pssh -O ForwardX11=no -h servers -i -I < commands.txt
[1] 03:10:03 [SUCCESS] skinner
Fri Jul 17 03:10:03 EDT 2015
hi
[2] 03:10:04 [SUCCESS] mulder
Fri Jul 17 03:06:41 EDT 2015
hi
您可以像这样捕获上面的输出:
$ .... | tee some_log_file.txt
或者您可以取出-i
交换机并让pssh
使用交换机为每个服务器创建单独的日志文件-o <dir>
。
$ pssh -O ForwardX11=no -h servers -o ~/somedir -I < commands.txt
[1] 03:13:04 [SUCCESS] skinner
[2] 03:13:04 [SUCCESS] mulder
现在检查结果:
$ ls somedir/
mulder skinner
$ cat somedir/*
Fri Jul 17 03:09:41 EDT 2015
hi
Fri Jul 17 03:13:03 EDT 2015