On 27/07/10 14:13, John Fano wrote:
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

_______________________________________________
paramiko mailing list
paramiko@lag.net
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

_______________________________________________
paramiko mailing list
paramiko@lag.net
http://www.lag.net/cgi-bin/mailman/listinfo/paramiko

Reply via email to