SSH(paramiko module implementation)

The process of SSH authentication is encrypted, but the communication after authentication is clear.
Certification process:

Host A sends A piece of ciphertext to host B with public key encryption, and host B decrypts the ciphertext through private key and sends it to host A for verification, and the authentication is successful after success.

Steps:

1: A host executes the command SSH keygen to generate the private key and public key
 2: Host A executes the command ssh-copy-id root @ host B IP to send the private key to host B

Log in to the remote host by password:

#!/bin/env python
#!coding=utf-8
import paramiko
import sys,os

#host = "192.168.137.101"   ##Host IP can be set actively 
user='root'
password='westos'
#cmd= 'ls /mnt'             ##Command executed after logging in the host
host=sys.argv[1]            ##Set the host as a parameter, similar to the shell's $1
cmd=sys.argv[2]             ##Set the command to be executed as a parameter, similar to the shell's $2

s=paramiko.SSHClient()      ##Binding instance
s.load_system_host_keys()   ##Load native HOST file
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())                         ##The function of this sentence is to solve the problem of "yes" in the first connection

s.connect(host,22,user,password,timeout=5)  ##Link up
stdin,stdout,stderr = s.exec_command(cmd)   ##Execute the command of 'cmd', assign the correct result to stdout and the wrong output to stderr

cmd_result = stdout.read(),stderr.read() ##Make the assigned value the result

for line in cmd_result:
    print line
                            ##Output result
s.close()

Connect via key (public key, private key)

#!/bin/env python
#!coding=utf-8
import paramiko
import sys,os
pkey_file='/root/.ssh/id_rsa'       ##Set private key file
key=paramiko.RSAKey.from_private_key_file(pkey_file)    ##Bind private key
host=sys.argv[1]            ##Set host IP to connect
user='root'
cmd=sys.argv[2]             ##Set command to execute
s=paramiko.SSHClient()      ##Binding instance
s.load_system_host_keys()   ##Load local HOST file
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())                         ##Solve the problem of "YES" when logging in for the first time

s.connect(host,22,user,pkey=key,timeout=5)  ##Connect
stdin,stdout,stderr = s.exec_command(cmd)   

cmd_result = stdout.read(),stderr.read()

for line in cmd_result:
    print line

s.close()

Implementation of ftp:

#!/bin/env python
#!coding=utf-8
import paramiko
import sys,os
pkey_file='/root/.ssh/id_rsa'
key=paramiko.RSAKey.from_private_key_file(pkey_file)
host=sys.argv[1]
user='root'

t=paramiko.Transport((host,22))     ##Set objects to connect
t.connect(username=user,pkey=key)   ##Set login user
sftp=paramiko.SFTPClient.from_transport(t)  ##Open ftp

sftp.get('/mnt/haha','/mnt/ha')     ##Download the opposite host / mnt/ha ha to the local host / mnt/ha
sftp.put('1.py','/mnt/1.py')        ##Upload local file 1.py to the opposite host / mnt/1.py
t.close()

Tags: ssh Python sftp shell

Posted on Wed, 01 Apr 2020 04:54:03 -0400 by mfindlay