Expect

From Halfface
Revision as of 15:57, 14 May 2008 by 88.80.164.166 (talk)
Jump to navigation Jump to search

Links.

http://floppsie.comp.glam.ac.uk/Glamorgan/gaius/scripting/5.html
http://expect.nist.gov/scripts/
Script.
#!/usr/bin/expect -f
#
# expect_ssh.exp
# bjorklun
#
if { $argc < 1 } {
       send_user "Usage: ./ssh-login.exp <username> <password> <hostname> <command> \r"
       exit
}
#
set username [lrange $argv 0 0]
set password [lrange $argv 1 1]
set hostname [lrange $argv 2 2]
set command [lrange $argv 3 3]
set arguments [lrange $argv 4 9]

set timeout -1
match_max 1000

spawn ssh $username@$hostname $command $arguments

expect {
        "Are you sure you want to continue connecting (yes/no)? " {
                send -- "yes\r"
                expect "*?assword:*" {
                        send -- "$password\r"
                        send -- "\r"
                        }
                }
        "*?assword:*" {
                send -- "$password\r"
                send -- "\r"
        }
        "*denied*" {
                puts "You are a loser Andreas\r"
        }
}

expect eof