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.

      # 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.auth_password(sys.argv[2], getpass.getpass('Password: '))
      # Start a cmd channel
      cmd_channel = t.open_session()
      data = cmd_channel.recv(1024)
      while data:
        data = cmd_channel.recv(1024)
      # Cleanup

    Tags for this post: python paramiko exec
    Related posts: paramiko exec_command timeout; Multiple file support with scp; Implementing SCP with paramiko; Calculating a SSH host key with paramiko; Weird paramiko problem; Python effective TLD library bug fix

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