Running commands with Paramiko

Now we are connected to the remote host with Paramiko, we can then run commands on the remote host using this connection. To execute command, we can simply call the connect() method along with the target hostname and the SSH login credentials. To run any command on the target host, we need to invoke the exec_command() method by passing the command as its argument:

ssh_client.connect(hostname, port, username, password)stdin, stdout, stderr = ssh_client.exec_command(cmd)for line in stdout.readlines():    print(line.strip())ssh.close()

The following code listing shows how to do an SSH login to a target host and then run an ifconfig command. The next script will make an SSH connection to the localhost and then run ...

Get Mastering Python for Networking and Security now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.