The Python Paramiko module enables

Source: Internet
Author: User



General use of the Paramiko function is to remotely execute commands via SSH, remote (upload, download) transfer files and so on

Dependency Package:/usr/local/python27/bin/pip Install Pycrypto

Download Package Paramiko

#wget Http://en.onlinedown.net/down/paramiko-1.7.7.1.zip

#unzip Paramiko-1.7.7.1.zip

#cd paramiko-1.7.7.1

#python setup.py Build

#python setup.py Install


Test scripts

#vim paramiko.py

#!/usr/bin/env python

Import Paramiko \ \ Imports Module

Hostname= ' 192.168.10.106 ' \ \ \ Define the host that needs to be linked

PORT=22 \ \ Define a linked port

Username= ' root ' \ \ definition Linked account

password= ' 123456 ' \ \ \ definition Login Password


If __name__== ' __main__ ': \ \ If the current module name equals the start of the main module run, do the following

Paramiko.util.log_to_file (' paramiko.log ') \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ Specify log file Paramiko.log

S=paramiko. Sshclient () \ \ Create a client-side link to the server object s

S.load_system_host_keys () \ \ \ Load host secret key

S.connect (hostname,port,username,password) \ \ Link Host

Stdin,stdout,stderr=s.exec_command (' ifconfig ')

\ \ Execute Command ifconfig stdin: standard input stdout: standard output stderr: standard error

Print stdout.read () \ \ Printing results of standard output

S.close () \ \ Close Link





Upload or download files from the server side


#!/usr/bin/env python

Import Paramiko Module

Import OS

Hostname= ' 192.168.10.106 ' \ \ \ Define the host that needs to be linked

PORT=22 \ \ Define a linked port

Username= ' root ' \ \ definition Linked account

password= ' 123456 ' \ \ \ definition Login Password

Dir_path= '/home/soul/temp ' \ \ Defines the file path for the requested download


If __name__== ' __main__ ':

T=paramiko. Transport ((hostname,port)) \ \ Create a Transport object

T.connect (Username=username,password=password) \ \ Establish a linked server

Sftp=paramiko. Sftpclient.from_transport (t) \ \ Create a download transport object sftp

Files=sftp.listdir (Dir_path)

\ \ Create a Files object to hold the file Listdir from the specified path Dir_path

For f in files: \ \ Defines f to poll from a stored file

print ' retrieving ', f \ \ Prints the file name you got

Sftp.get (Os.path.join (dir_path,f), f) \ \ Download files from the specified path

T.close () \ \ Close Link

Download: Get upload put

If the/home/soul/temp/paramiko-1.7.7.1.zip file is uploaded locally to the server's/home/path

Sftp.put ('/home/soul/temp/paramiko-1.7.7.1.zip ', '/home/paramiko-1.7.7.1.zip ')

T.close ()


This article is from the "My_soul" blog, make sure to keep this source http://soul455879510.blog.51cto.com/6180012/1872311

The Python Paramiko module enables

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.