Expect

From Halfface
Revision as of 14:38, 20 October 2008 by Ekaanbj (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Links.

http://floppsie.comp.glam.ac.uk/Glamorgan/gaius/scripting/5.html
http://expect.nist.gov/scripts/

info

timeout

set timeout 60

timeout of -1 signifies that expect should wait forever. timeout 0 indicates that expect should not wait at all.

regexp

Optional Regexp
The postfix question mark (?) operator makes the preceding regexp optional (i.e, it can occur zero or one times). This shorthand is equivalent to a common  use of summing. These two regexps are equivalent:

a(foo)?
afoo|a

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