Wizards of stackoverflow。私はparamikoを使って.txtファイルからSSHにコマンドを読み込もうとしています。現在、ファイルから最初の行を読み込んで実行します。私が2番目の行に到達すると、EOFErrorがスローされ、終了します。 2番目のコマンドで印刷するかどうかを確認するためにチェックインを行いますが、実行されません。誰かがこの問題を私に助けて解決できるようになることを願っていました。Python 3でのSSHプログラミングparamiko、コマンド実行時のEOFError
にconfigure terminal
インタフェースはGi0/9
説明Test_Interface
どれ:
は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)
.txtファイルには、このようなものを読んでいました。ここに私のコードですお手伝いが大変ありがとうございます。
'sendCommand'の現在の実装は、サーバ出力を受信しないことがあります。元のコードや単純化されたバージョンと同じですか? – Arnial
@Arnial元のバージョンと同じ – Aaron