The pexpect of the Python module

Source: Internet
Author: User

Pexpect is a Python module used to start subroutines and automatically control them.

Pexpect can be used to automatically interact with command-line programs like SSH, passwd, Telnet, rsync, etc.

Here is a script I wrote with Pexpect, which is used to execute the remote host command, and the script automatically interactively enters the remote host's password.

# encoding: utf-8#filename:python_rsync.py__author__ =  "ZhouLS" #同要用来同步远程主机上的日志文件到本地. import pexpectimport os,sysimport time# Remote host address dir =  "/usr/local/tomcat48080/webapps/ Webapi_develop/logs "#本地主机地址Ldir  = "/logs/"#远程主机列表, Host: Password   #也可自己定义其他方式, hosts = [' 192.168.20.20:redhat ', ' 192.168.20.22:redhat ']# #定义一个同步文件的方法def  rsync (sfile):     # Use a For loop to remove the remote host and password, and execute the corresponding remote command, automatically submit the password     for i in hosts:         hostname = i.split (': ') [0]         passwd = i.split (': ') [1]        sfile =  Os.path.join (Dir,sfile)         dfile = os.path.join (Ldir, hostname)          #cmd命令可以修改为自己的命令, own play, I used the rsync command to sync files.         cmd =  "" "rsync -azv [email protected]%s:%s %s" ""  %  (hostname,sfile,dfile)          print cmd                  #下面是自动交互程序                  ssh = pexpect.spawn (cmd, [], 86400)          try:             while true: #使用一个循环来进入交互式                  i = ssh.expect ([' assword:  ',  ' continue connecting  (yes/ NO)?   #接收交互的提示                  if i == 0:  #如果是assword: Send password directly and jump out of the loop                       ssh.sendline (passwd)                      break                 elif i == 1:# If the hint is yes/no)? Send yes directly and continue to the next loop                      ssh.sendline (' yes ')          Except pexpect. eof:            ssh.close         else:             Ssh.expect (Pexpect. EOF)             ssh.close ()                      print& nbsp;" Done "#测试程序if  __name__ ==  __main__":     rsync (' command.log.2015-07-13 ')


The above scripts are synchronous files and can also be modified under execute remote commands.

You can also change the host list, write in a configuration file, you can also group the host to achieve bulk management of remote host.

Most automated operations tools are similar in volume management, so you can implement your own remote management tools according to your needs.

The above script can also implement multi-threaded operation, this need to explore their own, I have realized here.

This article is from the Linux learning blog, so be sure to keep this source http://zhou123.blog.51cto.com/4355617/1747186

The pexpect of the Python module

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.