pexpect

how to use pexpect to get spontaneous output of subprocess in python

我们两清 提交于 2019-12-03 16:13:42
This is related to my another post multithreading issue with wx.TextCtrl (or underlying GTK+) , which after correction with calling GUI interactions from primary thread, I find it again comes to the pipe block buffering problem. so How to get spontaneous output from the subprocess.stdout? To be in short, currently I am using subprocess.popen to launch an external long-time running program. launchcmd=["EXTERNAL_PROGRAM_EXE"] p = subprocess.Popen(launchcmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) self.outputThread = BashProcessThread(p.stdout.readline) self

execute a shell-script from Python subprocess

牧云@^-^@ 提交于 2019-12-03 10:04:22
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! Using the subprocess library, you can tell the Popen class that you want to manage the standard input of the process like this: import subprocess shellscript = subprocess.Popen(["shellscript.sh"], stdin=subprocess.PIPE) Now shellscript

fabric vs pexpect

牧云@^-^@ 提交于 2019-12-03 06:13:34
I've stumbled upon pexpect and my impression is that it looks roughly similar to fabric . I've tried to find some comparison, without success, so I'm asking here--in case someone has experience with both tools. Is my impression (that they are roughly equivalent) correct, or it's just how it looks on the surface ? Jon I've used both. Fabric is more high level than pexpect, and IMHO a lot better. It depends what you're using it for, but if your use is deployment and configuration of software then Fabric is the right way to go. Jasper van den Bosch You can also combine them, to have the best of

python libraries for ssh handling

人盡茶涼 提交于 2019-12-03 03:11:43
问题 I'm going to write first code for handling ssh commands on python and I did search over the stackoverflow and can see that there are several python libraries that can be used for handling commands passed through ssh, like paramiko, pexpect and perhaps some others. Particularly, I will need to read content of the files from the remote server, copy files through ssh/scp, get output from remote server after starting the script on remote server. Perhaps some experts could advice what library is

pexpect child.before and child.after is empty

匿名 (未验证) 提交于 2019-12-03 01:45:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: >>> 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 a pattern or a list of patterns. These patterns

How to see the output in pexpect?

匿名 (未验证) 提交于 2019-12-03 01:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have write this program: [mik@mikypc ~]$ cat ftp.py #!/usr/bin/env python # This connects to the rediris ftp site # import pexpect child = pexpect.spawn('ftp ftp.rediris.es') child.expect('Name .*: ') child.sendline('anonymous') child.expect('ftp> ') child.sendline('noah@example.com') child.expect('ftp> ') child.sendline('lcd /tmp') child.expect('ftp> ') child.sendline('pwd') child.expect('ftp> ') child.sendline('bye') [mik@mikypc ~]$ ./ftp.py [mik@mikypc ~]$ [mik@mikypc ~]$ [mik@mikypc ~]$ But I cannot see the output. How could I see it?.

How to login the super user(root) in remote host system using pexpect?

心不动则不痛 提交于 2019-12-02 19:30:58
问题 How to login the super user(root) in remote host system using pexpect? user = root user password = 'pass' child = pexpect.spawn('ssh %s@%s'%(user,host,)) 回答1: You could also simply log in to a user on ssh like normal, then send commands to log into the root like you normally would in a terminal. #log into user account child = pexpect.spawn('ssh clientuser@localhost') child.expect('Password:') child.sendline('password') #then log into root account child.sendline('su') child.expect('Password:')

运维自动化部署

强颜欢笑 提交于 2019-12-02 16:09:35
运维自动化部署 pexpect fabric 安装 入门示例 fabric常用参数 fabric常用API fabric全局属性设定 示例1: 动态获取远程目录列表 示例2: 上传文件并执行 代码自动化部署 pexpect Pexpect 是 Don Libes 的 Expect 语言的一个 Python 实现,是一个用来启动子程序,并使用正则表达式对程序输出做出特定响应,以此实现与其自动交互的 Python 模块。 Pexpect 的使用范围很广,可以用来实现与 ssh、ftp 、telnet 等程序的自动交互;可以用来自动复制软件安装包并在不同机器自动安装;还可以用来实现软件测试中与命令行交互的自动化。 import pexpect import sys child = pexpect.spawn('ssh std20@123.57.211.212') child.logfile = sys.stdout #fout = file('mylog.txt', 'w') #child.logfile = fout child.expect('password:') child.sendline('std20') child.expect('std20.*') child.sendline('ls /') child.expect('std20.*') child.sendline(

How to login the super user(root) in remote host system using pexpect?

。_饼干妹妹 提交于 2019-12-02 09:16:27
How to login the super user(root) in remote host system using pexpect? user = root user password = 'pass' child = pexpect.spawn('ssh %s@%s'%(user,host,)) You could also simply log in to a user on ssh like normal, then send commands to log into the root like you normally would in a terminal. #log into user account child = pexpect.spawn('ssh clientuser@localhost') child.expect('Password:') child.sendline('password') #then log into root account child.sendline('su') child.expect('Password:') child.sendline('sudopassword1234') This is just longer and more code, and probably only works on linux the

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

若如初见. 提交于 2019-12-02 04:42:55
问题 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