Wizards of stackoverflow. paramiko를 사용하여 .txt 파일에서 SSH로 명령을 읽으려고합니다. 현재는 파일의 첫 번째 행에서 읽고 실행합니다. 두 번째 줄로 가면 EOFError가 발생하고 종료됩니다. 두 번째 명령이 인쇄를 수행하는지 확인하기 위해 체크인을했지만 실행하지는 않습니다. 나는 누군가가 나를 도와주고이 문제를 해결할 수 있기를 희망했다.Python 3의 paramiko로 SSH 프로그래밍, 명령을 실행하려고 할 때 EOFError
구성 터미널
인터페이스 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