无法在Expect脚本中通过SSH会话执行远程命令

时间:2019-05-29 00:03:02

标签: ssh expect

团队,使用Expect脚本,密码提示上什么也没有发生,并且我的远程命令未执行。我通过ssh代理,然后从那里执行Expect脚本以ssh到远程计算机并运行一些命令。

`#!/usr/bin/expect -f
set PROXY_PASSWORD $env(PROXY_PASSWORD)
set DOM0_PASSWORD $env(DOM0_PASSWORD)
set DOM0_USERNAME $env(DOM0_USERNAME)
spawn ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $env(PROXY_USERNAME)@8.4.1.2
expect "password"
send -- "$PROXY_PASSWORD\r"
send "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $DOM0_USERNAME@$dom0_host_ip 'export PATH=~/.local/bin:\$PATH; source ~/.bash_profile; source /etc/profile.d/lang.sh; sed -i 's/enabled=0/enabled=1/g' /etc/yum.repos.d/*; yum install -y centos-release-scl'\r"
expect "password"
send "$DOM0_PASSWORD\r"
interact`

输出:      ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/d ev/null root@10.2.1.100 'export PATH=~/.local/bin:$PATH; source ~/.bash_profile;
source /etc/profile.d/lang.sh; sed -i 's/enabled=0/enabled=1/g' /etc/yum.repos.d /*; yum install -y centos-release-scl' Warning: Permanently added '10.2.1.100' (ECDSA) to the list of known hosts.

root@10.2.1.100's password: 
Finished: SUCCESS

我的目标是ssh代理,然后运行Expect脚本,该脚本将ssh到远程并运行命令并退出。

我也在下面尝试过,但登录后未执行任何命令。

spawn ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $env(PROXY_USERNAME)@8.1.2.2
expect "password"
send "$PROXY_PASSWORD\r"
expect "$"
send "ls -ltr\r"
send "pwd\r"
send "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $NGN_DOM0_USERNAME@$dom0_host_ip\r"
expect "password"
send "$DOM0_PASSWORD\r"
send "pwd\r"
expect "#"
send "source /etc/profile.d/lang.sh\r"
send "export PATH=~/.local/bin:\$PATH\r"
send "source ~/.bash_profile\r"
send "source /etc/profile.d/lang.sh\r"
send "sed -i 's/enabled=0/enabled=1/g' /etc/yum.repos.d/*\r"
send "yum install -y centos-release-scl\r"
interact

0 个答案:

没有答案