23 Praramiko module

 '''

1. Praramiko module is used to do operations on remote machine.

2. used to run commands on remote host and copy file from local to remote and remote to local.

1. To install module ==> python -m pip install paramiko

'''

import paramiko


h_name = '10.8.2.5'

u_name ='siellamp'

p_word = '8Aug2019'

port_no = 22


# To create ssh object

ssh = paramiko.SSHClient()


# To avoid popups 

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())


# To create a connection on remote machine

ssh.connect(h_name, username=u_name, password=p_word, port=port_no)


# To run commnd on remote host 

stdin, stdout, stderr = ssh.exec_command('mkok /home/smellamp/sriram/RAMESH')


# To get command output

print(' \n stdout is ::', stdout.readlines())


# To get error message if any failures 

print(' \n stderr is ::', stderr.read())

ssh.close()



import paramiko

host_name = '10.1.2.35'
u_name ='admin'
p_word = '8Aug2019'
port_no = 22

try:
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(host_name, username=u_name, password=p_word, port=port_no)
except paramiko.AuthenticationException:
    print("Authentication failed, please verify your credentials")


## Create FTP object to copy file from local to remote and remote to local
ftp = ssh.open_sftp()

## Copy file from remote machine to local machine
#rsrc = '/home/smellamp/sriram/TODAY.py'
#ldst = r'C:\Users\smellamp\Desktop\GIT\Today_copy.txt'
#ftp.get(rsrc, ldst)

## Copy file from local machine to remote machine
lsrc = r'C:\Users\smellamp\Desktop\TODAY_GIT\SRIKUMAR.TXT'
rdst = '/home/smellamp/sriram/srikumar.py'
ftp.put(lsrc, rdst)

# close ftp and ssh connections
ftp.close()
ssh.close()

Comments

Popular posts from this blog

1 PYTHON PROGRAMMING

16 file handling in python

4 Tuple data types