简体   繁体   English

Unix:ssh 正在通过命令行而不是通过脚本工作

[英]Unix : ssh is working through command line but not through script

Below command is working through command line:下面的命令是通过命令行工作的:

expect -c 'spawn ssh username@Host ; expect "assword:" ; send "<password>\r" ; interact;'

Below is not working if I include in a script如果我包含在脚本中,则以下内容不起作用

while read server_from_file
do
    expect -c 'spawn ssh username@${server_from_file}; expect "assword:" ; send "<password>\r" ; interact;'
done < serverlist.conf

Please also let me know how could we run certain commands using the above script还请让我知道我们如何使用上述脚本运行某些命令

expect inherits stdin from its environment. expect从其环境继承标准输入。 In the first case, expect uses the script's stdin as its stdin.在第一种情况下, expect使用脚本的标准输入作为它的标准输入。 In the second case, expect inherits stdin from the enclosing while loop, so it will be reading from the file.在第二种情况下, expect从封闭的 while 循环继承标准输入,因此它将从文件中读取。 One typical solution would be to use a different fd for the loop.一种典型的解决方案是为循环使用不同的 fd。 eg:例如:

while read server_from_file <&3  
do
    expect -c 'spawn ssh username@${server_from_file}; expect "assword:" ; send "<password>\r" ; interact;'
done <3 serverlist.conf

(Note, some shells provide -u, and there are other ways to do this, but this should get you pointed in the right direction.) (注意,一些 shell 提供了 -u,还有其他方法可以做到这一点,但这应该会让你指向正确的方向。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM