Discussion:
Error msg
John Fano
2010-07-27 12:13:52 UTC
Permalink
Hi Everyone,

I recently found Paramiko and it's a great fit for my project.
However I am having an issue running a command remotely. I have a
windows machine running Cygwin with OpenSSH. Basically I am touching
a batch file and adding net share commands to it. Once I am done I
want to execute the batch file and have it share all the folders I
just created. I would just send the net share commands but since the
backslash means something to Python I have to put in a million of them
just to get them to come out right on the other end. Whereas I can
just echo the command to a batch file with normal backslashes to the
remote file and it comes out correct. Anyway, all goes well until I
try to execute the batch file.

Running this:

stdin, stdout, stderr = ssh.exec_command('/cygdrive/c/test/shares.bat')

I get the following stderr output:

System error 5 has occurred.

Access is denied.

Checking stdout shows that the command is complete and correct:

C:\cygwin\home\john>net share test$=c:\test\testfolder

Now, if I manually SSH to my windows host from the command line I can
issue the same command "/cygdrive/c/test/shares.bat" and it works
perfectly. So I know the batch file is good and I know that Cygwin is
interpreting and executing it correctly, so that leaves Python and/or
Paramiko. I am logging in to the remote host with a domain account
that has local admin rights.

Anyone have any thoughts?

John :-)
------------------------------
"Any people, nation, or
language that speaks anything
against the God of Shadrach,
Meshach, and Abednego shall be
torn limb from limb, and their
houses laid in ruins, for there
is no other god who is able to
rescue in this way."
                 - Daniel 3:29
Charles Duffy
2010-07-27 14:15:56 UTC
Permalink
run_command
do this:

$ ssh ***@host run_command

The latter approach will more accurately model what exec_command() is doing.

Do you still get different behavior between Paramiko and OpenSSH?
Hi Everyone,
I recently found Paramiko and it's a great fit for my project.
However I am having an issue running a command remotely. I have a
windows machine running Cygwin with OpenSSH. Basically I am touching
a batch file and adding net share commands to it. Once I am done I
want to execute the batch file and have it share all the folders I
just created. I would just send the net share commands but since the
backslash means something to Python I have to put in a million of them
just to get them to come out right on the other end. Whereas I can
just echo the command to a batch file with normal backslashes to the
remote file and it comes out correct. Anyway, all goes well until I
try to execute the batch file.
stdin, stdout, stderr = ssh.exec_command('/cygdrive/c/test/shares.bat')
System error 5 has occurred.
Access is denied.
C:\cygwin\home\john>net share test$=c:\test\testfolder
Now, if I manually SSH to my windows host from the command line I can
issue the same command "/cygdrive/c/test/shares.bat" and it works
perfectly. So I know the batch file is good and I know that Cygwin is
interpreting and executing it correctly, so that leaves Python and/or
Paramiko. I am logging in to the remote host with a domain account
that has local admin rights.
Anyone have any thoughts?
John :-)
------------------------------
"Any people, nation, or
language that speaks anything
against the God of Shadrach,
Meshach, and Abednego shall be
torn limb from limb, and their
houses laid in ruins, for there
is no other god who is able to
rescue in this way."
- Daniel 3:29
_______________________________________________
paramiko mailing list
http://www.lag.net/cgi-bin/mailman/listinfo/paramiko
John Fano
2010-07-27 15:56:05 UTC
Permalink
Thanks for the quick feedback.

I ran ssh ***@myhost /cygdrive/c/test/shares.bat

It returned the following:

C:\cygwin\home\john>net share test2$=c:\test\testfolder
test2$ was shared successfully.

So yes, it still behaves differently.

John :-)
------------------------------
"Any people, nation, or
language that speaks anything
against the God of Shadrach,
Meshach, and Abednego shall be
torn limb from limb, and their
houses laid in ruins, for there
is no other god who is able to
rescue in this way."
                 - Daniel 3:29
