Python Basics (ix)

Source: Internet
Author: User

One, Paramiko module

1.1 Introduction

Paramiko is a free and open source module used to implement SSH2 protocol security (authentication and encryption) to connect to a remote computer using the module to perform command or file operations on a remote server, which is worth saying, The remote management of fabric and ansible is the use of Paramiko to reality.

1.2 Paramiko Simple to use

1) Sshclient:

Connect based on user name and password

ImportParamiko#To create an SSH objectSSH =Paramiko. Sshclient ()#allow connections to hosts that are not in the Know_hosts fileSsh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())#connecting to a serverSsh.connect (hostname='192.168.1.108', port=22,username='Root', password='123456')#Execute CommandStdin,stdout,stderr = Ssh.exec_command ('df-h')#Get command Resultsresult =Stdout.read (). Decode ()#Print ResultsPrint(Result)#Close ConnectionSsh.close ()

Show Results:
File system capacity has been used with available percent mount points
/dev/sda3 18G 13G 4.3G 75%/
Tmpfs 495M 4.0K 495M 1%/DEV/SHM
/DEV/SDA1 190M 48M 133M 27%/boot

Sshclient Package Transport:

ImportParamikotransport= Paramiko. Transport (('192.168.1.108', 22)) Transport.connect (username='Root', password='123456') SSH=Paramiko. Sshclient () Ssh._transport=Transportstdin,stdout,stderr= Ssh.exec_command ('free-m')Print(Stdout.read (). Decode ()) Transport.close ( )

The results show:
Total used free shared buffers cached
mem:988 437 551 0
-/+ buffers/cache:307 681
swap:2047 0 2047

Connection based on public key:

ImportParamikoprivate_key= Paramiko. Rsakey.from_private_key_file ('C:\id_rsa')#To create an SSH objectSSH =Paramiko. Sshclient ()#allow connections to hosts that are not in the Know_hosts fileSsh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())#connecting to a serverSsh.connect (hostname='192.168.1.108', port=22,username='Root', pkey=Private_key)#Execute CommandStdin,stdout,stderr = Ssh.exec_command ('df-h')#Get command Resultsresult =Stdout.read (). Decode ()Print(Result)#Close ConnectionSsh.close ()

Show Results:
File system capacity has been used with available percent mount points
/dev/sda3 18G 13G 4.3G 75%/
Tmpfs 495M 4.0K 495M 1%/DEV/SHM
/DEV/SDA1 190M 48M 133M 27%/boot

Sshclient Package Transport:

ImportParamikoprivate_key= Paramiko. Rsakey.from_private_key_file ('C:\id_rsa') Transport= Paramiko. Transport (('192.168.1.108', 22)) Transport.connect (username='Root', pkey=private_key) SSH=Paramiko. Sshclient () Ssh._transport=Transportstdin,stdout,stderr= Ssh.exec_command ('df-h')Print(Stdout.read (). Decode ()) Transport.close ( )


2) sftpclient

For connecting to a remote server and performing an upload download

ImportParamikotransport= Paramiko. Transport (('192.168.1.108', 22)) Transport.connect (username='Root', password='123456') sftp=Paramiko. Sftpclient.from_transport (transport)#The local Id_rsa file is uploaded to the server/tmp/belowSftp.put ('C:\id_rsa','/tmp/id_rsa')#Download the Nihao below/tmp to the local C: directorySftp.get ('/tmp/nihao','C:\\nihao') Transport.close ()

Upload and download based on public key keys

ImportParamikoprivate_key= Paramiko. Rsakey.from_private_key_file ('C:\id_rsa') Transport= Paramiko. Transport (('192.168.1.108', 22)) Transport.connect (username='Root', pkey=private_key) sftp=Paramiko. Sftpclient.from_transport (transport)#The local Id_rsa file is uploaded to the server/tmp/belowSftp.put ('C:\id_rsa','/tmp/id_rsa')#Download the Nihao below/tmp to the local C: directorySftp.get ('/tmp/nihao','C:\\nihao') Transport.close ()

Python Basics (ix)

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.