Executing a command with paramiko

    I wanted to provide a simple example of how to execute a command with paramiko as well. This is quite similar to the scp example, but is nicer than executing a command in a shell because there isn't any requirement to do parsing to determine when the command has finished executing.

      #!/usr/bin/python
      
      # A simple command example for Paramiko.
      # Args:
      #   1: hostname
      #   2: username
      #   3: command to run
      
      import getpass
      import os
      import paramiko
      import socket
      import sys
      
      # Socket connection to remote host
      sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      sock.connect((sys.argv[1], 22))
      
      # Build a SSH transport
      t = paramiko.Transport(sock)
      t.start_client()
      t.auth_password(sys.argv[2], getpass.getpass('Password: '))
      
      # Start a cmd channel
      cmd_channel = t.open_session()
      cmd_channel.exec_command(sys.argv[3])
      
      data = cmd_channel.recv(1024)
      while data:
        sys.stdout.write(data)
        data = cmd_channel.recv(1024)
      
      # Cleanup
      cmd_channel.close()
      t.close()
      sock.close()
      


    Tags for this post: python paramiko exec
    Related posts: Weird paramiko problem; Calculating a SSH host key with paramiko; Multiple file support with scp; Implementing SCP with paramiko; paramiko exec_command timeout; Finding locking deadlocks in python

posted at: 15:11 | path: /python/paramiko | permanent link to this entry