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