No output in output file even though pexpect scrift runs succesfully

0 votes

I am trying to add the results of ls/home into mylogfile.txt through ssh. If I run the script there is no error but there is no output in mylogfile.txt.

#!/usr/bin/env python
import pexpect
import sys

child=pexpect.spawn('ssh anony@192.168.42.2')
fout=file('mylogfile.txt','w')
child.logfile=fout

child.expect("password:")
child.sendline("xxxxx")
child.expect('$')
child.sendline('ls /home')
anony:pythontest/ $ cat mylogfile.txt                                                                                    
anony@192.168.42.2's password: xxxxxxx 
ls /home

Why's there only tow commands in mylogfile.txt?

Aug 10, 2018 in Docker by ffdfd
• 5,550 points
3,382 views

1 answer to this question.

0 votes

You've got to wait til the ls command finishes execution, same as when you run it on the terminal. Here's an example for you(I'm using ssh that's why there's no password prompt):

[STEP 106] # cat foo.py
import pexpect

shell_prompt = 'bash-[.0-9]+[$#] '

ssh = pexpect.spawn('ssh -t 127.0.0.1 bash --noprofile --norc')
ofile = file('file.out', 'w')
ssh.logfile_read = ofile

ssh.expect(shell_prompt)

ssh.sendline('echo hello world')
ssh.expect(shell_prompt)

ssh.sendline('exit')
ssh.expect(pexpect.EOF)
[STEP 107] # python foo.py
[STEP 108] # cat file.out
bash-4.3# echo hello world
hello world
bash-4.3# exit
exit
Connection to 127.0.0.1 closed.
[STEP 109] #
answered Aug 10, 2018 by DareDev
• 6,890 points

Related Questions In Docker

0 votes
1 answer
0 votes
1 answer

Different file owner inside Docker container and in host machine Ask

Filesystems, at least in Unix- and Linux-like ...READ MORE

answered Jun 25, 2018 in Docker by Damon Salvatore
• 5,980 points
717 views
0 votes
1 answer

Different file owner inside Docker container and in host machine

Here is what you can try. Since ...READ MORE

answered Jun 27, 2018 in Docker by Atul
• 10,240 points
1,717 views
0 votes
7 answers

docker: executable file not found in $PATH

try to build the image with --no-cache. I ...READ MORE

answered Nov 1, 2020 in Docker by Deeptesh Agrawal
61,060 views
+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
3,460 views
+2 votes
1 answer
+4 votes
7 answers

If conditional in docker file

First of all, create a build_internal.sh file ...READ MORE

answered May 29, 2018 in Docker by DareDev
• 6,890 points
119,831 views
0 votes
1 answer

IBM Cloud:Cannot filter work items in DevOps Track and Plan in

When you search with the # the ...READ MORE

answered Aug 21, 2018 in Docker by DareDev
• 6,890 points
397 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP