Python pexpect scripts run without error,but there are no outputs in output file

garenwang

I want to put the results of 'ls /home' into mylog1.txt through ssh.So,I can check it on my computer.When I run the script,there is no error,there is no output in mylog1.txt。

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

child=pexpect.spawn('ssh [email protected]')
fout=file('mylog1.txt','w')
child.logfile=fout

child.expect("password:")
child.sendline("xxxxx")
child.expect('$')
child.sendline('ls /home')
shiyanlou:pythontest/ $ cat mylog1.txt                                                                                    
[email protected]'s password: xxxxxxx 
ls /home

There are just tow commands in the mylog1.txt file.Why?

pynexj

You have to wait until the ls command finishes, just like when you are interacting with the terminal. See following example (I'm using public key auth for ssh so 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] #

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Run Python scripts without explicitly invoking `python`

From Dev

Getting error "NameError: name 'ExceptionPexpect' is not defined" while using Pexpect in my Python scripts

From Dev

run python script with and without output

From Dev

Python Append From Text file outputs error

From Dev

Run python file and read output

From Dev

Running powershell scripts from Python without reimporting modules on every run

From Dev

Python - Run multiple python scripts from main file

From Dev

Run Perl code (with output to file) from Python

From Dev

Run php scripts without crontab

From Dev

Run php scripts without crontab

From Dev

Spyder in python will not run scripts

From Dev

How to run many PHP scripts parallel and redirect output of each script to a file?

From Dev

Parsing pexpect output

From Dev

How to run the bash scripts file?

From Dev

Writing all outputs to a file (Python)

From Dev

FFMPEG outputs a video file without its audio

From Dev

Python: Error saving output into csv file?

From Dev

Python: Error saving output into csv file?

From Dev

Syntax error for output to a file in python 3

From Dev

EXE file for python scripts

From Dev

EXE file for python scripts

From Dev

Error running python scripts

From Dev

Run multiple python scripts concurrently

From Dev

Python: run bpy scripts on Windows

From Dev

failed to run python scripts with jenkins

From Dev

Run Python scripts in PowerShell directly

From Dev

How can I run my python scripts on a windows machine without installing extra libraries?

From Dev

python: run external program and direct output to file and wait for finish

From Dev

Django Run Python script and Pass output to javascript file

Related Related

HotTag

Archive