#!/usr/bin/expect -f # Expect script for SSH logging as $username to $hostname using $password and executing $command. # Usage: # ./ssh.exp username password hostname command set username [lrange $argv 0 0] set password [lrange $argv 1 1] set hostname [lrange $argv 2 2] set command [lrange $argv 3 10] set timeout 15 # connect to remote host and execute given command log_user 1 spawn ssh -t $username@$hostname $command expect { -nocase "yes/no" { send -- "yes\r" ; exp_continue } -nocase "password" { send -- "$password\r" } } log_user 1 # send -- "\r" expect eof