pexpect python

与终端交互 锐捷上网

#!/usr/bin/python
# coding : cp936
# CopyRight 2012 Adou , All Rights Reserved .

import pexpect
import time

child = pexpect.spawn('xrgsu')
print 'xrgsu'

child.expect("Please input your user name:")
child.sendline('02085143')
print 'user name: 02085143'

child.expect("Please input your password:")
child.sendline('123456')
print 'password: 123456'

#child.expect("Use DHCP,1-Use,0-UnUse(Default: 0):")
child.sendline('0')
print 'Unuse DHCP'

#child.expect("Use default auth parameter,0-Use 1-UnUse(Default: 0):")
child.sendline('0')
print 'Use auth parameter'

#child.expect("Please input 'unauth' to LogOff:")
print "'unauth' logoff"

#child.expect(pexpect.EOF)

time.sleep(3)

print "Enjoy the internet !"

shell写法,这个才真正能用

#!/usr/bin/expect -f

set timeout -1

spawn xrgsu

#match_max 100000

expect -exact "Please input your user name:"
send -- "02085143\r"
expect -exact "Please input your password:"
send -- "123456\r" 
expect -exact "Use DHCP,1-Use,0-UnUse(Default: 0):"
send -- "0\r"
expect -exact "Use default auth parameter,0-Use 1-UnUse(Default: 0):"
send -- "0\r"
expect -exact "Please input 'unauth' to LogOff:"
expect eof