在期望失败的情况下登录sudo - BASH

时间:2013-12-10 18:52:25

标签: linux bash ssh expect

这是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$

注意最后一行,我不确定为什么密码没有通过。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

您可以在不期望的情况下执行此操作:查看系统的sudo是否具有-S选项,该选项从stdin读取密码。

#!/bin/sh
echo "$2" | ssh "$1"@remote sudo -S sh -c ./run.sh runtime
相关问题