Login based on user name and password sshclient:
#!/usr/bin/env python#-*-coding=utf-8-*-#说明: Login based on user name and password sshclient mode import paramikotry:ssh = Paramiko. Sshclient () Ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) Ssh.connect (hostname= "192.168.58.136", port=22,username= "root", password= "111111") Stdin,stdout, stderr = Ssh.exec_command ("Ifconfig") print (Stdout.read (). Decode (). Rstrip ()) Ssh.close () except Exception as E:p Rint ("The operation failed! ")
Login based on user name and password transport:
#!/usr/bin/env python#-*-coding=utf-8-*-#说明: Login based on user name and password transport mode import Paramikotry:trans = Paramiko. Transport (("192.168.58.136")) Trans.connect (username= "root", password= "111111") ssh = Paramiko. Sshclient () Ssh._transport = trans Stdin,stdout,stderr = Ssh.exec_command ("Ifconfig") print (Stdout.read (). Decode (). Rstrip ()) Trans.close () except Paramiko. Sshexception:print "Operation failed! "
Transfer files:
#!/usr/bin/env python#-*-coding=utf-8-*-#说明: Transfer file Import Paramikotry:trans = Paramiko. Transport (("192.168.58.136")) Trans.connect (username= "root", password= "111111") sftp = Paramiko. Sftpclient.from_transport (trans) #发送文件 Sftp.put (localpath= "/root/code/test.py", remotepath= "/root/test.py") #下载文件 #sftp. Put (Remotepath,localpath) trans.close () except Paramiko. Sshexception:print "Operation failed"
Python third-party library Paramiko SSH Connection