In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
1. Introduce pexpect
Through pexpect, we can automatically interact with ssh,ftp,passwd,telnet and other commands without human intervention.
2. Introduction to the components of pexpect
2.1, spawn class
Function: start and control the sub-application, and its constructor is as follows:
Class pexpect.spawn (command,args= [], timeout=30,maxread=2000,searchwindowsize=None,logfile=None,cwd=None,env=None,ignore_sighup=True)
Where command is any known system command, timeout is the timeout for waiting for the result, maxread is the maximum number of bytes read from the terminal at a time, and searchwindowsize is the position where the buffer string is matched.
Note: pexpect does not parse metacharacters (redirect >, pipe |, wildcard *) in shell commands, but calling commands with these three special metacharacters as arguments to / bin/bash can solve this problem. For example:
Child=pexpect.spawn ('/ bin/bash-c "ls-l | grep LOG > logs.txt"')
Child.expect (pexpect.EOF)
Replace the parameters of the command with a Python list:
Shell_cmd ='ls-l | grep LOG > logs.txt'
Child=pexpect.spawn ('/ bin/bash', ['- Clearing Magnum shellflower cmd])
Child.expect (pexpect.EOF)
The debug code is that pexpect provides two ways to view input and output: 1 > write to log 2 > output to stdout
Child=pexpect.spawn ('some_cmd')
Fout=file ('mylog.txt','w')
Child.logfile = fout
Or: child.logfile=sys.stdout
The expect method defines a matching rule for the output of a subroutine:
The defined method is expect (pattern,timeout=-1,searchwindowsize=-1)
Pattern represents a string, pexpect.EOF (pointing to the end of the buffer), pexpect.TIMEOUT, and regular expressions.
Send sends the corresponding command to the subroutine, which is equivalent to the standard input keyboard
Send (self,s) sends a command and does not enter
Sendline (self,s='') sends commands, enter
Sendcontrol (self,char) send control characters, such as child.sendcontrol ('c') is equivalent to ctrl+c
Sendeof () sends eof
2.2 > run function
Run is a function of external commands encapsulated with pexpect
Run (command,timeout=-1,withexitstatus=False,events=None,extra_args=None,logfile=None,cwd=None,env=None)
Import pexpect
Pexpect.run ('scp filename user@ip:~',events= {' password':mypassword})
3 、 pxssh
Pxssh encapsulates ssh session operations, providing a more direct mode of operation.
Pxssh common methods: login () establish ssh link, logout () disconnect, prompt () wait for the system prompt, the user waits for the command to finish execution.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 272
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.