簡體   English   中英

Paramiko 未從頻道接收所有數據

[英]Paramiko not receiving all data from Channel

我已經完成了一個腳本來配置幾個 ASA 設備。 它完美地完成了工作,但我無法從我正在配置的設備中獲取整個 output,在某些時候數據被卡住並且沒有更多的 output。我想要它來檢查問題或配置錯誤等. 我正在從 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 庫之外的所有你想要的數據。 我在下面給出了示例代碼鏈接。 我希望這會有用。

https://github.com/MertKulac/Nokia--TTP--Template--Command--Set/blob/main/show%20system%20informtaion.py

好吧,我找到了解決方案……這很愚蠢。 我沒有退出啟用模式……似乎就是這樣。 大聲笑我沒有得到關系,但無論如何下面的幾行工作......感謝大家的幫助!

....
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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM