Truman Boyes零与一

Blogspot

埋头写 Python

作者:Truman Boyes · · 1 分钟阅读

我正在写一个快速脚本,从 JUNOSe 的 CLI 拉取一些统计信息。以前我大量使用 PERL,写过标准的正则解析脚本、套接字编程以及各种网络相关的应用。最近我发现 Python 非常强大,而且使用起来很轻松。

我已经开始使用 pexpect 来取代普通的 TCL expect。

下面是一个快速脚本的示例,用来从 Juniper ERX 拉取队列统计:

#!/usr/bin/python
# quick JUNOSe Queue View

import pexpect
import getpass

mypassword = "juniper123"
interface_name ="fast 0/0"
host = raw_input('Hostname: ')

child = pexpect.spawn('telnet %s'%(host,))
child.expect ('(?i)assword')
child.sendline (mypassword)
child.expect ('>')
child.sendline ('show version')
child.expect ('>')
#print child.before
child.sendline ('enable')
child.expect ('#')
child.sendline ('term leng 0')
child.expect ('#')
child.sendline ('show ip interface fast 5/0 | begin queue 0')
child.expect ('#')
queue_out = child.before
print "The output of the queue is :"
print queue_out

接下来要做的是解析实际的队列值,并把这些值随时间绘制出来。


最初发表于 trumanboyes.blogspot.com。查看归档副本。