You may be able to do this without expect: See if your system's sudo
has a -S
option, which reads the password from stdin.
#!/bin/sh
echo "$2" | ssh "$1"@remote sudo -S sh -c ./run.sh runtime
Question
Here is my expect script that a bash script calls on:
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
My results are:
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$
Notice the last line, I'm not sure why the password is not passing. Any ideas?
Solution
You may be able to do this without expect: See if your system's sudo
has a -S
option, which reads the password from stdin.
#!/bin/sh
echo "$2" | ssh "$1"@remote sudo -S sh -c ./run.sh runtime