Post by Charles Duffy
run_command
The latter approach will more accurately model what exec_command() is doing.
Do you still get different behavior between Paramiko and OpenSSH?
Hi Everyone,
I recently found Paramiko and it's a great fit for my project.
However I am having an issue running a command remotely.  I have a
windows machine running Cygwin with OpenSSH.  Basically I am touching
a batch file and adding net share commands to it.  Once I am done I
want to execute the batch file and have it share all the folders I
just created.  I would just send the net share commands but since the
backslash means something to Python I have to put in a million of them
just to get them to come out right on the other end.  Whereas I can
just echo the command to a batch file with normal backslashes to the
remote file and it comes out correct.  Anyway, all goes well until I
try to execute the batch file.
stdin, stdout, stderr = ssh.exec_command('/cygdrive/c/test/shares.bat')
System error 5 has occurred.
Access is denied.
C:\cygwin\home\john>net share test$=c:\test\testfolder
Now, if I manually SSH to my windows host from the command line I can
issue the same command "/cygdrive/c/test/shares.bat" and it works
perfectly.  So I know the batch file is good and I know that Cygwin is
interpreting and executing it correctly, so that leaves Python and/or
Paramiko.  I am logging in to the remote host with a domain account
that has local admin rights.
Anyone have any thoughts?
John :-)
------------------------------
"Any people, nation, or
language that speaks anything
against the God of Shadrach,
Meshach, and Abednego shall be
torn limb from limb, and their
houses laid in ruins, for there
is no other god who is able to
rescue in this way."
                 - Daniel 3:29
_______________________________________________
paramiko mailing list
http://www.lag.net/cgi-bin/mailman/listinfo/paramiko
Eric Noulard
2010-07-27 15:47:23 UTC
Permalink
Post by John Fano
Hi Everyone,
I recently found Paramiko and it's a great fit for my project.
However I am having an issue running a command remotely.  I have a
windows machine running Cygwin with OpenSSH.  Basically I am touching
a batch file and adding net share commands to it.  Once I am done I
want to execute the batch file and have it share all the folders I
just created.  I would just send the net share commands but since the
backslash means something to Python I have to put in a million of them
just to get them to come out right on the other end.  Whereas I can
just echo the command to a batch file with normal backslashes to the
remote file and it comes out correct.  Anyway, all goes well until I
try to execute the batch file.
stdin, stdout, stderr = ssh.exec_command('/cygdrive/c/test/shares.bat')
System error 5 has occurred.
Access is denied.
C:\cygwin\home\john>net share test$=c:\test\testfolder
Now, if I manually SSH to my windows host from the command line I can
issue the same command "/cygdrive/c/test/shares.bat" and it works
perfectly.  So I know the batch file is good and I know that Cygwin is
interpreting and executing it correctly, so that leaves Python and/or
Paramiko.  I am logging in to the remote host with a domain account
that has local admin rights.
Anyone have any thoughts?
May be the command itself, i.e. "shares.bat" does return a non-zero
return code. If this is the case you won't notice it with an interactive but
ssh.exec_command will.

On Windows/Dos return code seems to be called ERRORLEVEL:
http://www.robvanderwoude.com/errorlevel.php

If this is the case and you don't have any control on that
(even if I think an EXIT 0 at the end of .bat file should do)
you can try to use paramiko "invoke_shell"
http://www.lag.net/paramiko/docs/paramiko.SSHClient-class.html#invoke_shell
to mimic an interactive session.
--
Erk
Membre de l'April - « promouvoir et défendre le logiciel libre » -
http://www.april.org
Charles Duffy
2010-07-27 16:56:01 UTC
Permalink
Well, we can test that theory pretty easily:

$ ssh ***@myhost /cygdrive/c/test/shares.bat; echo "Exit status is: $?"
John Fano
2010-07-27 16:02:48 UTC
Permalink
From what I have seen in the past in some of my other scripts it
should return 0 for success with text output "SHARENAME was share
successfully." When ever the net share command fails it returns a
error number 4 digits long as well as a text message about searching
for help. I ran ssh ***@myhost 'net share asdf' to produce an error
and this it what it looks like:

This shared resource does not exist.

More help is available by typing NET HELPMSG 2310.

I haven't tried the invoke_shell yet. I'll take a look at it.

John :-)
------------------------------
"Any people, nation, or
language that speaks anything
against the God of Shadrach,
Meshach, and Abednego shall be
torn limb from limb, and their
houses laid in ruins, for there
is no other god who is able to
rescue in this way."
                 - Daniel 3:29
