html tool

显示标签为“pexpect”的博文。显示所有博文
显示标签为“pexpect”的博文。显示所有博文

2020年11月10日星期二

pexpect-spawn-sys.std.out

 https://www.jianshu.com/p/cfd163200d12


spawn() 参数:

logfile - 运行输出控制

默认值: None

当给 logfile 参数指定了一个文件句柄时,所有从标准输入和标准输出获得的内容都会写入这个文件中(注意这个写入是 copy 方式的),如果指定了文件句柄,那么每次向程序发送指令(process.send)都会刷新这个文件(flush)。

这里有一个很重要的技巧:如果你想看到spawn过程中的输出,那么可以将这些输出写入到 sys.stdout 里去,比如:

process = pexpect.spawn("ftp sw-tftp", logfile=sys.stdout)

用这样的方式可以看到整个程序执行期间的输入和输出,很适合调试。


2017年11月20日星期一

pexpect send tab && Enter




linux shell setup mysql 有提示字符拼装的GUI输入如下:


pexpect 使用要求移动光标,确认使用 tab 和 回车 key。

测试了一下,使用
 m.expect("password.*root", timeout= -1)
 35     m.send("password")
 36     time.sleep(0.01)
 37     m.send('\033[B')  # down
 38     time.sleep(0.01)
 39     m.send('\r\n')      #回车换行符


参考:
https://stackoverflow.com/questions/29217645/pexpect-and-sending-an-enter-key-issues

https://stackoverflow.com/questions/12981982/pexpect-send-cursor-movement

2017年11月19日星期日

pexpect log flush

https://www.ibm.com/developerworks/cn/linux/l-cn-pexpect2/index.html

清单 3. log 内容为空的例子代码
1
2
3
4
5
import pexpect
p = pexpect.spawn( ‘ ls -l ’ )
fout = open ('log.txt', "w")
p.logfile = fout
fout.close()

运行该脚本后,你会发现其实 log.txt 是空的,没有记录 ls -l 命令的内容,原因是没有调用 send 或 read_nonblocking,真正的内容没有被 flush 到 log 中。如果在 fout.close() 之前加上 p.expect(pexpect.EOF),log.txt 才会有 ls -l 命令的内容。

pexpect use cd

https://stackoverflow.com/questions/11289602/a-new-bie-clarification-on-pexpect-module

cd is a builtin function of your shell. You can run whatever command you want to do in whatever directory you want to using the run() methods cwd keyword arg:
pexpect.run("pwd", cwd="/home")
For more information check the API docs at: http://pexpect.sourceforge.net/pexpect.html
Alternatively you can use Python's os.chdir() function to change the current working directory before you execute the pexpect run method:
os.chdir("/home")
pexpect.run("pwd")

[?]
>>> pexpect.run('pwd')
'/home/vijay\r\n'
>>> pexpect.run('cd /home')
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/pexpect.py", line 219, in run
    child = spawn(command, maxread=2000, logfile=logfile, cwd=cwd, env=env)
  File "/usr/lib/python2.7/dist-packages/pexpect.py", line 429, in __init__
    self._spawn (command, args)
  File "/usr/lib/python2.7/dist-packages/pexpect.py", line 516, in _spawn
    raise ExceptionPexpect ('The command was not found or was not executable: %s.' % self.command)
pexpect.ExceptionPexpect: The command was not found or was not executable: cd.
>>> 

2016年9月6日星期二

pexpect.expect中正则使用

问题:

在sh运行使用对异常的处理如何监控

解决方式:
class sh_test():                        
    def __init__(self):                 
        cmd = "python ter.py"           
        self.pe = pexpect.spawn(cmd)    
                                        
    def t_doing(self):                  
        s1 = "here_10"                  
        s2 = "hi_50"                    
        print "start expect"            
        x = self.pe.expect([s2,s1])    -----------------1 
        self.pe.kill(0)                 
        print "********************"    
        print "before %s" % str(self.pe.before)
        print "after %s" % str(self.pe.after)
        if s1 == self.pe.after :        
            print "s1[%s] is first" % s1

其中1的参考:https://www.ibm.com/developerworks/cn/linux/l-cn-pexpect2/

expect() 中正则表达式的使用 tips

expect() 中的正则表达式不是贪婪匹配 greedy match,而是最小匹配,即只匹配缓冲区中最早出现的第一个字符串。 因为是依次读取一个字符的 stream 流来判断是否和正则表达式所表达的模式匹配,所以如果参数 pattern 是个 list,而且不止一次匹配,那么缓冲区中最早出现的第一个匹配的字符串才算数。
清单 14. expect() 的最小匹配例子代码
# 如果输入是 'foobar'
index = p.expect (['bar', 'foo', 'foobar'])
#index 返回是 1 ('foo') 而不是 2 ('foobar'),即使 'foobar' 是个更好的匹配。原因是输入是个流 stream,
# 当收到 foo 时,第 1 个 pattern ('foo') 就被匹配了,不会等到’ bar ’的出现了,所以返回 1
  • “$”不起任何作用,匹配一行的结束 (end of line),必须得匹配 CR/LF
正则表达式中,'$'可以匹配一行的结束(具体'$'正则表达式的使用,请参阅参考资料),但是 pexpect 从子程序中一次只读取一个字符,而且每个字符都好像是一行的结束一样,pexpect 不能在子程序的输出流去预测。匹配一行结束的方法必须是匹配 "\r\n" (CR/LF) 。即使是 Unix 系统,也是匹配 "\r\n" (CR/LF),因为 pexpect 使用一个 Pseudo-TTY 设备与子程序通话,所以当子程序输出 "\n" 你仍然会在 python 主程序中看到 "\r\n" 。原因是 TTY 设备更像 windows 操作系统,每一行结束都有个 "\r\n" (CR/LF) 的组合,当你从 TTY 设备去解释一个 Unix 的命令时,你会发现真正的输出是 "\r\n" (CR/LF),一个 Unix 命令只会写入一个 linefeed (\n),但是 TTY 设备驱动会将其转换成 "\r\n" (CR/LF) 。
清单 15. 匹配一行结束 1
child.expect ('\r\n')

2016年8月29日星期一

pexpect调用git中cd目录问题解决

address:http://www.simonzhang.net/?p=2637

当前需要python调用git命令自动执行pull操作。因为git还需要输入密码,所以使用到pexpect模块。
最初写法
但是pexpect不能使用cd命令,报错如下:
pexpect.ExceptionPexpect: The command was not found or was not executable: cd.
只能先切换到相应目录中,然后在执行。修改代码如下,执行成功。
还有一点学习。cd为shell自带的命令,当用which和whereis查找时都会报找不到,因为命令是按照系统的PATH变量设置路径进行搜索。用type cd则显示cd为shell buildin命令。pexpect调用时并不加载shell,导致了该问题。