#!/usr/bin/python
Import Sys
Import time
Import OS
Import Pexpect
now = Time.strftime ('%y-%m-%d ', Time.localtime (Time.time ()))
AA = open ('/home/hanlei/test/%s/log.txt '% now, "W")
Fout = open ('/home/hanlei/test/%s/log.txt '% now, "W")
IP = open ('/home/hanlei/test/switch_ip ', ' R ')
PASS = open ('/home/hanlei/test/switch_passwd ', ' R ')
While 1:
Readip = Ip.readline ()
Readpass = Pass.readline ()
If not READIP:
print ' END '
Break
Foo = pexpect.spawn (' telnet%s '% Readip)
Foo.log_file = Fout
Foo.expect ([' Username: '])
Foo.sendline (' admin ')
Foo.expect ([' Password: '])
Foo.sendline (' admin ')
Foo.expect ([' > '])
Foo.sendline (' en ')
Foo.expect ([' Password: '])
Foo.sendline (Readpass)
Foo.expect ([' # '])
Foo.sendline (' Copy flash:config.text SCP: ')
Foo.expect ([' Address or name of remote host '])
Foo.sendline (' 192.168.1.141 ') #FTP SERVER
Foo.expect ([' Destination username '])
Foo.sendline (' Cisco ')
Foo.expect ([' Config.text '])
Foo.sendline (' t1206 ')
Foo.expect ([' Password: '])
Foo.sendline (' 123456 ')
A = Foo.expect ([' bytes/sec ', ' Error ', Pexpect. EOF, Pexpect. TIMEOUT])
If a = = 0:
Aa.write ('%s......ok\n '% Readip)
Foo.expect ([' # '])
Foo.sendline (' Quit ')
If a = = 1:
Aa.write ('%s......failed\n '% Readip)
Foo.sendline (' Quit ')
Foo.expect ([' Closed '])
Foo.interact
print ' Done '
Python crawl Cisco switch profile