pexpect

Transfer file(scp) and run command from jumpserver to finalserver using python script on localhost

大兔子大兔子 提交于 2019-12-12 01:29:39
问题 A file needs to be uploaded to final-server and a command needs to be run using that file as input. Following will be the steps for whole action to be performed (but I am stuck at step 3): 1 - transfer file from localhost to jumpserver (using scp) 2 - ssh to jumpserver 3 - transfer file from jumpserver to finalserver (using scp) 4 - ssh to finalserver 5 - run the command on finalserver (with file as input) try: cmd_str = "scp " + file_path + " " + user + "@" + jumpbox + ":/tmp/" print "------

Without exiting from the ssh_tunnel, open new terminal

好久不见. 提交于 2019-12-12 01:12:51
问题 I am using Python and wxpython for gui. I am trying to connect ssh tunnel. After connecting to ssh, wants a new terminal to open and have to continue my operation in local machine. How to achieve this? I tried subprocess, pexpect and paramiko, but all are capable to connect to ssh but not open the new teminal Below my code is there which I tried with pexpect: import time import sys import pexpect c = pexpect.spawn("ssh -Y -L xxxx:localhost:xxxx user @ host.com") time.sleep(0.1) c.expect("[pP

How do I make a command to run in background using pexpect.spawn?

可紊 提交于 2019-12-11 23:34:07
问题 I have a code snippet in which I run a command in background: from sys import stdout,exit import pexpect try: child=pexpect.spawn("./gmapPlayerCore.r &") except: print "Replaytool Execution Failed!!" exit(1) child.timeout=Timeout child.logfile=stdout status=child.expect(["[nN]o [sS]uch [fF]ile","",pexpect.TIMEOUT,pexpect.EOF]) if status==0: print "gmapPlayerCore file is missing" exit(1) elif status==1: print "Starting ReplayTool!!!" else: print "Timed out!!" Here, after the script exits the

python3.6 PEXPECT is not writing logs to a logfile

混江龙づ霸主 提交于 2019-12-11 19:19:58
问题 I am trying to log pexpect logs to a file. The code was working in python2.7 but logs not getting printed in python3.6 import pexpect child = pexpect.spawn("telnet IP") fout = open("abc.txt", "wb"): child.logfile = fout child.sendlines("somecommand) 回答1: It's a bit hard to believe that exactly this code was working in Python 2.7 ;) Your contextmanager exits right after child.logfile = fout completes, hence your file handle is closed when your child process tries to write to it afterwards. You

python ssh in to a jumpserver and then ssh in to a host from the jumpserver to execute a command

若如初见. 提交于 2019-12-11 15:55:27
问题 I want SSH to a jumpserver. From the jumpserver, I want SSH to a host and execute a command: 'rpm -qa | grep package' and get the output. Once done, I want successfully logout of the host first and then logout of the jump server. Below is what I have tried thus far: Import pexpect options = '-q -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null -oPubkeyAuthentication=no' child = pexpect.spawn('ssh ssheth@jumpserver %s'%(options), timeout=None) child.expect(['password: ']) child

pexpect equivalent of Expect's “send_user”

梦想与她 提交于 2019-12-11 14:56:01
问题 As a continuation from my previous question on building interactive option menus in pexpect, print statements within an interact input filter do not get sent to stdout until after the interact is complete. The docs don't seem to contain an equivalent method to send_user from expect, is there any workaround to send output to the user, not to the spawned child from within pexpect's interact method? bash-4.1$ cat testInputFilter.py import pexpect def input_filter(s): if s == b'\003': print('you

pexpect and sending an “Enter Key” issues

橙三吉。 提交于 2019-12-11 12:17:59
问题 I am using pexpect with python 2.7. I am currently writing a script to login to a jump server and then subsequently login to some cisco devices to perform some operations in an automated way. There is a stage during the login process, where I need to send just a blank line by pressing 'Enter/Return' on the keyboard. After reading many of the posts and other forums, I have tried the following: <handler>.sendline() <handler>.sendline('') <handler>.send('\013') However, none of these seem to

Can pexpect be told to ignore a pattern or signal?

浪子不回头ぞ 提交于 2019-12-11 10:33:02
问题 I'm trying to write a python script to access several Cisco network devices. Sadly the devices are not set up the same, some have banners up that appear at weird places. These banners might have the patterns that have some of the same stuff a prompt might have. For example, once I log in I expect a prompt of 'hostname#' , so I basically use a pexpect('#') I might have to handle certain other prompts such as 'hostname>' as well so pexpect('>') works. I'll really have a list of possible prompts

Running wexpect on windows

落爺英雄遲暮 提交于 2019-12-11 09:45:58
问题 I have installed wexpect on Windows 7. Now, when I am trying to run any command, I am getting the below error. I am using MKS toolkit, so ls is a valid command. >>> import pexpect >>> pexpect.run('ls ') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python27\lib\site-packages\winpexpect-1.5-py2.7.egg\pexpect.py", line 219, in run child = spawn(command, maxread=2000, logfile=logfile, cwd=cwd, env=env) File "C:\Python27\lib\site-packages\winpexpect-1.5-py2.7.egg

Need little assistance with pexpect interaction

≯℡__Kan透↙ 提交于 2019-12-11 09:32:27
问题 Need little assistance with making this code work with pexpect module. This code executes git pull by logging into a server and then downloads the latest code (if upgrade is available) or just sends out a messages saying "Already up-to-date." The code actually idenfies the password screen but does not identify the text "already up-to-date" not sure if I'm missing anything here. A snippet from the code is: p = pexpect.spawn('git pull',cwd = comp_dir,maxread = 10000, timeout = 100) i = p.expect