0

请帮忙,在 Paramiko 上执行脚本时出现问题,显示来自 switch.Aruba 2530 的信息时,OS 配置

脚本:

import paramiko
import getpass
import time
import json

with open('devices.json', 'r') as f:
devices = json.load(f)
with open ('commands.txt', 'r') as f:
commands = [line for line in f.readlines()]

username = input('Username: ')
password = getpass.getpass('Password: ')
max_buffer = 65535

def clear_buffer(connection):
if connection.recv_ready():
    return connection.recv(max_buffer)

# Starts the loop for devices
for device in devices.keys(): 
outputFileName = device + '_output.txt'
connection = paramiko.SSHClient()
connection.set_missing_host_key_policy(paramiko.AutoAddPolicy())
connection.connect(devices[device]['ip'], username=username, password=password, look_for_keys=False, allow_agent=False)
new_connection = connection.invoke_shell()
output = clear_buffer(new_connection)
time.sleep(3)
#new_connection.send("terminal length 5\n")
output = clear_buffer(new_connection)
with open(outputFileName, 'wb') as f:
    for command in commands:
        new_connection.send(command)
        time.sleep(2)
        output = new_connection.recv(max_buffer)
        print(output)
        f.write(output)
print ("\r\nEXIT"*15)        
new_connection.close()

执行后产生以下内容: 无法理解的事情

在 output.txt 我有:

输出.txt

在 Comware 中一切正常。我基本了解 python,但我不明白这意味着什么。

4

1 回答 1

0
import paramiko
import getpass
import time
import json
import re

with open('devices.json', 'r') as f:
    devices = json.load(f)
with open ('commands.txt', 'r') as f:
    commands = [line for line in f.readlines()]

username = input('Username: ')
password = getpass.getpass('Password: ')
max_buffer = 65535

def escape_ansi(line):
    ansi_escape = re.compile(r'(?:\x1B[@-_]|[\x80-\x9F])[0-?]*[ -/]*[@-~]')
    return ansi_escape.sub('', line)

def clear_buffer(connection):
    if connection.recv_ready():
        return connection.recv(max_buffer)

# Starts the loop for devices
for device in devices.keys(): 
    outputFileName = device + '_output.txt'
    connection = paramiko.SSHClient()
    connection.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    connection.connect(devices[device]['ip'], username=username, password=password, look_for_keys=False, allow_agent=False)
    new_connection = connection.invoke_shell()
    output = clear_buffer(new_connection)
    time.sleep(3)
    #new_connection.send("terminal length 5\n")
    output = clear_buffer(new_connection).decode('utf-8') # #first decode,then kill ANSI-symbols,for output
    a=escape_ansi(output)
    print (a)
    with open(outputFileName, 'wb') as f:
        for command in commands:
            new_connection.send(command)
            time.sleep(2)
            output = new_connection.recv(max_buffer)
            b=output.decode('utf-8') #first decode,then kill ANSI-symbols,for output to terminal
            b=escape_ansi(b)
            print(b)
            c = b.encode('utf-8') #encode from str to bytes,for write to file.
            f.write(c)
    print ("\r\nEXIT"*15)        
    new_connection.close()
于 2020-06-10T09:47:26.120 回答