- 论坛徽章:
- 0
|
本帖最后由 liuyaming0938 于 2014-09-10 16:45 编辑
想把获取到的字符分行保存,琢磨了半天没成功
#!/usr/bin/env python
import pexpect
address = '****'
userName = '****'
password = '****
cmd = 'telnet ' + address
prompt = '>'
cmd2='dis cu'
child = pexpect.spawn(cmd)
index = child.expect(['Username',pexpect.EOF,pexpect.TIMEOUT],timeout=1)
if index == 0:
child.sendline(userName)
index = child.expect('Password',timeout=1)
child.sendline(password)
child.expect(prompt,timeout=1)
child.sendline(cmd2)
child.expect(cmd2,timeout=1)
child.expect('More ----',timeout=1)
sstr1=child.before
sstr1=str(sstr1)
sstr2='/r' ###想把含有/r标记的分行保存输出到文件或者数据库保存以后可以配合数据库自动恢复和管理配置文件
sstr1=sstr1[sstr1.find(sstr2) +1:]
print (sstr1)
else:
print ('expect "login",but get EOF or TIMEOUT')
child.close()
|
|