Implementation of the SSH protocol in Python-Paramiko

Source: Internet
Author: User

When operating system maintenance, the SSH command is usually connected to the remote server for some operations. How to do this in Python, of course, can also execute SSH commands, but there is a more elegant way, with the help of Paramiko, which implements the SSHV2 protocol of an open source project, the following mainly uses its SSH and SFTP client related functions.



Installation

# pip Install Paramiko



SSH client Use

In [1]: Import Paramiko


#获取SSHClient对象.

In [2]: SSH = Paramiko. Sshclient ()


#设置host key policy.

#ssh. Load_system_host_keys ('/root/.ssh/known_hosts ')

In [3]: Ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())


#连接到SSH server, the Connect method has several parameters that can be set as needed, such as a time-out (timeout).

In [4]: Ssh.connect (' 192.168.1.4 ')


Executes a command on #在SSH server, returning its I/O stream, similar to a file handle in Python, standard output (STDOUT) and standard error (STDERR) are more commonly used.

In [5]: stdin, stdout, stderr = Ssh.exec_command (' ls-l/tmp/')


#获取标准输出

In [all]: for line in stdout:

...: print ' + ' + line.strip (' \ n ')

...:

--Total 16

--rw-r--r--1 root root 2 Jan 18:27 a.txt

--rw-r--r--1 root root 2 Jan 18:27 b.txt

--rw-r--r--1 root root 2 Jan 18:27 c.txt

--rw-r--r--1 root root 2 Jan 18:27 d.txt


SFTP Client Use

#在连接到SSH server, gets the Sftpclient object.

SFTP = Ssh.open_sftp ()


#上传文件

In []: Sftp.put ('/tmp/zz.txt ', '/tmp/')

---------------------------------------------------------------------------

IOError Traceback (most recent)

<ipython-input-13-0e19a403e0d2> in <module> ()

----> 1 sftp.put ('/tmp/zz.txt ', '/tmp/')


/usr/local/python27/lib/python2.7/site-packages/paramiko/sftp_client.pyc in put (self, localpath, RemotePath, Callback, confirm)

...

Ioerror:failure


In [14]:


Why the error, the command line test normal ...

# SFTP 192.168.1.4

Connected to 192.168.1.4.

Sftp> put '/tmp/zz.txt '/tmp '

Uploading/tmp/zz.txt To/tmp/zz.txt

/tmp/zz.txt 100% 3 0.0kb/s 00:00

Sftp>


When viewing the Put method in sftp_client.py, the RemotePath parameter is found and the name of the target file needs to be indicated.

:p Aram Str remotepath:the destination path on the SFTP server. Note

That's the filename should be included. Only specifying a directory

May result in an error.


That's all you can do.

in [+]: sftp.put ('/tmp/zz.txt ', '/tmp/zz.txt ')

OUT[21]: <sftpattributes: [size=3 uid=901 gid=901 mode=0100664 atime=1516792881 mtime=1516792881]>


#关闭连接

In []: Sftp.close ()


in [+]: Ssh.close ()


If interested, you can follow the subscription number "database Best practices" (Dbbestpractice).

Implementation of the SSH protocol in Python-Paramiko

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.