2016-02-12 218 views
1

我的代碼下面的Linux服務器正常工作,但腳本不適用於安裝了OpenSSH的Windows 2008服務器。我也測試過ssh與teraterm相同的用戶名,密碼,ipaddress和port22它工作得很好。Paramiko ssh與ConnectionResetError窗口:[WinError 10054]

我收到下面的錯誤。

C:\Users\>python auto-ssh_v1.py 
    Socket exception: An existing connection was forcibly closed by the remote host (10054) 
    Traceback (most recent call last): 
    File "auto-ssh_v1.py", line 71, in <module> 
    results = executer.execute() 
    File "auto-ssh_v1.py", line 53, in execute 
    stdin, stdout, stderr = ssh.exec_command(self.command) 
    File "C:\Anaconda3\lib\site-packages\paramiko\client.py", line 405, in exec_command 
    chan.exec_command(command) 
    File "C:\Anaconda3\lib\site-packages\paramiko\channel.py", line 60, in _check 
    return func(self, *args, **kwds) 
    File "C:\Anaconda3\lib\site-packages\paramiko\channel.py", line 229, in exec_command 
    self._wait_for_event() 
    File "C:\Anaconda3\lib\site-packages\paramiko\channel.py", line 1086, in _wait_for_event 
    raise e 
    File "C:\Anaconda3\lib\site-packages\paramiko\transport.py", line 1726, in run 
    ptype, m = self.packetizer.read_message() 
    File "C:\U\Anaconda3\lib\site-packages\paramiko\packet.py", line 386, in read_message 
    header = self.read_all(self.__block_size_in, check_rekey=True) 
    File "C:\Anaconda3\lib\site-packages\paramiko\packet.py", line 249, in read_all 
    x = self.__socket.recv(n) 
    ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host 

的代碼是

#Modules 
    import paramiko 

    #Variables 
    USER = 'Administrator' 
    PSWD = 'Passw0rd' 

    #Classes and Functions 
    class InputReader: 
     def __init__(self, commands_path, hosts_path): 
      self.commands_path = commands_path 
      self.hosts_path = hosts_path 

     def read(self): 
      self.commands = self.__readlines(self.commands_path) 
      self.hosts = self.__readlines(self.hosts_path) 

     def __readlines(self, path): 
      with open(path) as f: 
      return [v.strip() for v in f.readlines()] #List comprehension 

    class CommandExecuter: 
     def __init__(self, host, command): 
      self.host = host 
      self.command = command 

     def execute(self): 
      ssh = paramiko.SSHClient() 
      ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
      ssh.connect(self.host, username=USER, password=PSWD) 

      stdin, stdout, stderr = ssh.exec_command(self.command) 

      errors = stderr.readlines() 
      if len(errors) != 0: 
       raise Exception(errors) 

      lines = [v.strip() for v in stdout.readlines()] 
      ssh.close() 
      return lines 

    #Main Procedure 
    if __name__ == '__main__': 
     reader = InputReader("commands.txt", "systems.txt") 
     reader.read() 

     for h in reader.hosts: 
      for c in reader.commands: 
       executer = CommandExecuter(h, c) 
       results = executer.execute() 

       print("{0}({1}):".format(h, c)) 
       for i in results: 
        print(i) 
        print('\n') 

回答

0

您可以嘗試線運行的paramiko線ssh到您的Windows Server和查看跟蹤。

希望這將有助於,

鄭氏

+0

鄭氏,非常感謝您的評論!我將分享按行^^運行代碼的結果 – mh0189