[Python tour] Article 6 (1): demo of Paramiko Module
At the end of the fifth article, there should have been an ftp software development job. However, compared with what I wrote with my senior brother, I felt that my writing was too bad, so I won't upload it, wait for the technology to be better, and then develop some gadgets. Now it's still far away, and we have to work very hard! The Paramiko module is used to prepare for the development of monitoring software in the future. Therefore, the Paramiko module is used to connect to the remote host, and the Paramiko module is used for remote file transfer. The main content is the following items: 1. paramiko SSH connection to the remote host (1) connect to the remote host using the user name and password (2) connect to the remote host using the key 2. paramiko SFTP Transfer File Paramiko module is a third-party library, so you need to install it first. paramiko SSH connection to the remote host (1) Use the user name and password to connect to the remote host program code and comments are as follows:
#! /Usr/bin/env pythonimport paramiko # import the paramiko module import sys, oshost = sys. argv [1] # obtain the command line input through argv under sys, here, the first parameter user = 'oldboy '# password of the remote host to be connected = '000000' # cmd = sys. argv [2] # obtain the command line input through argv under sys. Here, the second parameter s = paramiko of the command is obtained. SSHClient () # Call SSHClient () under the paramiko Module ()
S. load_system_host_keys () # load the local known_hosts file, which records the host key given by the other Party when the file is connected to the other party. During each connection, check whether the host key given by the other party is the same as the recorded host key. You can simply verify whether the link is fraudulent and other related matters. S. set_missing_host_key_policy (paramiko. autoAddPolicy () # If you add this sentence, you do not have to worry about selecting yes. The system will automatically select the option (when you connect to a remote host through ssh, the system will prompt you whether to continue the remote connection during the first connection, select yes) s. connect (host, 22, user, password, timeout = 5) connect to the remote host. The sshport number is 22stdin,stdout,stderr;s.exe c_command (cmd) # execute the command pai_result = stdout. read (), stderr. read () # read the command results. when reading the command results, one is empty, and the other is not empty. If there is a mistake, no result is returned. If there is a result, no result is returned. For line in each _result: print line, s. close () about sys. argv [] method, you can see here: the http://xpleaf.blog.51cto.com/9315560/1700811 below demonstrates the execution result, here I have a host with the same network segment, IP Address: 192.168.1.124, as follows:
root@xpleaf-machine:/mnt/hgfs/Python/day6# python ssh1.py 192.168.1.124 dfFilesystem 1K-blocks Used Available Use% Mounted on/dev/sda3 9602276 3538300 5576196 39% /tmpfs 959228 0 959228 0% /dev/shm/dev/sda1 198337 34143 153954 19% /bootroot@xpleaf-machine:/mnt/hgfs/Python/day6# python ssh1.py 192.168.1.124 'df -h'Filesystem Size Used Avail Use% Mounted on/dev/sda3 9.2G 3.4G 5.4G 39% /tmpfs 937M 0 937M 0% /dev/shm/dev/sda1 194M 34M 151M 19% /boot
The returned data is the command execution result of the remote host. (2) using the key to connect to the remote host is not much different from (1), but the authentication method is to use the key. Therefore, you do not need to write a password in the Code. The Code and comments are given first:
#! /Usr/bin/env pythonimport paramikoimport sys, oshost = sys. argv [1] user = 'oldboys' pkey _ file = '/home/xpleaf /. ssh/id_rsa '# specifies the path of the private key to be decrypted. This must be manually generated. The following describes how to generate key = paramiko. RSAKey. from_private_key_file (pkey_file) # use the private key to decrypt cmd = sys. argv [2] s = paramiko.SSHClient()s.load_system_host_keys()s.set_missing_host_key_policy(paramiko.AutoAddPolicy())s.connect(host,22,user,pkey=key,timeout=5)stdin,stdout,stderr=s.exe c_command (cmd) cmd_result = stdout. read (), stderr. read () for line in rows _result: print line,
S. close () before using the above program, let's talk about how to generate the private key and Public Key (symmetric and asymmetric encryption, public key encryption, Private Key decryption, and so on ), at the same time, the public key is also sent to the authenticated party (remote host): 1) the authenticated party generates the private key and Public Key
Xpleaf @ xpleaf-machine:/mnt/hgfs/Python/day6 $ ssh-keygen xpleaf @ xpleaf-machine:/mnt/hgfs/Python/day6 $ ls/home/xpleaf /. ssh/id_rsa id_rsa.pub known_hosts2) Send the public key to the authenticator (remote host)
1xpleaf @ xpleaf-machine:/mnt/hgfs/Python/day6 $ ssh-copy-id after completing the above steps, you can connect to the remote host and execute the command:
root@xpleaf-machine:/mnt/hgfs/Python/day6# python ssh_key2.py 192.168.1.124 dfFilesystem 1K-blocks Used Available Use% Mounted on/dev/sda3 9602276 3538304 5576192 39% /tmpfs 959228 0 959228 0% /dev/shm/dev/sda1 198337 34143 153954 19% /bootroot@xpleaf-machine:/mnt/hgfs/Python/day6# python ssh_key2.py 192.168.1.124 'df -h'Filesystem Size Used Avail Use% Mounted on/dev/sda3 9.2G 3.4G 5.4G 39% /tmpfs 937M 0 937M 0% /dev/shm/dev/sda1 194M 34M 151M 19% /boot
2. Paramiko SFTP Transfer File Code and comments are as follows:
#! /Usr/bin/env pythonimport paramikoimport sys, oshost = sys. argv [1] user = 'oldboy 'password = '000000' t = paramiko. transport (host, 22) t. connect (username = user, password = password) # You can use the key for the connection method. Here you only need to Change password = password to pkey = key, the remaining key code is the same as the previous sftp = paramiko. SFTPClient. from_transport (t) # Use the t setting method to connect to the remote host sftp. get ('/tmp/hello.txt', 'hello.txt') # download the sftp file. put ('ssh1. py ','/tmp/ssh1.py ') # upload an object
T. close (): here, the FTP client has the file ssh1.py, And the FTP server (host 192.168.1.124)/tmpdirectory has the hello.txt file. Then, upload ssh1.pyto the service terminal and download hello.txt to the current directory of the client. 1) run the program
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day6$ python ssh_sftp3.py 192.168.1.124
If there are no errors, there will be no prompts. 2) view the downloaded file under the client
Xpleaf @ xpleaf-machine: /mnt/hgfs/Python/day6 $ ls-l total usage 7-rwxrwxrwx 1 root 19 October 8 23:49 hello.txt drwxrwxrwx 1 root 4096 October 8 22:40 sorftwares-rwxrwxrwx 1 root 435 October 8 11:36 ssh1.py- rwxrwxrwx 1 root 544 October 8 15:07 ssh_key2.py-rwxrwxrwx 1 root 331 October 8 23:42 ssh_sftp3.py
3) view uploaded files on the server
[oldboy@moban tmp]$ ls -ltotal 12drwxrwxrwt. 3 root root 4096 May 13 09:35 etc-rw-rw-r-- 1 oldboy oldboy 19 Oct 8 12:11 hello.txt-rw-rw-r-- 1 oldboy oldboy 435 Oct 8 20:31 ssh1.py