mwilliamson / spur.py

Run commands and manipulate files locally or over SSH using the same interface
BSD 2-Clause "Simplified" License
267 stars 37 forks source link

Command not found: hostname. Check that echo is installed and on $PATH #15

Closed iliaselmatani closed 10 years ago

iliaselmatani commented 10 years ago

Hi there,

Trying to execute the command 'hostname' on an ESXi server but it fails with the error message: Command not found: hostname. Check that echo is installed and on $PATH

The code:

import spur

shell = spur.SshShell(hostname="ESXI-SERVER", username="user1", password="pass1", missing_host_key=spur.ssh.MissingHostKey.accept)
with shell:
    result = shell.run(["hostname"])
print result.output

When executing the command manually using SSH getting the output from the server.

ssh user1@ESXI-SERVER
Password: 
The time and date of this login have been sent to the system logs.

VMware offers supported, powerful system administration tools.  Please
see www.vmware.com/go/sysadmintools for details.

The ESXi Shell can be disabled by an administrative user. See the
vSphere Security documentation for more information.
~ # hostname
ESXI-SERVER
~ # 

Kind regards, Ilias

mwilliamson commented 10 years ago

What happens when you run which hostname? One possible explanation is that hostname is built into your shell, rather than being an actual executable.

iliaselmatani commented 10 years ago

Hi Williamson,

Tried the code

with shell:
    result = shell.run(["which", "hostname"])

But the same result. Hereby also the logs from the ESXI-server.

2014-04-16T14:40:33Z sshd[3142035]: Connection from 1.2.3.4 port 39759
2014-04-16T14:40:34Z sshd[3142035]: Failed password for user1 from 1.2.3.4 port 39759 ssh2
2014-04-16T14:40:34Z sshd[3142038]: pam_per_user: create_subrequest_handle(): doing map lookup for user "user1"
2014-04-16T14:40:34Z sshd[3142038]: pam_per_user: create_subrequest_handle(): creating new subrequest (user="user1", service="system-auth-generic")
2014-04-16T14:40:34Z sshd[3142035]: Accepted keyboard-interactive/pam for user1 from 1.2.3.4 port 39759 ssh2
2014-04-16T14:40:34Z sshd[3142035]: pam_per_user: create_subrequest_handle(): doing map lookup for user "user1"
2014-04-16T14:40:34Z sshd[3142035]: pam_per_user: create_subrequest_handle(): creating new subrequest (user="user1", service="system-auth-generic")
2014-04-16T14:40:34Z sshd[3142035]: pam_unix(system-auth-generic:session): session opened for user user1 by (uid=0)
2014-04-16T14:40:34Z sshd[3142035]: User 'user1' running command 'command -v 'which' > /dev/null 2>&1 ; echo $?; exec 'which' 'hostname''
2014-04-16T14:40:34Z sshd[3142035]: pam_unix(system-auth-generic:session): session closed for user user1
mwilliamson commented 10 years ago

Sorry, I meant trying to run which manually. I'm not familiar with the ESXi shell -- does it support command -v? Trying to run command -v hostname might be more informative.

iliaselmatani commented 10 years ago

which hostname

~ # which hostname
/bin/hostname

and command -v hostname

~ # command -v hostname
-sh: command: not found
mwilliamson commented 10 years ago

Ah, that's the issue then: Spur uses command -v to determine if a command exists or not. The simplest fix is probably to fall back to using which instead of command -v if the latter doesn't exist (or possibly vice versa). Not sure when I'll have time to publish a fix, but feel free to have a go yourself if you like.

mwilliamson commented 10 years ago

I've committed a potential fix to master. Could you let me know whether that fixes the issue?

iliaselmatani commented 10 years ago

Fixed the issue. Thanks Williamson.

mwilliamson commented 10 years ago

Great, thanks!