简体   繁体   English

使用paramiko的Python交互式ssh客户端

[英]Python interactive ssh client using paramiko

I'm not a programmer, but would like to use Python for automation of some Administrative purposes. 我不是程序员,但想将Python用于某些管理目的的自动化。 The first application after "Hello world" I tried to create is interactive ssh client. 我尝试创建的“ Hello world”之后的第一个应用程序是交互式ssh客户端。 I've read some documentation and articles and decided it would be the easiest way to use paramiko module, but unfortunately I'm facing a problem: My applications asks you to enter some neccessary information such as server ip, username, password. 我已经阅读了一些文档和文章,并确定这将是使用paramiko模块的最简单方法,但是不幸的是,我遇到了一个问题:我的应用程序要求您输入一些必要的信息,例如服务器ip,用户名,密码。 After this it establishes connection with defined server and provide you with cli on your screen. 此后,它将与定义的服务器建立连接,并在屏幕上为您提供cli。 For emulating the process of entering command I use while loop. 为了模拟输入命令的过程,我使用了while循环。 Unfortunately my application works well only with the first command you enter. 不幸的是,我的应用程序只能与您输入的第一个命令一起很好地工作。 While trying to type the second command an error appears: 尝试键入第二个命令时,出现错误:

Traceback (most recent call last):
  File "C:\Python27\Tests\ssh_client.py", line 53, in <module>
    client.execute_command(command)
  File "C:\Python27\Tests\ssh_client.py", line 26, in execute_command
    stdin,stdout,stderr = self.connection.exec_command(command)
  File "C:\Python27\lib\site-packages\paramiko\client.py", line 343, in exec_command
    chan.exec_command(command)
AttributeError: 'NoneType' object has no attribute 'exec_command'

Code of the programm (Windows 7): 程序代码(Windows 7):

import paramiko

SERVER = raw_input('Please enter an ip address of remote host: ')
USER = raw_input('Please enter your username: ')
PASSWORD = raw_input('Please enter your password: ')

class MYSSHClient():

    def __init__(self, server=SERVER, username=USER, password=PASSWORD):
        self.server = server
        self.username = username
        self.password = password
        self.connection = None
        self.result =  ''
        self.is_error = False


    def do_connect(self):
        self.connection = paramiko.SSHClient()
        self.connection.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.connection.connect(self.server, username=self.username, password=self.password)

    def execute_command(self, command):
        if command:
            print command            
            stdin,stdout,stderr = self.connection.exec_command(command)
            stdin.close()
            error = str(stderr.read())
            if error:
                self.is_error = True
                self.result = error
                print 'error'
            else:
                self.is_error = False
                self.result = str(stdout.read())
                print 'no error'

            print self.result


        else:
            print "no command was entered"

    def do_close(self):
        self.connection.close()

if __name__ == '__main__':
    client = MYSSHClient()
    client.do_connect()
    while 1:
        command = raw_input('cli: ')
        if command == 'q': break
        client.execute_command(command)
    client.do_close()

I tried to delete while loop and just call commands one by one right in the code, but have the same problem (when typing the second command see the same error). 我试图删除while循环,只是在代码中一个接一个地调用命令,但是存在相同的问题(键入第二个命令时会看到相同的错误)。 It looks like I don't understand fully how paramiko module works. 看来我不太了解paramiko模块的工作原理。 I tried to find information on web but unfortunately didn't find any solution. 我试图在网上查找信息,但不幸的是没有找到任何解决方案。

I'd be very appreciated if somebody could tell me what I do wrong or give me a link on the similar issue where I can find a solution. 如果有人可以告诉我我做错了什么,或者给我有关类似问题的链接,我可以找到解决办法,我将不胜感激。

Thanks in advance for any help. 在此先感谢您的帮助。

Please use for pxssh module this is very useful for your application if work for windows Python: How can remote from my local pc to remoteA to remoteb to remote c using Paramiko this example very helpful for you Python - Pxssh - Getting an password refused error when trying to login to a remote server 请为pxssh模块使用,如果适用于Windows,则对您的应用程序非常有用Python:如何使用Paramiko从我的本地PC到remoteA到remoteb到远程c远程访问本示例对您非常有用Python-Pxssh-出现密码拒绝错误尝试登录到远程服务器

i think u check for your server settings in remote host machine 我认为您检查远程主机中的服务器设置

Not a real answer to your problem but more of a suggestion. 不是您问题的真正答案,而是更多建议。

I would recommend you to have a look at fabric, which does exactly what you want: Automate tasks on local or remote hosts. 我建议您看一下结构,它可以完全满足您的要求:在本地或远程主机上自动执行任务。 Might be a bit easier since you don't have to implement the logic for the connection and execution of commands. 可能会容易一些,因为您不必实现用于连接和执行命令的逻辑。

Fabric documentation: http://docs.fabfile.org/en/1.6/ 面料文档: http : //docs.fabfile.org/en/1.6/

Unfortunately I didn't find the way to resolve my issue using paramiko module, but I've found such a module as Exscript. 不幸的是,我没有找到使用paramiko模块解决问题的方法,但是我找到了Exscript这样的模块。 The simple code is below: 简单的代码如下:

from Exscript.util.interact import read_login
from Exscript.protocols import SSH2


account = read_login()
conn = SSH2()                       
conn.connect('192.168.1.1')     
conn.login(account)  


while True:
    command = raw_input('cli: ')
    if command == 'q': break
    conn.execute(command)
    print conn.response



conn.send('quit\r')               
conn.close() 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM