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) channel=stdout.channel ret=channel.recv_exit_status ()ifRET = =0:return(ret, Stdout.read ())Else: return(ret, Stderr.read ())if __name__=='__main__': IP='192.168.0.1'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