Python Paramiko execute command

Source: Internet
Author: User

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

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.