![](/img/trans.png)
[英]Write a specific bit in a 16bits register with modbus in python
[英]ssh result seems to only be 16bits in python with paramiko
我在python 2.7中使用paramiko连接到cisco路由器,发送命令,然后在for循环中解析命令的输出。 问题似乎是返回的结果限制为65535个字符(16位)。 我打印输出并将其粘贴到编辑器中以计算字符数,这就是给我的结果。 我确定我做错了这是因为我在学习python时会学习hehe。 这是代码:
import sqlite3
import paramiko
import time
import re
def disable_paging(remote_conn):
'''Disable paging on a Cisco router'''
remote_conn.send("terminal length 0\n")
time.sleep(1)
output = remote_conn.recv(10000)
return output
if __name__ == '__main__':
username = 'user'
password = 'password'
db = sqlite3.connect('cmts-priv.sqlite')
cursor = db.cursor()
cursor.execute('''SELECT ID, ip, hostname, uptime, active, cmtstype FROM cmts''')
all_rows = cursor.fetchall()
print "Producing report. This takes a few seconds so be patient and do not refresh\n"
for row in all_rows:
if (row[4] == 1):
print "Docsis 1.x modems for : " + row[2]
print"\n"
remote_conn_pre = paramiko.SSHClient()
remote_conn_pre.set_missing_host_key_policy(
paramiko.AutoAddPolicy())
ip=row[1]
remote_conn_pre.connect(ip, username=username, password=password)
remote_conn = remote_conn_pre.invoke_shell()
disable_paging(remote_conn)
remote_conn.send("\n")
remote_conn.send("show cable modem docsis version | inc 1\.[10] 1\.[10]\n")
time.sleep(5)
output = remote_conn.recv(100000000)
output = output.split("\n")
remote_conn_pre.close()
#print output
for i in output:
if "0013.11" not in i and "0015.96" not in i and "0015.a2" not in i and "0015.a3" not in i and "0015.a4" not in i and "0015.ce" not in i and "0015.cf" not in i and "0015.d0" not in i:
X = '([0-9a-fA-F]{4}\.[0-9a-fA-F]{4}\.[0-9a-fA-F]{4})'
c = re.compile(X).finditer(i)
if c:
for y in c:
print i[y.start(): y.end()]
print "\n=============================\n"
这将赋予输出变量的结果如下:
00a0.7373.2a14 C4 / 0 / U1在线11 1.0 1.0 tdma NB
00a0.7372.ed18 C4 / 0 / U1在线12 1.0 1.0 tdma NB
00a0.7373.2af2 C4 / 0 / U0在线20 1.1 1.1 tdma NB
.....
每个路由器约有3500个结果
然后,我在过滤了我不想要的Mac之后提取了Mac,并将它们输出到列表中。 问题是我从路由器取回的结果似乎停止在16位,而实际上我会得到更多。 与我直接在路由器cli中产生输出相比,它停止在大约1/6处。 我尝试玩超时,睡眠和recv缓冲区。 我只是想不通这一点:( of和sqlite db存储了一些东西,因为我在一些脚本中使用了它。row [1]的作用是路由器的ip将其馈送到连接字符串。Im当然,一群人会说我让我的生活变得更加地狱,但是就像我说的那样,我正在从google搜索中学习所有这些内容,然后试图理解它。我从路由器上得到的部分和不完整的结果。帮助:(
您需要围绕recv
进行循环,如下所示:
buff = ''
while not buff.endswith(':~# '):
resp = chan.recv(9999)
buff += resp
print(resp)
参见以下示例: https : //gist.github.com/rtomaszewski/3397251
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.