Post by John Fano
Hi Everyone,
I recently found Paramiko and it's a great fit for my project.
However I am having an issue running a command remotely.  I have a
windows machine running Cygwin with OpenSSH.  Basically I am touching
a batch file and adding net share commands to it.  Once I am done I
want to execute the batch file and have it share all the folders I
just created.  I would just send the net share commands but since the
backslash means something to Python I have to put in a million of them
just to get them to come out right on the other end.  Whereas I can
just echo the command to a batch file with normal backslashes to the
remote file and it comes out correct.  Anyway, all goes well until I
try to execute the batch file.
stdin, stdout, stderr = ssh.exec_command('/cygdrive/c/test/shares.bat')
System error 5 has occurred.
Access is denied.
C:\cygwin\home\john>net share test$=c:\test\testfolder
Now, if I manually SSH to my windows host from the command line I can
issue the same command "/cygdrive/c/test/shares.bat" and it works
perfectly.  So I know the batch file is good and I know that Cygwin is
interpreting and executing it correctly, so that leaves Python and/or
Paramiko.  I am logging in to the remote host with a domain account
that has local admin rights.
Anyone have any thoughts?
May be the command itself, i.e. "shares.bat" does return a non-zero
return code. If this is the case you won't notice it with an interactive but
ssh.exec_command will.
http://www.robvanderwoude.com/errorlevel.php
If this is the case and you don't have any control on that
(even if I think an EXIT 0 at the end of .bat file should do)
you can try to use paramiko "invoke_shell"
http://www.lag.net/paramiko/docs/paramiko.SSHClient-class.html#invoke_shell
to mimic an interactive session.
--
Erk
Membre de l'April - « promouvoir et défendre le logiciel libre » -
http://www.april.org
Moritz Beber
2010-07-27 15:59:27 UTC
Permalink
Post by John Fano
Hi Everyone,
I recently found Paramiko and it's a great fit for my project.
However I am having an issue running a command remotely. I have a
windows machine running Cygwin with OpenSSH. Basically I am touching
a batch file and adding net share commands to it. Once I am done I
want to execute the batch file and have it share all the folders I
just created. I would just send the net share commands but since the
backslash means something to Python I have to put in a million of them
just to get them to come out right on the other end. Whereas I can
just echo the command to a batch file with normal backslashes to the
remote file and it comes out correct. Anyway, all goes well until I
try to execute the batch file.
stdin, stdout, stderr = ssh.exec_command('/cygdrive/c/test/shares.bat')
System error 5 has occurred.
Access is denied.
C:\cygwin\home\john>net share test$=c:\test\testfolder
Now, if I manually SSH to my windows host from the command line I can
issue the same command "/cygdrive/c/test/shares.bat" and it works
perfectly. So I know the batch file is good and I know that Cygwin is
interpreting and executing it correctly, so that leaves Python and/or
Paramiko. I am logging in to the remote host with a domain account
that has local admin rights.
Anyone have any thoughts?
John :-)
------------------------------
"Any people, nation, or
language that speaks anything
against the God of Shadrach,
Meshach, and Abednego shall be
torn limb from limb, and their
houses laid in ruins, for there
is no other god who is able to
rescue in this way."
- Daniel 3:29
_______________________________________________
paramiko mailing list
http://www.lag.net/cgi-bin/mailman/listinfo/paramiko
Hi John,

have you tried executing the same command via a pseudo terminal? For
example, I use the following code for certain commands that need the
shell environment. Assuming ssh is an instance of paramiko.SSHClient.

try:
channel = ssh.invoke_shell()
except paramiko.SSHException:
print "Failed to invoke shell!"
raise IOError
if channel.gettimeout():
print "Channel timeout: %f", channel.gettimeout()
else:
channel.settimeout(5.)
cmd = "/cygdrive/c/test/shares.bet\n"
try:
channel.sendall(cmd)
except socket.timeout:
print "Connection timed out!"
channel.close()
raise IOError
stdout = ""
expect = "some expected string the shell will return\r\n"
while True:
try:
stdout += channel.recv(1024)
if stdout.endswith(expect):
break
except socket.timeout:
break
print stdout
channel.close()

