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 the ifconfig command that allows us to see the configuration of the network for the machine to which we are connecting.

With this script we could create an interactive shell that could automate many tasks. We create a function called ssh_command, which makes a connection to an SSH server and runs a single command.

To execute the command we use the exec_command() method of the ssh_session object that we have obtained from the open session when logging in to the server.

You can find the following code in the filename: SSH_command.py:

#!/usr/bin/env python3
import getpass
import paramiko

HOSTNAME = 'localhost'
PORT = 22

def run_ssh_command(username, password, command, 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)
ssh_session = client.get_transport().open_session()
if ssh_session.active:
stdin, stdout, stderr = ssh_client.exec_command(command)
print(stdout.read())
return

if __name__ == '__main__':
username = input("Enter username: ")
password = getpass.getpass(prompt="Enter password: ")
command= 'ifconfig'
run_ssh_command(username, password, command)

In this example, we perform the same functionality as in the previous script, but in this case we use the Transport class to establish the connection with the SSH server. To be able to execute commands we have to open a session previously on the transport object.

You can find the following code in the filename: SSH_transport.py:

import paramiko

def ssh_command(ip, user, passwd, command):
transport = paramiko.Transport(ip)
try:
transport.start_client()
except Exception as e:
print(e)

try:
transport.auth_password(username=user,password=passwd)
except Exception as e:
print(e)

if transport.is_authenticated():
print(transport.getpeername())
channel = transport.opem_session()
channel.exec_command(command)
response = channel.recv(1024)
print('Command %r(%r)-->%s' % (command,user,response))

if __name__ == '__main__':
username = input("Enter username: ")
password = getpass.getpass(prompt="Enter password: ")
command= 'ifconfig'
run_ssh_command('localhost',username, password, command)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.142.2