这是我期望的 bash 脚本调用的脚本:
1 #!/usr/bin/expect -f
2
3 set timeout 1
4 set user [lindex $argv 0]
5 set pw [lindex $argv 1]
6
7 spawn ssh $user@remotehost.com
8 expect "$"
9 send -- "cd /opt/Data/\r"
10 expect "$"
11 send -- "sudo su runtime\r"
12 expect "*?assword for*"
13 send -- "$pw\r"
14 expect "$"
15 send -- "./run.sh\r"
16 expect "$"
17 send -- "exit\r";
18 send -- "exit\r";
19 exit
我的结果是:
user@localhost:~/Documents/scripts$ ./bashscript
Enter your web password:
spawn ssh user@remotehost.com
Last login: Tue Dec 10 11:10:37 2013 from user@localhost
[user@remotehost.com ~]$ cd /opt/Data/
[user@remotehost.com /opt/Data]$ sudo su runtime
[sudo] password for user: user@localhost:~/Documents/scripts$
注意最后一行,我不确定为什么密码没有通过。有什么想法吗?
最佳答案
你可能会在意想不到的情况下做到这一点:看看你的系统的 sudo
是否有一个 -S
选项,它从 stdin 读取密码。
#!/bin/sh
echo "$2" | ssh "$1"@remote sudo -S sh -c ./run.sh runtime
关于linux - 预期失败时登录 sudo - BASH,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20502877/