Learning Python Network Programming

(Sean Pound) #1

Interacting with Remote Systems


HOSTNAME = 'localhost'
PORT = 22

def run_ssh_cmd(username, password, cmd, hostname=HOSTNAME,
port=PORT):
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(\
paramiko.AutoAddPolicy())
ssh_client.load_system_host_keys()
ssh_client.connect(hostname, port, username, password)
stdin, stdout, stderr = ssh_client.exec_command(cmd)
print(stdout.read())

if __name__ == '__main__':
username = input("Enter username: ")
password = getpass.getpass(prompt="Enter password: ")
cmd = 'ls -l /dev'
run_ssh_cmd(username, password, cmd)

Before running it, we need to ensure that the SSH server daemon is running on the
target host (which in this case is the localhost). As shown in the following screenshot,
we can use the netstat command for doing that. This command will show all the
running services that are listening to a particular port:

Free download pdf