Linux 什么';混合使用远程expect脚本和本地bash命令的最佳方法是什么?
我正在本地和远程机器(防火墙后面)上自动化任务。在远程机器上完成任务后,我希望脚本返回到本地机器上执行命令Linux 什么';混合使用远程expect脚本和本地bash命令的最佳方法是什么?,linux,bash,expect,Linux,Bash,Expect,我正在本地和远程机器(防火墙后面)上自动化任务。在远程机器上完成任务后,我希望脚本返回到本地机器上执行命令 #!/usr/bin/expect -f set timeout -1 spawn ssh username@host expect "Password: " send "mypassword\r" expect "username@host:~$" ...do some stuff... send "exit\r" expect eof [then, once on the local
#!/usr/bin/expect -f
set timeout -1
spawn ssh username@host
expect "Password: "
send "mypassword\r"
expect "username@host:~$"
...do some stuff...
send "exit\r"
expect eof
[then, once on the local machine, change directories and do other things]
附加bash命令的最佳方式是什么?我想我可以从bash开始,在其中调用expect,然后在expect完成后返回bash Expect基于,因此它可以运行相同的命令。但是,如果您的目标是运行bash命令,那么最好的办法就是将它们作为一个单独的脚本从bash中运行,就像您在上一句中建议的那样。这实际上取决于您的想法是什么,
…做些什么…
。下面是我最近从OSX w/s到AWS实例所做的一个示例
export all_status
init_scripts=($(ssh -q me@somehost 'ls /etc/init.d'))
for this_init in ${init_scripts[@]};do
all_status="${all_status}"$'\n\n'"${this_init}"$'\n'"$(ssh -q somehost \'sudo /etc/init.d/${this_init} status\')"
done
echo "$all_status" > ~/somehost_StatusReport.txt
unset all_status
在ssh
命令末尾传递命令将导致该命令在远程主机上运行。或者,您可以scp
将脚本发送到远程主机并使用
ssh somehost '/home/me/myscript'
我最近也遇到了这种情况。我制作了一个shell supexpect.sh,它可以自动登录并执行命令。最后,它将返回到本地shell
#!/usr/bin/expect
#Usage:supexpect <host ip> <ssh username> <ssh password> <commands>
set timeout 60
spawn ssh [lindex $argv 1]@[lindex $argv 0] [lindex $argv 3]
expect "yes/no" {
send "yes\r"
expect "*?assword" { send "[lindex $argv 2]\r" }
} "*?assword" { send "[lindex $argv 2]\r" }
send "exit\r"
expect eof
注:
提示可能需要适应您自己的系统,当然您需要向其传递执行权限。这就是我的建议。从bash scdript运行expect脚本。
./supexpect.sh 10.89.114.132 username password "ls -a;pwd;your_stuff_on_remote_host"