Let’s get on with the code because nothing is more interesting than the code itself.

Before, you run the code install the Paramiko library.

Command to install: pip install paramiko

Command to upgrade it: pip install –upgrade paramiko

Explanation for the code below:

param_ssh(addr,user,passwd): is the function I have created that takes hostname of the target machine with the username and password.

client = paramiko.SSHClient() : With this code we are creating an object for paramiko.SSHClient(). This object will handle all our requests and commands. You can name it anything.

client.set_missing_host_key_policy(paramiko.AutoAddPolicy()): This line handles the key policies, Add keys to the local hostkey lists. This line must be specified because key handling is important while making a connection.

sock=socket.socket()

ip=socket.gethostbyname(address) :These two lines make a connection with the host to get the IP address. I added this line because I felt there is a need to add it if you enter a domain name and not a proper IP address. Must be enclosed in try and Except Block.

client.connect(ip, username=username, password=password, look_for_keys=False): This line will then make a SSH connection using ip, username, password and look_for_keys=False will set the search of private keys in the local machine to false. This ways it will make connection using password only.

print(“Connected Successfully with “+username+”:”+password)

stdin,stdout,stderr=client.exec_command(‘whoami’)

outlines=stdout.readlines()

resp=”.join(outlines) print(resp)

The above lines of code handles the std input and output when the connection has been successfully made. You can use interactive command so as to communicate with the server.