Paramiko 未从频道接收所有数据
Paramiko not receiving all data from Channel
我已经编写了一个脚本来配置几个 ASA 设备。它完美地完成了工作,但我无法从我正在配置的设备获得全部输出,在某些时候数据被卡住并且没有更多输出。我想要它来检查问题或配置错误等。我正在从 ASA 防火墙上的文件配置大约 500 个 IP、对象、组等……我不知道该怎么做,我还没有没有找到任何清理或擦除 Paramiko 缓冲区的命令:(
有什么想法吗?这是我的代码:
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)
我试过像下面这样的一行,但它也不起作用:
device_output = connection.recv(1000000000000).decode(encoding='utf-8')
我无法直接测试你的代码,但我过去遇到过类似的问题并找到了一个实用的解决方案。要一次获取所有数据,您可以打开记事本并在其中打印 received_data 数据。您还可以在 encoding='utf-8' 方法之外使用 ascii。
示例代码:
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()
我推荐 Netmiko-TTP 库来解析除 Paramiko 库之外的所有你想要的数据。我在下面给出了示例代码 link。希望有用。
好吧,我找到了解决方案……这很愚蠢。我没有退出启用模式……似乎就是这样!大声笑我没有得到关系,但无论如何下面的行工作......感谢大家的帮助!
....
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)
我已经编写了一个脚本来配置几个 ASA 设备。它完美地完成了工作,但我无法从我正在配置的设备获得全部输出,在某些时候数据被卡住并且没有更多输出。我想要它来检查问题或配置错误等。我正在从 ASA 防火墙上的文件配置大约 500 个 IP、对象、组等……我不知道该怎么做,我还没有没有找到任何清理或擦除 Paramiko 缓冲区的命令:(
有什么想法吗?这是我的代码:
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)
我试过像下面这样的一行,但它也不起作用:
device_output = connection.recv(1000000000000).decode(encoding='utf-8')
我无法直接测试你的代码,但我过去遇到过类似的问题并找到了一个实用的解决方案。要一次获取所有数据,您可以打开记事本并在其中打印 received_data 数据。您还可以在 encoding='utf-8' 方法之外使用 ascii。
示例代码:
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()
我推荐 Netmiko-TTP 库来解析除 Paramiko 库之外的所有你想要的数据。我在下面给出了示例代码 link。希望有用。
好吧,我找到了解决方案……这很愚蠢。我没有退出启用模式……似乎就是这样!大声笑我没有得到关系,但无论如何下面的行工作......感谢大家的帮助!
....
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)