Paramiko Remote Control module
Paramiko is a module for remote control, which can be used for command or file operation on a remote server . The remote management of fabric and ansible is the use of Paramiko to reality.
1. Download and install
# Pycrypto, since the Paramiko module is internally dependent on Pycrypto, download the installation first Pycrypto
# Download Install Pycrypto
wget http://files.cnblogs.com/files/wupeiqi/pycrypto-2.6.1.tar.gz
TAR-XVF pycrypto-2.6.1.tar.gz
CD pycrypto-2.6.1
Python setup.py Build
Python setup.py Install
# Enter the Python environment, import crypto to check if the installation was successful
# Download Install Paramiko
wget http://files.cnblogs.com/files/wupeiqi/paramiko-1.10.1.tar.gz
TAR-XVF paramiko-1.10.1.tar.gz
CD paramiko-1.10.1
Python setup.py Build
Python setup.py Install
# Enter the Python environment, import Paramiko to check if the installation was successful
2. Using the module
1) Execute command-connect to server via username and password
Import Paramiko
SSH = Paramiko. Sshclient ()
Ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())
Ssh.connect (' 192.168.1.108 ', 22, ' username ', ' password')
stdin, stdout, stderr = Ssh.exec_command (' DF ')
Print Stdout.read ()
Ssh.close ();
2) Execute command-over key link server
Import Paramiko
Private_key_path = '/home/auto/.ssh/id_rsa'
Key = Paramiko. Rsakey.from_private_key_file (Private_key_path)
SSH = Paramiko. Sshclient ()
Ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())
Ssh.connect (' hostname ', port, ' username ', key)
stdin, stdout, stderr = Ssh.exec_command (' DF ')
Print Stdout.read ()
Ssh.close ()
3) upload or download files-by user name and password
Import Os,sys
Import Paramiko
conn = Paramiko. Transport (' 192.168.20.21 ',())
Conn.connect (username= ' username ', password= ' password ')
SFTP = Paramiko. Sftpclient.from_transport (conn)
Sftp.put ('/tmp/test.py ', '/tmp/test.py ')
Conn.close ()
Import Os,sys
Import Paramiko
conn = Paramiko. Transport (' 192.168.20.21 ',())
Conn.connect (username= ' username ', password= ' password ')
conn = Paramiko. Sftpclient.from_transport (conn)
Sftp.get ('/tmp/test.py ', '/tmp/test2.py ')
Conn.close ()
4) upload or download files-via key
Import Paramiko
Pravie_key_path = '/home/auto/.ssh/id_rsa'
Key = Paramiko. Rsakey.from_private_key_file (Pravie_key_path)
conn = Paramiko. Transport (' 192.168.20.21 ',())
Conn.connect (username= ' username ', pkey=key)
SFTP = Paramiko. Sftpclient.from_transport (conn)
Sftp.put ('/tmp/test3.py ', '/tmp/test3.py ')
Conn.close ()
Import Paramiko
Pravie_key_path = '/home/auto/.ssh/id_rsa '
Key = Paramiko. Rsakey.from_private_key_file (Pravie_key_path)
conn = Paramiko. Transport (' 192.168.20.21 ',())
Conn.connect (username= ' username ', pkey=key)
SFTP = Paramiko. Sftpclient.from_transport (conn)
Sftp.get ('/tmp/test3.py ', '/tmp/test4.py ')
Conn.close ()
This article is from the "a myriad of simple pleasures of technology to enjoy" blog, please be sure to keep this source http://51enjoy.blog.51cto.com/8393791/1837839
Paramiko Remote Control Module