Basic knowledge of Bastion hosts-paramiko module and basic knowledge of Bastion hosts-paramiko
1. paramiko Module
The paramiko module of Python is used to connect to a remote server and perform related operations based on SSH.
1.1 install the paramiko module on windows
Test environment: win10, python3.5, and pip installed
In the command window, switch to Scripts in the python installation directory.
Run pip install paramiko.
Check: import paramiko
Ii. SSHClient 2.1 connection based on user name and password
Import paramiko ''' is used to connect to the remote server and run the command ''' # create an SSH object ssh = paramiko. SSHClient () # Allow connection to host ssh that is not in the know_hosts file. set_missing_host_key_policy (paramiko. autoAddPolicy () # connect to the server ssh. connect (hostname = '2017. 16.200.30 ', port = 22, username = 'root', password = '000000') # Run the stdin, stdout, stderr = ssh.exe c_command ('ls & df') command ') # obtain command result # Trielement operation res, err = stdout. read (), stderr. read () result = res if res else errprint (result. decode () # Close the ssh connection. close ()
Output
2.2 Public Key-based connection
In fact, there is no difference in using a password to log on, but there is no need to write a plaintext password.
Import paramikoprivate_key = paramiko. RSAKey. from_private_key_file (r 'G: \ python \ untitled \ study9 \ ceshi') # create an SSH object ssh = paramiko. SSHClient () # Allow connection to host ssh that is not in the know_hosts file. set_missing_host_key_policy (paramiko. autoAddPolicy () # connect to the server ssh. connect (hostname = '2017. 16.200.30 ', port = 22, username = 'root', pkey = private_key) # Run stdin, stdout, stderr = ssh.exe c_command ('LS ') # obtain command result # Trielement operation res, err = stdout. read (), stderr. read () result = res if res else errprint (result. decode () # Close the ssh connection. close ()
Output
0047761_1748anaconda-ks.cfgmonitornpm-debug.log
2.3 SSHClietn encapsulates transport
#-*-Coding: UTF-8-*-# SSHClient encapsulates Transportimport paramikotransport = paramiko. transport ('2017. 16.200.30 ', 22) transport. connect (username = 'root', password = '000000') ssh = paramiko. SSHClient () ssh. _ transport = transport # Run stdin, stdout, stderr = ssh.exe c_command ('df ') res, err = stdout. read (), stderr. read () result = res if res else errprint (result. decode () transport. close ()
Key-based
# SSHClient encapsulates Transportimport paramikoprivate_key = paramiko. RSAKey. from_private_key_file (r 'G: \ python \ untitled \ study9 \ ceshi') transport = paramiko. transport ('2017. 16.200.30 ', 22) transport. connect (username = 'root', pkey = private_key) ssh = paramiko. SSHClient () ssh. _ transport = transport # Run stdin, stdout, stderr = ssh.exe c_command ('ds ') res, err = stdout. read (), stderr. read () result = res if res else errprint (result. decode () transport. close ()
Iii. SFTPClient
Used to connect to the remote server and perform upload and download
3.1 based on user name and password
Import paramikotransport = paramiko. transport ('2017. 16.200.30 ', 22) transport. connect (username = 'root', password = '000000') sftp = paramiko. SFTPClient. from_transport (transport) # upload a local file to the sftp server. put (r 'G: \ python \ untitled \ study9 \ ceshi', '/tmp/ceshi') # download the remote server file to the local sftp. get ('/tmp/test.txt', r 'G: \ python \ untitled \ study9 \ test_linux.txt ') transport. close () # Note: the upload and download paths must be full paths, that is, the file name must be added, not just the directory name.
3.2 uploading and downloading based on public and private keys
Few changes
Import paramiko # Private Key private_key = paramiko. RSAKey. from_private_key_file (r 'G: \ python \ untitled \ study9 \ ceshi') transport = paramiko. transport ('2017. 16.200.30 ', 22) transport. connect (username = 'root', pkey = private_key) sftp = paramiko. SFTPClient. from_transport (transport) # upload a local file to the sftp server. put (r 'G: \ python \ untitled \ study9 \ ceshi', '/tmp/ceshi') # download the remote server file to the local sftp. get ('/tmp/test.txt', r 'G: \ python \ untitled \ study9 \ test_linux.txt ') transport. close ()