[英]Paramiko not receiving all data from Channel
I've done a script to configure a couple of ASA devices.我已经完成了一个脚本来配置几个 ASA 设备。 It does the job perfectly BUT I can't get the whole output from the devices I'm configuring, at some point the data gets stucked and there's no more output. I want to have that in order to check for problems or misconfigurations, etc. I'm configuring around 500 IPs, objects, groups, etc. from files on ASA firewalls... I don't know what to do, I haven't found any command to clean or erase Paramiko's buffer:(
它完美地完成了工作,但我无法从我正在配置的设备中获取整个 output,在某些时候数据被卡住并且没有更多的 output。我想要它来检查问题或配置错误等. 我正在从 ASA 防火墙上的文件中配置大约 500 个 IP、对象、组等...我不知道该怎么做,我还没有找到任何命令来清理或擦除 Paramiko 的缓冲区:(
Any ideas?有任何想法吗? This is my code:
这是我的代码:
import paramiko
import re
import time
from tqdm.auto import tqdm
from io import StringIO
device_ip = 'X.X.X.X'
ssh = paramiko.SSHClient() # Connection
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname = device_ip, username = username, password = password)
connection = ssh.invoke_shell()
output_data = StringIO()
connection.send("conf t\n") # Enter configuration mode
time.sleep(1)
file = open(file_name, 'r') # IP list file
lines = file.readlines()
objects = []
ip_subnet = re.compile(r'([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\/([0-9]+)')
group_name = "GRP-OBJ-EU-Blablabla"
for line in tqdm(lines):
match = ip_subnet.findall(line.strip())
ip = match[0][0]
subnet = match[0][1] # Not used, for future use may be...
object_name = "obj-"+ip
object_configuration = "object network "+object_name
object_network = "host "+ip
object_description = "description whatever"
objects.append(object_name)
connection.send(object_configuration+"\n")
time.sleep(1)
connection.send(object_network+"\n")
time.sleep(1)
connection.send(object_description+"\n")
time.sleep(1)
received_data = connection.recv(5000).decode(encoding='utf-8')
if received_data:
output_data.write(received_data)
group_command = "object-group network "+group_name
connection.send(group_command+"\n")
time.sleep(1)
for object_host in tqdm(objects):
connection.send("network-object object "+object_host+"\n")
time.sleep(1)
received_data = connection.recv(5000).decode(encoding='utf-8')
if received_data:
output_data.write(received_data)
connection.send("end \n")
time.sleep(1)
connection.send("wr \n")
time.sleep(5)
connection.close()
ssh.close()
file.close()
print(output_data)
I've tried a line like this one below but it does not work either:我试过下面这样的一行,但它也不起作用:
device_output = connection.recv(1000000000000).decode(encoding='utf-8')
I could not directly test your code, but I had a similar problem in the past and found a practical solution.我无法直接测试您的代码,但我过去遇到过类似的问题并找到了实用的解决方案。 To get all the data at once, you can open a notepad and print the received_data data in it.
要一次获取所有数据,您可以打开记事本并在其中打印 received_data 数据。 You can also use ascii outside of the encoding='utf-8' method.
您还可以在 encoding='utf-8' 方法之外使用 ascii。
Example Code:示例代码:
received_data = remote_connection.recv(99999999)
result = received_data.decode('ascii').strip("\n")
LOGfile = "log.txt"
log = open(LOGfile, 'a')
log.write(result )
log.close()
I recommend Netmiko-TTP library to parse all the data you want except the Paramiko library.我推荐 Netmiko-TTP 库来解析除 Paramiko 库之外的所有你想要的数据。 I have given a sample code link below.
我在下面给出了示例代码链接。 I hope that will be useful.
我希望这会有用。
https://github.com/MertKulac/Nokia--TTP--Template--Command--Set/blob/main/show%20system%20informtaion.py https://github.com/MertKulac/Nokia--TTP--Template--Command--Set/blob/main/show%20system%20informtaion.py
Well I've found the solution... it's pretty dumb.好吧,我找到了解决方案……这很愚蠢。 I was not exiting enable mode... and it seems that was it.
我没有退出启用模式……似乎就是这样。 lol I don't get the relation but anyways the lines below work... Thanks everyone for your help!
大声笑我没有得到关系,但无论如何下面的几行工作......感谢大家的帮助!
....
connection.send("end \n")
time.sleep(1)
connection.send("wr \n")
time.sleep(5)
device_output = connection.recv(10000000).decode(encoding='utf-8')
connection.close()
ssh.close()
file.close()
print(device_output)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.