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,530 points
199 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 9, 2018 by DareDev
• 6,810 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,510 points
66 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
151 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
7,646 views
0 votes
2 answers

How do I fix the “no space left on device” error in docker?

Try cleaning up: $ docker volume rm $(docker ...READ MORE

answered 6 days ago in Docker by Sirajul
• 17,540 points
1,253 views
+13 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 26, 2018 in DevOps & Agile by DragonLord999
• 8,380 points
112 views
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
12,007 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,810 points
34 views