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 9, 2018 in Docker by Atul
• 5,470 points
108 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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 9, 2018 by DareDev
• 6,520 points

Related Questions In Docker

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,250 points
36 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,170 points
82 views
0 votes
6 answers

docker: executable file not found in $PATH

file not found in $PATH READ MORE

answered May 9 in Docker by anonymous
4,584 views
0 votes
2 answers
+13 votes
2 answers
0 votes
1 answer
+2 votes
6 answers

If conditional in docker file

You can use the test command RUN test ...READ MORE

answered Dec 10, 2018 in Docker by Shushant
7,301 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,520 points
30 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.