我需要创建一个bash脚本,该脚本将在一批计算机上远程运行另一个脚本。为此,我通过SSH传递了脚本。
ssh -p$port root@$ip 'bash -s' < /path/to/script/test.sh
我以为它将使用我的RSA密钥,但出现错误:
"Enter password: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)"
我尝试使用sshpass无济于事。所以我的下一个解决方案是使用Expect。我以前从未使用过Expect,我很肯定我的语法还很遥远。
ssh -p$port root@$ip 'bash -s' < /path/to/script/test.sh
/usr/bin/expect <<EOD
expect "password"
send "$spass\n"
send "\n"
EOD
我具有对所有计算机的root访问权限,只要代码保留在bash中,任何解决方案都可以。请记住,这将通过从父脚本传递的全局变量($ spass,$ ip,$ port等)在循环中完成。
您通过两种方式做错了:
如果要expect
与进行交互ssh
,则需要ssh
从expect
脚本而不是之前开始。
如果将脚本(/path/to/script/test.sh
)放在stdin
of中ssh
,则无法再与该ssh
进程进行通信。
您应该使用scp将脚本复制到远程主机,然后运行它。
Expect脚本可能如下所示:
/usr/bin/expect <<EOF
spawn ssh -p$port root@$ip
expect "password"
send "$Spass\r"
expect "$ "
send "/path/to/script/on/remote/server/test.sh\r"
expect "$ "
interact
EOF
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句