python2.0 S12 Day8 _ Fortress Machine Foreplay Paramiko module

Source: Internet
Author: User
Tags stdin

Fortress Machine Foreplay
Before developing a bastion machine, learn the Python Paramiko module, which is an opportunity for SSH to connect to a remote server and perform related operations
The Paramiko module is host-managed, and he simulates an ssh.
There are two forms of connection form,
A password by user name: ssh-p [email protected]
One is through the key way: ssh-p 22-i ~/.ssh/dongjing-shanghai.pem [email protected]_pro01
There are two ways to re-connect the syntax:
One is to directly pass in the connected host address and port directly
One is to first host address, port, user name, password encapsulated to Paramiko. In the Transport () instance

Sshclient
The instance code connected by the user name password is as follows:
Call the Ssh.connect () method directly
1                     #!/usr/bin/env python3.52                     #__author__: ' Ted.zhou '3                     " "4 Use the Paramiko module to implement sshclient connection to a remote server5 First Use the user name, password method6                     " "7                     ImportParamiko8 9SSH = Paramiko. Sshclient ()#To create an SSH objectTen  OneSsh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())#The key is to allow connections to hosts that are not in the Know_hosts file ASsh.connect (hostname='127.0.0.1', port=7272,username='Root', password='123')#connecting to a server -Stdin,stdout,stderr = Ssh.exec_command ('DF')#Execute Command -result = Stdout.read ()#Get command Results theSsh.close ()#Close Connection -  -                     Print(Result.decode ())#Print Results

Through Paramiko. Transport () Package:
1                     #!/usr/bin/env python3.52                     #__author__: ' Ted.zhou '3                     " "4 Paramiko module sshclient connect to remote server via username and password5 using the syntax Transport () encapsulation6                     " "7 8                     ImportParamiko9 TenTransport = Paramiko. Transport (('127.0.0.1', 7272))#A tuple of incoming host IP or hostname and port combinations, remember that it must be a tuple OneTransport.connect (username='Root', password='123') A  -SSH =Paramiko. Sshclient () -Ssh._transport =Transport the  -Stdin,stdout,stderr = Ssh.exec_command ('DF') -result =Stdout.read () -  +                     Print(Result.decode ()) -Transport.close ()



Instances connected by Sshkey mode:
Python2.x is different from python3.x.
The python2.x code is as follows:
Call the Ssh.connect () method directly#!/usr/bin/env python3.5                    #__author__: ' Ted.zhou '                    " "use Paramiko. Sshclient () Sshkey way to connect to a remote server" "                    ImportParamiko#Load Key FilePrivate_key = Paramiko. Rsakey.from_private_key_file ('/users/tedzhou/.ssh/id_rsa')                    #print (Type (private_key))SSH = Paramiko. Sshclient ()#To create an SSH object                    #Ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())#allow connections to hosts that are not in the Know_hosts file                    #start to connect server, here note again python2.x in so write, 3.x error, should be written key_file = '/users/tedzhou/.ssh/id_rsa 'Ssh.connect (hostname='127.0.0.1', port=7272,username='Root', key=Private_key)#Gets the result of the remote host execution commandStdin,stdout,stderr = Ssh.exec_command ('ifconfig') Result=Stdout.read ()Print(Result.decode ()) Ssh.close ()
                Python3.x is written in the following notation:
1                     #!/usr/bin/env python3.52                     #__author__: ' Ted.zhou '3                     " "4 use Paramiko. Sshclient () Sshkey way to connect to a remote server5 6                     " "7                     ImportParamiko8 9                     #There is no need to load the private key file in the python3.x.Ten                     #Private_key = Paramiko. Rsakey.from_private_key_file ('/users/tedzhou/.ssh/id_rsa ') OneSSH = Paramiko. Sshclient ()#To create an SSH object ASsh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())#allow connections to hosts that are not in the Know_hosts file -                     #start connecting to the server -Ssh.connect (hostname='kiri_pro01', Port=22, Username='Root', key_filename='/users/tedzhou/.ssh/dongjing-shanghai.pem') the                     #Gets the result of the remote host execution command -Stdin,stdout,stderr = Ssh.exec_command ('ifconfig') -result =Stdout.read () -                     Print(Result.decode ()) +Ssh.close ()
                Through Paramiko. Transport () Package:
Note: Using transport to connect to a remote server, python2.x and python3.x are the same, the code is as follows:
1 #!/usr/bin/env python3.52 3                     ImportParamiko4Private_key = Paramiko. Rsakey.from_private_key_file ('/users/tedzhou/.ssh/dongjing-shanghai.pem')5                     #'/users/tedzhou/.ssh/dongjing-shanghai.pem '6                     #Package Transport7Transport = Paramiko. Transport (('127.0.0.1', 22))#This must be a tuple .8Transport.connect (username='Root', pkey=Private_key)9 Ten                     #Create an SSH connection OneSSH =Paramiko. Sshclient () ASsh._transport =Transport -  -                     #Gets the result of the Execute command theStdin,stdout,stderr = Ssh.exec_command ('DF') -  -result =Stdout.read () -  +                     Print(Result.decode ()) -  +Transport.close ()

In the example above, it was found using Paramiko. After the Transport () method is encapsulated, it is better to call the Sshclinet () method, as python2.x and python3.x syntax
and use Paramiko below. Sftpclient () file upload and download, using the syntax is also Paramiko. Transport () method
Sftpclient
For connecting to a remote server and performing an upload download
Upload and download based on user name password
1                 ImportParamiko2 3Transport = Paramiko. Transport (('hostname', 22))4Transport.connect (username='Wupeiqi', password='123')5 6SFTP =Paramiko. Sftpclient.from_transport (transport)7                 #uploading location.py to the server/tmp/test.py8Sftp.put ('/tmp/location.py','/tmp/test.py')9                 #download Remove_path to local Local_pathTenSftp.get ('Remove_path','Local_path') One  ATransport.close ()

Upload and download based on public key keys
1                 ImportParamiko2 3Private_key = Paramiko. Rsakey.from_private_key_file ('/home/auto/.ssh/id_rsa')4 5Transport = Paramiko. Transport (('hostname', 22))6Transport.connect (username='Wupeiqi', pkey=Private_key)7 8SFTP =Paramiko. Sftpclient.from_transport (transport)9                 #uploading location.py to the server/tmp/test.pyTenSftp.put ('/tmp/location.py','/tmp/test.py') One                 #download Remove_path to local Local_path ASftp.get ('Remove_path','Local_path') -  -Transport.close ()



python2.0 S12 Day8 _ Fortress Machine Foreplay Paramiko module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.