#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    13
    Rep Power
    0

    Paramiko SSH and issuing multiple commands??


    Anyone have any luck issuing multiple commands utilizing paramiko? Here's some of my code. Can't I just put this code in a function and send the command as an argument anytime I want?

    Code:
    import paramiko
    
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    
    objHost = 'xx'
    objUser = 'xx'
    objPass = 'xx'
    
    client.connect(objHost, port=22, username=objUser, password=objPass)
    
    stdin,stdout,stderr = client.exec_command("df -h")
    sshdata = stdout.readlines()
    
    for line in sshdata:
        print(line)
    
    client.close()
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,893
    Rep Power
    481
    would this work?
    Code:
    import atexit
    import paramiko
    
    class myssh:
    
        def __init__(self, h, u, p):
            client = paramiko.SSHClient()
            client.load_system_host_keys()
            client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            client.connect(objHost, port=22, username=objUser, password=objPass)
            atexit.atexit.register(client.close)
            self.client = client
    
        def __call__(self, command):
            stdin,stdout,stderr = self.client.exec_command(command)
            sshdata = stdout.readlines()
            for line in sshdata:
                print(line)
    
    remote = myssh('xx','xx','xx')
    remote('df -h')
    remote('ls')
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    13
    Rep Power
    0
    Code:
    import atexit
    atexit.atexit.register(client.close)
    AttributeError: 'module' object has no attribute 'atexit'

    Code:
    stdin,stdout,sterr = self.client.exec_command(command)
    Unused variable stdin and stderr...
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,893
    Rep Power
    481

    I'll try a little harder


    Code:
    import atexit
    import paramiko
    
    class myssh:
    
        def __init__(self, host, user, password, port = 22):
            client = paramiko.SSHClient()
            client.load_system_host_keys()
            client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            client.connect(host, port=port, username=user, password=password)
            atexit.register(client.close)
            self.client = client
    
        def __call__(self, command):
            stdin,stdout,stderr = self.client.exec_command(command)
            sshdata = stdout.readlines()
            for line in sshdata:
                print(line)
    
    remote = myssh('localhost','me','mypassword')
    remote('df -h')
    remote('ls')
    Still untested.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    13
    Rep Power
    0
    Hey I just want to let you know this works perfectly. I'm jumping back and forth between a number of tasks and just got around to testing this.

    After reading the atexit documentation on python.org I was expecting the commands to go reverse order. Since this is set up in a function and being called seperately they are going in order in which I call the function.

    Again sir you are awesome!

IMN logo majestic logo threadwatch logo seochat tools logo