Python Learning Path Network programming chapter (fifth)

Source: Internet
Author: User
Tags stdin

Paramiko Introduction

Paramiko is a python-based SSH2 Remote installation connection that supports authentication and secret key methods. Can implement remote command execution, file transfer, intermediate SSH proxy and other functions.

  Paramiko Installation 

#!/bin/bash#install indepence Packagecd/data/softwget https://www.dlitz.net/pub/dlitz/crypto/pycrypto/ Pycrypto-2.6.tar.gzyum-y install gcc python-develtar zxf pycrypto-2.6.tar.gz-c/usr/local cd/usr/local/ Pycrypto-2.6python setup.py buildpython setup.py installwget https://pypi.python.org/packages/source/e/ecdsa/ Ecdsa-0.13.tar.gz#md5=1f60eda9cb5c46722856db41a3ae6670tar zxf ecdsa-0.13.tar.gz-c/usr/local cd/usr/local/ Ecdsa-0.13python setup.py build python setup.py install#install Paramiko packagecd/data/softwget https:// Pypi.python.org/packages/source/p/paramiko/paramiko-1.16.0.tar.gz#md5=7e1203f5ffeb7d2bc2bffc4feb804216tar ZXVF Paramiko-1.16.0.tar.gz-c/usr/localcd/usr/local/paramiko-1.16.0python setup.py buildpython setup.py Install

Import Paramiko module: Import Paramiko

[Email protected] ~]# Pythonpython 2.6.6 (r266:84292, Jul, 15:22:56) [GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on L Inux2type "Help", "copyright", "credits" or "license" for more information.>>> import paramiko>>>

Paramiko API Installation: Pip install Paramiko

Paramiko Login to remote host based on username and password

Sshclient

Used to connect to a remote server and execute basic commands

Import Paramiko   # SSH object created ssh = Paramiko. Sshclient () # allows connections to host Ssh.set_missing_host_key_policy (Paramiko) that are not in the Know_hosts file. Autoaddpolicy ()) # Connection Server Ssh.connect (hostname= ' 172.16.2.100 ', port=22, username= ' root ', password= ' 123345 ')   # Execute command stdin, stdout, stderr = Ssh.exec_command (' ls ') # get command result = Stdout.read ()   # Close Connection ssh.close ()

The specific implementation script is as follows:

#!/usr/bin/env pythonimport paramikotransport = Paramiko. Transport ((' 172.20.2.234 ')) Transport.connect (username= ' root ', password= ' 000000 ') ssh = Paramiko. Sshclient () Ssh._transport = Transportstdin,stdout,stderr = Ssh.exec_command (' uname-a ') print (Stdout.read ()) Transport.close ()
Paramiko Login to remote host based on secret key
Import Paramiko  Private_key = Paramiko. Rsakey.from_private_key_file ('/root/.ssh/id_rsa ')  # create ssh object ssh = Paramiko. Sshclient () # allows connections to host Ssh.set_missing_host_key_policy (Paramiko) that are not in the Know_hosts file. Autoaddpolicy ()) # Connection Server Ssh.connect (hostname= ' 172.16.2.234 ', port=22, username= ' root ', Key=private_key)  # Execute command stdin, stdout, stderr = Ssh.exec_command (' df ') # get command result = Stdout.read ()  # Close Connection ssh.close ()

The specific implementation script is as follows:

[email protected] paramiko]# cat para_key.py #!/usr/bin/env python import paramikoprivate_key = Paramiko. Rsakey.from_private_key_file ('/root/.ssh/id_rsa ') transport = Paramiko. Transport ((' 172.20.2.234 ')) Transport.connect (username= ' root ', pkey=private_key) ssh = Paramiko. Sshclient () Ssh._transport = Transportstdin,stdout,stderr =  ssh.exec_command (' Df-ht ') print (Stdout.read ()) Transport.close ()
Paramiko for uploading and downloading remote files

Sftpclient

For connecting to a remote server and performing an upload download

Upload and download based on user name password:

Import Paramiko  transport = Paramiko. Transport (' 172.16.2.234 ', ()) Transport.connect (username= ' root ', password= ' 123345 ')  sftp = Paramiko. Sftpclient.from_transport (transport) # upload location.py to Server/tmp/test.pysftp.put ('/data/local/location.py ', '/data/ remote/test.py ') # download Remove_path to local local_pathsftp.get (' Remove_path ', ' Local_path ')  transport.close ()

The specific implementation code is as follows:

#!/usr/bin/env Pythonimport paramikousername = ' root ' password = ' 000000 ' hostname = ' 172.20.2.234 ' port = 22try:    t = par Amiko. Transport ((hostname,port))    t.connect (username=username,password=password)    sftp = Paramiko. Sftpclient.from_transport (t)    sftp.put ('/data/local/localfile ', '/data/remote/remotefile ')    sftp.get ('/ Data/remote/remotefile1 ', '/data/local/localfile1 ')    sftp.mkdir ('/data/aaa ', 0755)    sftp.rmdir ('/data/aaa ') )    sftp.rename ('/data/remote/test.sh ', '/data/remote/testfile.sh ')    print (Sftp.stat ('/data/remote/ testfile.sh ')    print (Sftp.listdir ('/data/remote ')) except Exception as E:    print (e)

  

 

Python Learning Path Network programming chapter (fifth)

Related Article

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.