Please, pay attention to the command stored in cmd, it requires a line
end symbol now '\n'. Also, shells are interactive, so getting the output
can be tricky. There's a whole module called pexpect for that but in my
case I knew what string to expect and could just use that in a lazy way.
This is so you know when to stop receiving from the channel.

Best,
Moritz
John Fano
2010-07-29 12:38:25 UTC
Permalink
OK I first checked the output code...

ssh ***@myhost /cygdrive/c/test/shares.bat; echo "Exit status is: $?"

C:\cygwin\home\john>net share test2$=c:\test\testfolder
test2$ was shared successfully.

Exit status is: 0

The exit status for trying to reshare a folder is 2 and the exit
status for sharing a folder that doesn't exist or issuing an
incomplete net share command is also 2

The I tried the invoke_shell test given by Moritz. I just get a
timeout and nothing gets shared on the windows machine.

I then tried:

status = commands.getoutputstatus('ssh ***@myhost
"/cygdrive/c/test/shares.bat"')

and it shared the folder, so I think I just execute the batch file
this way until I can get Paramiko working. Feel free to pass on any
other thoughts.

Thanks!

John :-)
------------------------------
"Any people, nation, or
language that speaks anything
against the God of Shadrach,
Meshach, and Abednego shall be
torn limb from limb, and their
houses laid in ruins, for there
is no other god who is able to
rescue in this way."
                 - Daniel 3:29



On Tue, Jul 27, 2010 at 11:59 AM, Moritz Beber
Post by Moritz Beber
Post by John Fano
Hi Everyone,
I recently found Paramiko and it's a great fit for my project.
However I am having an issue running a command remotely.  I have a
windows machine running Cygwin with OpenSSH.  Basically I am touching
a batch file and adding net share commands to it.  Once I am done I
want to execute the batch file and have it share all the folders I
just created.  I would just send the net share commands but since the
backslash means something to Python I have to put in a million of them
just to get them to come out right on the other end.  Whereas I can
just echo the command to a batch file with normal backslashes to the
remote file and it comes out correct.  Anyway, all goes well until I
try to execute the batch file.
stdin, stdout, stderr = ssh.exec_command('/cygdrive/c/test/shares.bat')
System error 5 has occurred.
Access is denied.
C:\cygwin\home\john>net share test$=c:\test\testfolder
Now, if I manually SSH to my windows host from the command line I can
issue the same command "/cygdrive/c/test/shares.bat" and it works
perfectly.  So I know the batch file is good and I know that Cygwin is
interpreting and executing it correctly, so that leaves Python and/or
Paramiko.  I am logging in to the remote host with a domain account
that has local admin rights.
Anyone have any thoughts?
John :-)
------------------------------
"Any people, nation, or
language that speaks anything
against the God of Shadrach,
Meshach, and Abednego shall be
torn limb from limb, and their
houses laid in ruins, for there
is no other god who is able to
rescue in this way."
                 - Daniel 3:29
_______________________________________________
paramiko mailing list
http://www.lag.net/cgi-bin/mailman/listinfo/paramiko
Hi John,
have you tried executing the same command via a pseudo terminal? For
example, I use the following code for certain commands that need the shell
environment. Assuming ssh is an instance of paramiko.SSHClient.
           channel = ssh.invoke_shell()
           print "Failed to invoke shell!"
           raise IOError
           print "Channel timeout: %f", channel.gettimeout()
           channel.settimeout(5.)
       cmd = "/cygdrive/c/test/shares.bet\n"
           channel.sendall(cmd)
           print "Connection timed out!"
           channel.close()
           raise IOError
       stdout = ""
       expect = "some expected string the shell will return\r\n"
               stdout += channel.recv(1024)
                   break
               break
       print stdout
       channel.close()
Please, pay attention to the command stored in cmd, it requires a line end
symbol now '\n'. Also, shells are interactive, so getting the output can be
tricky. There's a whole module called pexpect for that but in my case I knew
what string to expect and could just use that in a lazy way. This is so you
know when to  stop receiving from the channel.
Best,
Moritz
_______________________________________________
paramiko mailing list
http://www.lag.net/cgi-bin/mailman/listinfo/paramiko
Loading...