Expect: spawn id exp5 not open while executing "expect "$" { send "sudo su -\r" }"

Hi All,
i am trying to ssh to a remote machine and execute certain command to remote machine through script.
i am able to ssh but after its getting hung at the promt and after pressing ctrl +d i am gettin the out put as

expect: spawn id exp5 not open
    while executing
"expect "$" { send "sudo su -\r" }"

can any one help me with this.

here is my code.

#!/usr/bin/expect -f
set host [lindex $argv 0]
#set user [lindex $argv 1]
spawn scp -r /home/uname/dhclient/  upupmadm@$host:/home/uname/
sleep 5
expect "*password: " { send "******\r"; exp_continue }
#expect "$"
spawn ssh uname@$host
sleep 5
#expect "yes/no"
#send "yes\r"
expect "*password: " { send "******\r"; exp_continue }
interact
expect "$" { send "sudo su -\r" }
expect "#" { send "cd /home/uname/dhclient/\r" }
expect "#" { send "./upgrade.sh" }
expect "#"
send "exit\r"
 send "exit\r"

Embedding plaintext passwords in scripts is incredibly insecure. You could try modifying sudoers instead of making everything so hard on yourself.

yes but my question is i am not able to execute the commands further after login to remote server.

I would recommend removing expect from your process entirely.

Put simply,

  • You can set up can set up sudo rules so that a password is not needed.
  • You can set up SSH keys so that a password is not needed for ssh, scp and similar.

You should be able to then simply do something like these:-

scp -r /home/uname/dhclient/  upupmadm@$host:/home/uname/
sudo ssh uname@host "cd /home/uname/dhclient ; ./upgrade.sh"

Does that help?

We can test the two parts by:-

ssh uname@$host hostname
sudo ssh uname@$host hostname

If set up correctly, neither should prompt for a password. The output should be something obvious.

How far do you get? If you get prompted, can you show us the attempt and the output (wrapped in CODE tags, of course)

Thanks, in advance,
Robin

1 Like