Question

I'm using openSSH to make ssh connection between test machine (Windows XP) and controller machine (win 7). I've configured the ssh server on test machine and I'm able to get the ssh session and run command. When i run command in this ssh session i get o/p displayed in the putty session, however when i use the same command and try to retrieve the o/p I'm getting no value.

I've installed the openssh on my XP box, this is the version OpenSSH for Windows v3.8.1p1-1.

This is how I configured it for running the ssh on windows

C:\Documents and Settings\Administrator>cd c:\OpenSSH    
C:\OpenSSH>cd bin    
C:\OpenSSH\bin>mkgroup -l >> ..\etc\group    
C:\OpenSSH\bin>mkpasswd -l -u administrator >> ..\etc\passwd    
C:\OpenSSH\bin>net start opensshd
The requested service has already been started.

Below is my code for the same

import paramiko


class TestSSH():

    def __init__(self):
        self.ip = "172.xxx.xxx.xxx"
        self.user = "Administrator"
        self.password = "passpass"


    def run(self):
        ssh_client = paramiko.SSHClient()
        ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh_client.connect(
            self.ip,
            username = self.user,
            password = self.password
        )
        std_in, std_out, std_err = ssh_client.exec_command("dir")

        print std_out.read()


obj = TestSSH()
obj.run()

this is the o/p i'm getting in putty session -

Directory of C:\Documents and Settings\Administrator

11/14/2013  03:11 PM    <DIR>          .
11/14/2013  03:11 PM    <DIR>          ..
11/14/2013  03:12 PM    <DIR>          Desktop
07/27/2011  06:00 PM    <DIR>          Favorites
11/14/2013  03:11 PM    <DIR>          hemant
07/27/2011  06:00 PM    <DIR>          My Documents
07/27/2011  11:12 PM    <DIR>          Start Menu
               0 File(s)              0 bytes
               7 Dir(s)  39,932,612,608 bytes free

C:\Documents and Settings\Administrator>

however I'm not getting any o/p

I've tried with other sshutility for windows name freeSSHd, I'm able to connect through putty and execute command, however, I'm only able to connect but not execute the command. I'm getting the following error with the freeSSHd server.

Unable to execute command or shell on remote system: Failed to Execute process.

This o/p is came into std_out not in std_err variable.

Was it helpful?

Solution

I don't know how to fix your problem with your original SSH server, but now that you've switched to freeSSHd, you're having a different problem.

Unable to execute command or shell on remote system: Failed to Execute process basically means that it's trying to execute dir without a shell, which won't work. There is no dir.exe on your PATH; there's a dir command built in to cmd.exe.

In other words, it's the same problem you'd get trying to execute a bash builtin without the shell on a POSIX system. And the solution is the same: Execute the shell with dir as a command.

The way to do that is:

cmd /c dir

There's a further bug you might have with freeSSHd, which does some fancy stuff trying to hide the differences between a cmd.exe terminal and a POSIX terminal that can cause you to get errors about "no console window found". Hopefully you won't run into that one; if you do, dig through the preferences, and there should be a checkbox to turn this stuff off.


And, now that I think about it, whatever reason your original ssh server was acting weird (running ksh or something), having it execute cmd /c dir instead of dir would work around that problem, too; worst-case scenario you'd be running an unnecessary shell-inside-a-shell, which is silly, but wouldn't break simple things like dir.

OTHER TIPS

The reason you didn't get any output is that there was no output, because the remote system couldn't run the dir command. You did get an error message, but that was printed to stderr, not stdout, so unless you print out std_err.read() you'll never see that.

As to why you got the error, that's hard to guess without more information. Here are some possibilities.

First, dir: not found that looks like an error message you'd get from a POSIX shell. (It's exactly what I get from ksh; sh/bash, csh/tcsh, and zsh are all similar but not identical.) And most POSIX systems won't have a program named dir anywhere on the PATH. Is it possible you accidentally connected to the wrong computer, and it's not a Windows box?

If it's a Windows box, but it's got a *nix-style shell installed, e.g., via cygwin or MinGW, is it possible that this is the default shell for the user, or the shell specified in the ssh server configs? This is very likely if you're using, e.g., cygwin sshd instead of a native Windows ssh server.

It's possible to configure Powershell to give brief errors instead of the usual verbose ones. I've never done this, so I have no idea if its brief error for "The term 'dir' is not recognized as the name of a cmdlet, function, script file, or operable program…" looks like "dir: not found", but it's not impossible. Of course you'd also have to set Powershell as the user's default shell or override the default shell in the ssh server configs for this to happen, and I'm pretty sure dir is in the default list of aliases, so you'd have to override that too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top