pexpect

Using expect() and interact() simultaneously in pexpect

半腔热情 提交于 2019-12-21 06:29:16
问题 The general problem is, that I want to use pexpect to call scripts that require sudo rights, but I don't always want to enter my password (only once). My plan is to use pexpect to spawn a bash session with sudo rights and to call scripts from there. Basically I always want to keep the session busy, whenever one script stopped, I want to start another. But while the scripts are running, I want the user to have control. Meaning: The scripts should be called after something like expect("root@"),

execute a shell-script from Python subprocess

核能气质少年 提交于 2019-12-21 02:59:25
问题 I need to call a shellscript from python. The problem is that the shellscript will ask a couple of questions along the way until it is finished. I can't find a way to do so using subprocess ! (using pexpect seems a bit over-kill since I only need to start it and send a couple of YES to it) PLEASE don't suggest ways that requires modification to the shell-script! 回答1: Using the subprocess library, you can tell the Popen class that you want to manage the standard input of the process like this:

python, set terminal type in pexpect

谁说胖子不能爱 提交于 2019-12-18 06:54:43
问题 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 "----

python, set terminal type in pexpect

喜夏-厌秋 提交于 2019-12-18 06:52:09
问题 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 "----

ping for indefinite amount of time and get its output in Python

荒凉一梦 提交于 2019-12-17 21:17:34
问题 The task is: Try to send ping in python using the most basic form like "ping 8.8.8.8". After some time terminate the ping command (In a terminal, one will do Ctrl+C) and get its output. The last several lines of output which shows the ping statistics are of particular interest. Two methods tried, did not work. My OS version is Mac OS X 10.10.1. First method uses module pexpect, and ping will stop after about 17 seconds though I did not ask it to stop: import pexpect import time child =

python3 module 'pexpect' has no attribute 'spawn' 解决方法

空扰寡人 提交于 2019-12-17 00:09:24
python 在使用 pexpect 的时候,报了 module 'pexpect' has no attribute 'spawn' 并不是”pexpect”没有“spanwn”模块,点进去看 pexpect 的源代码: import sys PY3 = (sys.version_info[0] >= 3) from .exceptions import ExceptionPexpect, EOF, TIMEOUT from .utils import split_command_line, which, is_executable_file from .expect import Expecter, searcher_re, searcher_string if sys.platform != 'win32': # On Unix, these are available at the top level for backwards compatibility from .pty_spawn import spawn, spawnu from .run import run, runu __version__ = '4.0.1' __revision__ = '' __all__ = ['ExceptionPexpect', 'EOF', 'TIMEOUT', 'spawn',

pexpect output not getting generated

烈酒焚心 提交于 2019-12-14 03:12:59
问题 I’m having a problem with pexpect not providing any output of the commands that it runs. I’ve tried a variety of methods including installing we expect but there is no change in behavior. For example, when I do this: #!/usr/bin/python import sys, pexpect import os, subprocess, signal process = pexpect.spawn('/bin/bash') process.sendline("export PS1=\"checkout-script:\"\r") process.sendline("make\r") process.expect("checkout-script") process.sendline("echo $?\r") process.expect("checkout

How to get sentiment via stanford corenlp interactive shell?

ⅰ亾dé卋堺 提交于 2019-12-13 18:47:19
问题 I have been trying to get the sentiment value from the stanford corenlp , but it seems in the interactive shell, the sentiment is not given as an output. I have specified the annotators using the command given in the official website. java -cp "*" -Xmx3g edu.stanford.nlp.pipeline.StanfordCoreNLP -annotators tokenize,ssplit,pos,lemma,ner,parse,dcoref, sentiment Also, when i tried for getting just the sentiment, then at first asked for other annotators, after providing i didn't give any output

SSH persistent connection using pxssh in flask

杀马特。学长 韩版系。学妹 提交于 2019-12-13 07:16:53
问题 I am working on web interface to run command on remote servers using pxssh. Is there anyway I can create persistent ssh connection, below code calls connect_ssh for every request. from flask import Flask, jsonify, render_template, request, g, current_app from flask import _app_ctx_stack from pexpect import pxssh app = Flask(__name__) def connect_ssh(): s = pxssh.pxssh() s.login('localhost', 'user', 'passwd') return s def get_ssh(): top = _app_ctx_stack.top if not hasattr(top, 'ssh_conn'): top

pexpect child.before and child.after is empty

拜拜、爱过 提交于 2019-12-12 01:38:00
问题 >>> ssh_stuff ['yes/no', 'Password:', 'password', 'Are you sure you want to continue connecting'] >>> prompt ['$', '#'] >>> child = pexpect.spawn('ssh kumarshubham@localhost') >>> child.expect(ssh_stuff) 1 >>> child.sendline(getpass.getpass()) Password: 11 >>> child.expect(prompt) 0 >>> child.sendline('ls -l') 6 >>> child.expect(prompt) 0 >>> print child.before, child.after can one tell me why my child.before and after is empty, it should return directory listing. 回答1: The expect method takes