I have the following code which I've thieved from here, there and everywhere:
Code:
[root@localhost /]# cat autossh
#!/usr/bin/expect -f
set ipaddr [lrange $argv 0 0]
set username [lrange $argv 1 1]
set password [lrange $argv 2 2]
set command [lrange $argv 3 11]
set timeout -1
spawn ssh $username@$ipaddr
match_max 100000
expect "*?assword:*"
send -- "$password\r"
expect {
"#*" {}
"*>*" {}
}
send -- "$command\r"
#send -- "\r"
send -- "exit\r"
expect eof
This works great unless I put stuff in speech marks or when I need to run multiple commands, e.g the following wont work:
autossh 123.123.123.123 root password ls -al && cd / && ls -al
It gives me the contents of ~ on my made up 123 host, then exits and gives me the contents of / on my local machine. If I do the same thing with speech marks I get the following error:
Code:
root@hostname # {ls -al && cd / && ls -al}
exit
{ls: not found
Ideally I'd like to be execute either of the above commands properly. Can anyone help.
Thanks