Handling passwords at runtime in scripts

In this section, we will look at a simple example for handling passwords in script. We will create a script called handling_password.py and write the following content in it:

import sys
import paramiko
import time

ip_address = "192.168.2.106"
username = "student"
password = "training"
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.load_system_host_keys()
ssh_client.connect(hostname=ip_address,
username=username, password=password)
print ("Successful connection", ip_address)
ssh_client.invoke_shell()
remote_connection = ssh_client.exec_command('cd Desktop; mkdir work ')
remote_connection = ssh_client.exec_command('mkdir test_folder ')
#print( remote_connection.read() )
ssh_client.close

Run the preceding script and you will receive the following output:

$ python3 handling_password.py

Output:
Successful connection 192.168.2.106

In the preceding script, we used the paramiko module. The paramiko module is a Python implementation of ssh that provides client-server functionality.

Install paramiko as follows:

pip3 install paramiko

In the preceding script, we are remotely connecting to the host, 192.168.2.106. We have provided the host's username and password in our script.

After running this script, on the 192.168.2.106 desktop, you will find a work folder and test_folder can be found in the home/ directory of 192.168.2.106.

..................Content has been hidden....................

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