pexpect

How can pytest-cov report coverage of python code that is executed as a result of pexpect.spawn?

﹥>﹥吖頭↗ 提交于 2019-12-02 02:16:09
问题 I have a Python project that uses pytest-cov for unit testing and code coverage measurement. The directory structure for my project is: rift-python +- rift # The package under test | +- __init__.py | +- __main__.py | +- cli_listen_handler.py | +- cli_session_handler.py | +- table.py | +- ...lots more... +- tests # The tests | +- test_table.py | +- test_sys_2n_l0_l1.py | +- ...more... +- README.md +- .travis.yml +- ... I use Travis to run pytest --cov=rift tests for every checkin, and I use

pxssh throwing End Of File (EOF). Exception style platform exception

眉间皱痕 提交于 2019-12-02 01:11:41
I am having problem using pxssh module. My code is below: try: ssh_handle = pxssh.pxssh(timeout=None) ssh_handle.logfile = sys.stdout ssh_handle.login(host, username, password) index = ssh_handle.expect(['Are you sure you want to continue connecting \(yes\/no\)\? ', '.*?password:.*', '.*?\$.*']) if index == 0: ssh_handle.sendline('yes') ssh_handle.sendline(password) if index == 1: ssh_handle.sendline(password) ssh_handle.sendline('sudo -s') ssh_handle.sendline(password) return ssh_handle except pxssh.ExceptionPxssh as e: print "SSH connection to %s failed" % host sys.exit() For some reason I

How can pytest-cov report coverage of python code that is executed as a result of pexpect.spawn?

流过昼夜 提交于 2019-12-01 22:25:45
I have a Python project that uses pytest-cov for unit testing and code coverage measurement. The directory structure for my project is: rift-python +- rift # The package under test | +- __init__.py | +- __main__.py | +- cli_listen_handler.py | +- cli_session_handler.py | +- table.py | +- ...lots more... +- tests # The tests | +- test_table.py | +- test_sys_2n_l0_l1.py | +- ...more... +- README.md +- .travis.yml +- ... I use Travis to run pytest --cov=rift tests for every checkin, and I use codecov to view the code coverage results. The package under test offer a command line interface (CLI)

Python - Pxssh - Getting an password refused error when trying to login to a remote server

白昼怎懂夜的黑 提交于 2019-12-01 11:42:44
问题 I'm trying to use the pexpect module pxssh to log in into my one of my server. I get password refused. I think I know what it is the problem, but can't figure out how to fix it. The issue is that there is a welcome banner when I log in into the server(Changing the banner is not an option) and the pexpect get confuse. Here is my code: import pxssh ssh = pxssh.pxssh() ssh.login('192.168.1.10', 'username', 'password') I'm expecting a 'password:', but pxssh is expecting original_prompt='[#$]'and

Grabbing the output of MAPLE via Python

只谈情不闲聊 提交于 2019-12-01 05:54:32
问题 How would I use the subprocess module in Python to start a command line instance of MAPLE to feed and return output to the main code? For example I'd like: X = '1+1;' print MAPLE(X) To return the value of "2". The best I've seen is a SAGE wrapper around the MAPLE commands, but I'd like to not install and use the overhead of SAGE for my purposes. 回答1: Trying to drive a subprocess "interactively" more often than not runs into issues with the subprocess doing some buffering, which blocks things.

Verify a file exists over ssh

馋奶兔 提交于 2019-11-30 14:51:56
I am trying to test if a file exists over SSH using pexpect. I have got most of the code working but I need to catch the value so I can assert whether the file exists. The code I have done is below: def VersionID(): ssh_newkey = 'Are you sure you want to continue connecting' # my ssh command line p=pexpect.spawn('ssh service@10.10.0.0') i=p.expect([ssh_newkey,'password:',pexpect.EOF]) if i==0: p.sendline('yes') i=p.expect([ssh_newkey,'password:',pexpect.EOF]) if i==1: p.sendline("word") i=p.expect('service@main-:') p.sendline("cd /opt/ad/bin") i=p.expect('service@main-:') p.sendline('[ -f

pexpect模块

梦想的初衷 提交于 2019-11-30 04:31:33
pexpect模块 自动交互式模块 vim test.py #!/usr/bin/python3 import pexpect ssh = pexpect.spawn('ssh 192.168.132.121') res = ssh.expect(['password','(yes/no)',pexpect.EOF,pexpect.TIMEOUT],timeout=10) print(res) if res == 0: ssh.sendline('2430') ssh.expect('#') ssh.sendline('ifconfig') elif res == 1: ssh.sendline('yes') ssh.expect('password') ssh.sendline('2430') ssh.expect('#') ssh.sendline('ip a') elif res == 2: print('error') else: print('time out') ssh.interact() 执行文件: 方法1: python3 test.py 方法2: chmod 777 test.py ./test.py(test.py在当前目录下) 注意: ² pexpect.spawn:当前主机输入命令 ² ssh.expect:对命令的输出作内容匹配,会返回一个索引值

EOF when using pexpect and pxssh

£可爱£侵袭症+ 提交于 2019-11-29 14:16:45
I'm trying to run the code in the Interacting with SSH Through Pexpect and Brute Forcing SSH Passwords with Pxssh sections from Chapter 2 of Violent Python . Using both child.expect() and pxssh I get similar EOF errors. Running these commands from the Python console: import pexpect connStr = "ssh root@127.0.0.1" child = pexpect.spawn(connStr) ret = child.expect([pexpect.TIMEOUT, ssh_newkey, "[P|p]assword:"]) I get this output: Traceback (most recent call last): File "<input>", line 1, in <module> File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pexpect.py",

python, set terminal type in pexpect

时光怂恿深爱的人放手 提交于 2019-11-29 11:09:54
I have a script which uses pexpect to start a CLI program. It works a bit like a shell where you get a prompt where you can enter some commands. The problem I have, I think, is that this program uses a coloured prompt. This is what I do import pprint import pexpect 1 a = pexpect.spawn('program') 2 a.expect("prompt>") 3 print "---------start------------" 4 print(a.before) 5 a.sendline("command") 6 a.expect("prompt>") 7 print "---------before------------" 8 pprint.pprint(a.before) 9 print "---------after------------" 10 pprint.pprint(a.after) This is the output: > python borken.py ---------start

Python how to read output from pexpect child?

半腔热情 提交于 2019-11-29 05:34:54
问题 child = pexpect.spawn ('/bin/bash') child.sendline('ls') print(child.readline()) print child.before, child.after All I get with this code in my output is ls ls But when my code is child = pexpect.spawn('ls') print(child.readline()) print child.before, child.after Then it works, but only for the first 2 prints. Am I using the wrong send command? I tried send, write, sendline, and couldn't find anymore. 回答1: In pexpect the before and after attributes are populated after an expect method. The