Python operation remote server Paramiko module Introduction

Source: Internet
Author: User
Tags ssh remote command

The Paramiko module is a python-based remote secure SSH connection for SSH remote command execution, file transfer, and more.

Installation Method

The Paramiko module is not a standard library that comes with Python and performs pip install Paramiko for installation. If the PIP is not configured, refer to <python tools installation and Pip tool configuration > Complete configuration.

How to use

1. Execute the specified command on the remote server

ImportParamikodefSsh_con (IP, username, password):Try: Con=Paramiko. Sshclient () Con.set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) Con.connect (IP=ip, Username=username, Password=password, timeout=5)    except:        returnNoneElse:        returnCondefexec_cmd (conn, cmd): Interact=Conn.invoke_shell () interact.send (cmd) result= INTERACT.RECV (65535)    returnresultdefexec_cmd2 (conn,cmd): stdin, stdout, stderr=Conn.exec_command (cmd) ret=channel.recv_exit_status ()ifRET = =0:return(ret, ())Else:        return(ret, ())if __name__=='__main__': IP=''User='Root'passwd='Root'Con=Ssh_con (IP, user, passwd)if  notCon:Print "Connect%s fail,please check."%(IP,) cmd='df-k'ret, result=exec_cmd2 (con,cmd)Printresult

Python operation remote server Paramiko module Introduction

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: 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.