Reference
http://wangwei007.blog.51cto.com/blog/68019/1058726
http://youzao.blog.51cto.com/3946111/1348903
On the node1, I want to execute the command on node2, the standard answer.
1. More Intuitive
#!/usr/bin/env pythonImport Paramikohostname= ' 192.168.0.102 'username= ' root 'password= ' abc 'port=22paramiko.util.log_to_file (' paramiko.log ')s=paramiko. Sshclient ()S.set_missing_host_key_policy (paramiko. Autoaddpolicy ())s.connect (hostname = hostname,port=port,username=username, Password=password)stdin,stdout,stderr=s.exec_command (' free;df-h ')Print Stdout.read ()
2. form of the class
#!/usr/bin/python
# Coding=utf8Import paramiko,datetime,os,threading#class Run_cmd (threading. Thread):class Run_cmd ():def __init__ (self,hostname=none,password=none,username=none,port=none,echo_cmd=none):#threading. thread.__init__ (self)Self.hostname=hostnameSelf.password=passwordSelf.username=usernameSelf.port=portSelf.echo_cmd=echo_cmd#self. Thread_stop=falsedef run (self):paramiko.util.log_to_file (' paramiko.log ')s=paramiko. Sshclient ()S.set_missing_host_key_policy (paramiko. Autoaddpolicy ())s.connect (hostname = self.hostname,username=self.username, Password=self.password)Stdin,stdout,stderr=s.exec_command (self.echo_cmd)return Stdout.read ()s.close ()def stop (self):self.thread_stop=true if __name__== ' __main__ ':f = File ('/home/python/filelist1 ', ' r ')port=22C = F.readlines ()for x in C:hostname = x.split (':: ') [0]password = x.split (':: ') [1]username = x.split (':: ') [2]remote = x.split (':: ') [4].strip (' \ n ')echo_cmd= '/bin/find%s-maxdepth 1-type d-mmin-1200 '% (remote)#echo_cmd = ' free;df-h 'Cmd_thread=run_cmd (hostname,password,username,port,echo_cmd)Result=cmd_thread.run ()Print Resultf.close ()
If you run an error, please refer to the Python Incremental Sync remote folder
This article from "people, to have their own ideas" blog, declined to reprint!
Python Paramiko execute command