Assistenten von Stackoverflow. Ich versuche, Befehle aus einer TXT-Datei in SSH mit Paramiko einzulesen. Momentan liest es in der ersten Zeile der Datei und führt sie aus. Wenn ich jedoch zur zweiten Zeile komme, wird ein EOFError geworfen und beendet. Ich checke ein, um zu sehen, ob der zweite Befehl es zum Drucken bringt, und es wird jedoch nicht ausgeführt. Ich hatte gehofft, dass jemand in der Lage wäre zu helfen und dieses Problem mit mir zu lösen. Hier ist mein Code:SSH-Programmierung mit Python 3 paramiko, EOFError beim Versuch, Befehle auszuführen
from paramiko import client
import logging
import os
#Clear the screen before starting the script
os.system('cls')
# Prompting for the user input config file
filename = input('Input configuration filename, the file extension must be attached: ')
# Creating the LOG file for the execution of the config file
logFileName = "LOG" + filename[0:]
try:
logging.basicConfig(filename= logFileName ,format='%(asctime)s %(message)s', level= logging.DEBUG)
print ("The file was created!")
except IOError:
print ("File failed to create")
logging.info("---------------------------------------------------------------------------")
logging.info("NEW CONFIGURATION LOG ENTRY")
logging.info("---------------------------------------------------------------------------")
# Class for creating an SSH client, logging in, letting us write out commands, and close the client.
class ssh:
client = None
def __init__(self, address, username, password):
print ("Connecting to server...")
logging.info('Connecting to server...')
self.client = client.SSHClient()
self.client.set_missing_host_key_policy(client.AutoAddPolicy())
self.client.connect(address, username= username, password= password, look_for_keys= False)
logging.info("Address: " + address)
logging.info("Username: " + username)
print ("Connection successful!")
logging.info('Connection successful!')
def sendCommand(self, command):
if (self.client):
stdin, stdout, stderr = self.client.exec_command(command)
receiveData = b""
while not stdout.channel.exit_status_ready():
receiveData += stdout.channel.recv(1024)
if stdout.channel.recv_ready():
received = stdout.channel.recv(1024)
while received:
receiveData += received
received = stdout.channel.recv(1024)
if receiveData:
print (str(receiveData, "utf8"))
else:
print ("stdout is empty")
else:
print ("Connection failed, check credentials and try again..")
logging.warning('Connection failed, check credentials and try again..')
connection = ssh('0.0.0.0', 'test', 'test')
with open(filename) as f:
for line in f:
print(line)
connection.sendCommand(line)
Die TXT-Datei in etwa so lesen würde:
configure terminal
Schnittstelle Gi0/9
Beschreibung Test_Interface
Beliebig Hilfe wird sehr geschätzt, danke.
Aktuelle Implementierung von 'sendCommand' einige Male nicht Serverausgabe erhalten. Ist es dasselbe wie im ursprünglichen Code oder in der vereinfachten Version? – Arnial
@Arnial das gleiche wie die ursprüngliche Version – Aaron