Python provides a Paramiko module that allows us to operate on remote systems via SSH, uploading and downloading files. His use is straightforward, see the example below.
Example 1
#!/usr/bin/env python# -*- coding:utf-8 -*-# author yuan liimport paramiko# Create the 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= ' host ', port=22, username= ' root ', password= ' 123 ') # Execute command stdin, stdout, stderr = ssh.exec_command (' df -ht ') # get command results result = stdout.read () print (Result.decode ()) # close Connection ssh.close ()----------"C:\program files\python3 \python.exe " C:/Users/yli/pycharmprojects/Exercise/Week12/paramiko_test.pyFilesystem type size Used Avail Use% Mounted on/dev/mapper/centos-root xfs 28G 6.5G 22G 24% /devtmpfs devtmpfs 988m 0 988M 0% /devtmpfs tmpfs 998M 80K 998M 1% /dev/shmtmpfs tmpfs 998M 112M 887M 12% /runtmpfs tmpfs 998m 0 998m 0% / sys/fs/cgroup/dev/sda1 xfs 497m 169m 329m 34% /boottmpfs tmpfs 200M 12K 200M 1% /run/user/42tmpfs tmpfs 200m 0 200m 0% /run/user/0
If we look at the source of the Ssh.connect () method, we can see that he actually invokes the method of the transport class. So we can use transport directly to create a session and then connect it, and the effect is the same as above.
Example 2
import paramiko# Create a Transport sessiontransport = paramiko. Transport (' Sydnagios ', 22) #连接sessiontransport. Connect (username= ' root ', password= ' Goat2015 ') SSH = paramiko. Sshclient () ssh._transport = transport# execute command stdin, stdout, stderr = ssh.exec_ Command (' DF ') print (Stdout.read (). Decode ()) #创建sftp的对象sftp =paramiko. Sftpclient.from_transport (transport) #上传sftp. Put (' c:\\temp\\aaa.txt ', '/tmp/aaa.txt ') #下载sftp. Get ('/tmp/aaa.txt '), ' C:\\temp\\bbb.txt ') transport.close ()----------"C:\program files\python3\python.exe" c:/users/yli/ pycharmprojects/exercise/week12/paramiko_test.pyfilesystem type size used avail Use% Mounted on/dev/mapper/centos-root xfs 28g 6.5g 22g 24% /devtmpfs devtmpfs 988M 0 988M 0% /devtmpfs tmpfs 998M 80K 998M 1% /dev/shmtmpfs tmpfs 998m 112m 887m 12% /runtmpfs tmpfs 998m 0 998m 0% /sys/fs/cgroup/dev/sda1 xfs 497m 169m 329m 34% /boottmpfs tmpfs 200m 12k 200m 1% /run/user/42tmpfs tmpfs 200M 0 200M 0% /run/user/0
Example 3, we can refine the example 2, the operation is encapsulated into a custom class
Import paramikoimport uuidclass sshconnection (object): #初始化封装字段 def __init__ (self, host= ' Sydnagios ', port=22, username= ' root ', pwd= ' 123 ') : self.host = host self.port = port self.username = username self.pwd = pwd self.__k = None #连接session, perform operation, disconnect session def run (self): self.connect () pass self.close () #连接 def connect (self): transport =&nbSp;paramiko. Transport ((Self.host,self.port)) transport.connect (username= SELF.USERNAME,PASSWORD=SELF.PWD) self.__transport = transport #断开 def close (self): self.__transport.close () #执行命令 def cmd (self, command): ssh = paramiko. Sshclient () ssh._transport = self.__transport # Execute Command stdin, stdout, stderr = ssh.exec_command (command) # Get command Results rEsult = stdout.read () return result def upload (Self,local_path, target_path): # Connect, upload sftp = paramiko. Sftpclient.from_transport (Self.__transport) # will location.py Upload to server /tmp/test.py sftp.put (local_path, target _path) ssh = sshconnection () Ssh.connect () r1 = ssh.cmd (' DF ') print (R1.decode ()) Ssh.upload (' C : \\temp\\aaa.txt ', "/home/yli/s7.py") ssh.close ()----------"C:\program files\python3\python.exe" c:/s13 Class Code/S13DAY13 Class code/s13day13_ Class Code/paramiko module _demo.pyfilesystem 1k-blocks used available use% mounted on/dev/mapper/centos-root 28813572 6760732 22052840 24% /devtmpfs 1011616 0 1011616 0% /devtmpfs 1021272 80 1021192 1% /dev/shmtmpfs 1021272 113928 907344 12% /runtmpfs 1021272 0 1021272 0% / sys/fs/cgroup/dev/sda1 508588 172604 335984 34% /boottmpfs 204256 12 204244 1% /run/user/ 42tmpfs 204256 0 204256 0% /run/user/0
This article is from the "Mapo Tofu" blog, please be sure to keep this source http://beanxyz.blog.51cto.com/5570417/1875644
Python Learning Note-Paramiko module