how to handle output generated after execution of command/script on host unix machine?

Dan Stromberg drsalists at gmail.com
Wed Dec 22 19:42:35 EST 2010


On Sun, Dec 19, 2010 at 11:38 PM, Darshak Bavishi
<bavishi.darshak at gmail.com> wrote:
> Hi Experts,
> I am still struggling with handling output generated after execution of
>  command/script on host unix machine using windows client machine
> ssh code :
> import sys
> import datetime
> import time
> # setup logging
> paramiko.util.log_to_file('darshak_simple.log')
> ssh=paramiko.SSHClient()
> ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
> ssh.connect("*****",username="****",password="****")
> try:
>     stdin,stdout,stderr=ssh.exec_command("BsPlSMProbe -f node -d >
> /var/log/Darshak/3.txt ") // output of this command will be store in
> /var/log/Darshak/ in remote machine
> except:
>                                                               {Issue is
> files are generating but remaining blank pls pls help me out of this}
>     print "check"
> time.sleep(10)
> print stdout.readlines()
> a=stdout.readlines()
> print 1
> ssh.close()
> #print stdout.readlines()
> Issue is files are generating but remaining blank pls pls help me out of
> this
> --
> BR
> Darshak Bavishi
>
> --
> BR
> Darshak Bavishi
>
> --
> http://mail.python.org/mailman/listinfo/python-list

I've never used paramiko, but I have done some python+ssh.

1) Are you wanting the output from BsPlSMProbe to go to
/var/log/Darshak/3.txt or your print stdout.readlines()?  If you need
both, you'd probably better get a tee in there.

2) If /var/log/Darshak/3.txt is coming up empty, it could mean that
BsPlSMProbe simply produces no output.  What if you ssh into the
server interactively (perhaps using openssh or putty), and your
command from there, as the same user, with the same options?

3) If you want errors as well as normal output in the same place, try
a 2>&1 in your shell command, assuming you're using sh/ksh/bash (POSIX
shell).  You can tell what shell you're in with echo $SHELL.

4) If paramiko allows you to access the exit status of the command,
you probably should retrieve that, not just stdout (and stderr).
Conventionally, if the exit status is 0, things are fine, if they're
nonzero, something's wrong - the oppose of what you see in most other
languages that treat integers as booleans, because there are usually
more ways things can fail than succeed.  In fact, strictly speaking,
the presence or absence of text on stderr is not a good indication of
an error, compared to checking the exit status.

HTH



More information about the Python-list mailing list