Use of the Paramiko module in Python

Source: Internet
Author: User
Tags disk usage

Paramiko is a Python module that adheres to the SSH2 protocol and supports remote server connections in an encrypted and authenticated manner.
1. Can remotely manipulate server files

For example:
DF: View disk usage
mkdir: Creating a Directory
Mv/cp/mkdir/rmdir: Working with files or directories
/sbin/service/xxxservice Start/stop/restart: Start, stop, restart a service
Netstat-ntl |grep 8080: viewing usage of port 8080
or nc-zv localhost: View the usage of all ports
Find/-name XXX: Finding a file, etc.

2, can realize the remote file upload, download (similar to the SSH function of the SCP)

Install Paramiko First
Pip Install Paramiko
However, installing Paramiko requires installing a module that relies on the package called Pycrypto. Pycrypto is a cryptographic toolkit written by Python, supported by a variety of cryptographic algorithms (mainly: MD2-bits; MD4-bits; MD5-bits; RIPEMD bits; SHA1 bits; SHA256-N-bits; AES, bytes/16 bytes; ARC2 VARIABLE/8 bytes; Blowfish VARIABLE/8 bytes; CAST variable/8 bytes;des 8 bytes/8 bytes;D ES3 (Triple DES) bytes/8 Bytes;idea (bytes/8 bytes) RC5 variable/8 bytes and so on. )

The specific instance code is as follows:
1. Implement simple command operation

1 #!/usr/bin/env Python 2 #-*-coding:utf-8-*-3  4 Import Paramiko 5  6 #创建ssh对象 7 ssh = Paramiko. Sshclient () 8  9 #允许连接不在know_host中的主机10 ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) #连接服务器13 ssh.connect (hostname= ' 192.168.1.1 ', port=22,username= ' user ', password= ' pwd ') 14 15 #  Execute command stdin, stdout, stderr = Ssh.exec_command (' cd home;ls-l ') #执行结果19 #result = Stderr.read () #如果有错误则打印20 result = Stdout.read () Print result22 #关闭连接23 ssh.close ()

2. Implement remote Upload

 1 #!/usr/bin/env Python 2 #-*-coding:utf-8-*-3 # Author:Eric.yue 4 5 Import Paramiko,os 6 7 class Paramiko_put (Obje          CT): 8 def __init__ (self,local_dir,remote_dir): 9 self.host = ' 192.168.1.1 ' self.username = ' user ' 11 self.passwd = ' pwd ' self.port = 2213 Self.local_dir = Local_dir14 Self.remote_dir = rem Ote_dir15 self.tt = None16 def pk_connect (self): Self.tt = Paramiko. Transport ((Self.host, Self.port)) self.tt.connect (username = self.username, password = self.passwd) tr y:21 return Paramiko. Sftpclient.from_transport (SELF.TT) except Exception as e:23 print ' Connect error: ', E24 E         XIT () def put_file (self): SFTP = Self.pk_connect () files = Os.listdir (self.local_dir) 29 CNT = 030 for file in files:31 sftp.put (Os.path.join (self.local_dir, file), Os.path.join (Self.remot     E_dir, file)) 32        CNT + = 133 if cnt = = Len (files): Print str (CNT) + ' files put successful ' Else:3 7 print ' Put failure ' __del__ def (self): self.tt.close () pk = Paramiko_put ('/home/mywo Rk/test/day8 ', '/home/mywork/test/day8 ') pk.put_file ()

 3, implementing remote downloads

 1 #!/usr/bin/env Python 2 #-*-coding:utf-8-*-3 # Author:Eric.yue 4 5 6 Import Paramiko,os 7 8 class Paramiko_get (o Bject): 9 def __init__ (Self,remote_dir,local_dir): Ten self.host = ' 192.168.1.1 ' one self.username = ' user ' self.passwd = ' pwd ' self.port = 2214 Self.local_dir = Local_dir15 Self.remote_dir = Remote_dir16 self.tt = None17 def pk_connect (self): Self.tt = Paramiko. Transport ((Self.host, Self.port)) self.tt.connect (username = self.username, password = self.passwd) tr y:22 return Paramiko. Sftpclient.from_transport (SELF.TT) except Exception as e:24 print ' Connect error: ', E25 E         XIT () def get_file (self): SFTP = Self.pk_connect () files = Sftp.listdir (self.remote_dir) 30 CNT = 031 for file in files:32 sftp.get (Os.path.join (self.remote_dir, file), Os.path.join (Self.lo Cal_dir, file)) 33             CNT + = 134 + if cnt = = Len (files): Print str (CNT) + ' files get successful ' PNs lse:38 print ' Get failure ' Max def __del__ (self): self.tt.close () pk = Paramiko_get ('/home /inf/mywork/day8 ', '/home/mywork/day8 ') pk.get_file ()

Warm tip: To implement the upload and download function to ensure that the directory exists, there are operational permissions Oh!

Use of the Paramiko module in Python

Related Article

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.