pexpect

How can I send single ssh command to get result string with pexpect?

荒凉一梦 提交于 2019-12-08 11:14:42
问题 I am trying to make my code as efficient and simple as possible. I would like to make my pexpect command into 1 line: Current command (simplified): import pexpect ... session=pexpect.spawn( 'ssh %s@%s'%(un,ip), timeout) session.expect(prompt, timeout) session.sendline('ls') session.expect(prompt) print session.before I would like to do this all in my ssh command but I am returning a pexpect object, is there a way to return just the output string? import pexpect ... print str(pexpect.spawn(

How do I set the column width of a pexpect ssh session?

被刻印的时光 ゝ 提交于 2019-12-08 09:33:51
问题 I am writing a simple python script to connect to a SAN via SSH, run a set of commands. Ultimately each command will be logged to a separate log along with a timestamp, and then exit. This is because the device we are connecting to doesn't support certificate ssh connections, and doesn't have decent logging capabilities on its current firmware revision. The issue that I seem to be running into is that the SSH session that is created seems to be limited to 78 characters wide. The results

automating shell script to login vpn passing sudo -S

删除回忆录丶 提交于 2019-12-08 09:16:33
问题 I have to re-login to my VPN every time I leave my desk, and it is tedious. I am trying to pass the shell the info but it doesn't get it in the right order. The order is "try to openconnect, enter sudo pw if needed, then username, then password". pexpect would be good, since it can tell if you need your sudo password or not, but isn't working: #!/usr/bin/env python # coding: utf-8 import os, sys, subprocess, time, re, pexpect from my_scripting_library import * child = pexpect.spawn('sudo

Creating interactive options in pexpect

岁酱吖の 提交于 2019-12-08 03:34:43
问题 This is sort of a clunky question as I can't figure out a good way to describe it, but in expect you can do something like this: interact { \001 {do_something} \003 {do_something_else} "?" { set timeout 1 expect_user { "?" {send "?"} timeout {send_user "show a menu of the things you can do"} } stty raw -echo set timeout 60 } \035 {send "^]" send "quit\r" send_user "\n" exit } } which would create an interactive session where the user could go about business as usual, but upon pressing

Catching a dying process in pexpect

折月煮酒 提交于 2019-12-07 13:14:17
问题 I'm writing some pexpect stuff that's basically sending commands over telnet. But, it's possible that my telnet session could die (due to networking problems, a cable getting pulled, whatnot). How do I initialize a telnet session such that, if it dies, I can catch it and tell it to reconnect and then continue execution of the code where it was at. Is this possible? 回答1: IMHO, you're normally better-off with a currently-maintained library like exscript or telnetlib, but the efficient

How to create and use multiple pipes within the same process with pexpect?

折月煮酒 提交于 2019-12-06 11:28:23
I'm trying to communicate with gdb asynchronously using pexpect. If I use the same pipe to do it, the commands sent using pexpect's sendline() function gets mixed into each other. And if I synchronize it like this: def send_command(str): global p with GDB_Engine.lock: p.sendline(str) p.expect_exact("(gdb)") It'll be too slow since there'll be tons of commands coming thru. So the thing I want to do is to implement different pipes for every send_command() block then close it when the work finishes. By this way, the text generated by sendline() commands won't get mixed into each other and I'll

pexpect and ssh: how to format a string of commands after su - root -c

独自空忆成欢 提交于 2019-12-06 03:01:12
I am trying to iterate through a list of servers & passwords to change the sshd configs on a group of servers so that I can login/run commands via root using passwordless SSH keys. I can do this easily in bash but I'm trying to learn Python and (obviously) would like to forego entering in passwords manually. Here's the bash of what I want to do: scp ~/.ssh/id_rsa.pub /etc/ssh/sshd_config USER@IP:/tmp/ ssh -o StrictHostKeyChecking=no -t USER@IP "su - root -c \"chown root:root /tmp/id_rsa.pub; chmod 600 /tmp/id_rsa.pub; chown root:root /tmp/sshd_config; mkdir /root/.ssh; chown root:root /root/

python和linux命令交互方式总结

筅森魡賤 提交于 2019-12-05 22:34:51
python和linux命令交互有两种方式: 1. 直接使用os执行命令 os.system("armory -leg %s"%(host)) 这种方式的问题在于命令直接输出到console中了,无法定制。 2.使用pexpect import pexpect child = pexpect.spawn ('armory', ['-leg', host]) child.expect([pexpect.EOF,pexpect.TIMEOUT]) hosts=child.before.split("\r\n") hosts = [host for host in hosts if host !=""] print ",".join(hosts) 3. 貌似还可以使用os.popen lines = os.popen("netstat -an|grep ':3306' |awk '{print $5, $6}'").readlines() 更多参考文档: http://my.oschina.net/renwofei423/blog/17403 来源: CSDN 作者: san_yun 链接: https://blog.csdn.net/SAN_YUN/article/details/84518672

Windows alternative to pexpect

不羁岁月 提交于 2019-12-05 21:01:41
问题 I'm trying to write a cross-platform tool that runs specific commands, expects certain output for verification, and sends certain output (like username/password) for authentication. On Unix, I have been successful in programming a Python tool that uses the pexpect library (via pip install pexpect ). This code works perfectly and is exactly what I am trying to do. I've provided a small excerpt of my code for proof-of-concept below: self.process = pexpect.spawn('/usr/bin/ctf', env={'HOME'

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

和自甴很熟 提交于 2019-12-05 01:23:01
问题 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