OpenWrt Forum Archive

Topic: Error running commands via ssh

The content of this topic has been archived on 1 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Hi,

I'd like to run commands via ssh, e.g.

ssh root@router "ifup wan"

This is what I get

[andrea@thinkpad ~]$ ssh root@router "ifup wan"
/sbin/ifup: 7: nvram: not found

This happens because the path is not set as when one logs in

[andrea@thinkpad ~]$ ssh root@router set
HOME='/tmp'
IFS='
'
LOGNAME='root'
OPTIND='1'
PATH='/usr/local/bin:/usr/bin:/sbin:/bin'
PPID='1946'
PS1='\w \$ '
PS2='> '
PS4='+ '
PWD='/tmp'
SHELL='/bin/ash'
USER='root'

This is when I log in

root@OpenWrt:~# set
HOME='/tmp'
IFS='
'
LOGNAME='root'
OPTIND='1'
PATH='/bin:/sbin:/usr/bin:/usr/sbin'
PPID='1948'
PS1='\u@\h:\w\$ '
PS2='> '
PS4='+ '
PWD='/tmp'
SHELL='/bin/ash'
TERM='xterm'
USER='root'
_='ll'

Basically /usr/sbin is missing

Other than setting PATH='/bin:/sbin:/usr/bin:/usr/sbin' before each command, does anybody know if it is possible to change it globally?

Thanks

You could modify the PATH variable in the /etc/profile - then it should be loaded at startup.

simba87 wrote:

You could modify the PATH variable in the /etc/profile - then it should be loaded at startup.

I think the issue is that /etc/profile is not execute when one runs commands via ssh.
/etc/profile is execute only when I log in the router, and I get a shell prompt.

This is /etc/profile

#!/bin/sh
[ -f /etc/banner ] && cat /etc/banner

export PATH=/bin:/sbin:/usr/bin:/usr/sbin
export PS1='\u@\h:\w\$ '

alias less=more
alias vim=vi
alias ll='ls -la'

arp() { cat /proc/net/arp; }
ldd() { LD_TRACE_LOADED_OBJECTS=1 $*; }
reboot() { ifdown wan 2>&1 >/dev/null ; /sbin/reboot; }

This is *not* the content of PATH when I run a command via ssh

[andrea@thinkpad ~]$ ssh root@router "echo \$PATH"
/usr/local/bin:/usr/bin:/sbin:/bin


The question is still: where are the DEFAULTS environment variables?

I guess I've overlooked that you're trying to run the command as a paramter of ssh.
And despite the fact I'm a Linux newbie, I'd say that the behaviour is stange.
I modified both the /etc/profile and the /etc/preinit and the made a script containing 'echo $PATH'... calling this script with ssh foo@router script.sh gave me /usr/local/bin:/usr/bin:/sbin:/bin (which is less than it should be)
Issuing the command ssh foo@router echo $PATH gave me the PATH variable of the system the ssh client ran on.

So, to be honest... I guess you have to ask somebody who actually has the knowledge for that... I'm running out of ideas tongue

simba87 wrote:

I guess I've overlooked that you're trying to run the command as a paramter of ssh.
And despite the fact I'm a Linux newbie, I'd say that the behaviour is stange.
I modified both the /etc/profile and the /etc/preinit and the made a script containing 'echo $PATH'... calling this script with ssh foo@router script.sh gave me /usr/local/bin:/usr/bin:/sbin:/bin (which is less than it should be)
Issuing the command ssh foo@router echo $PATH gave me the PATH variable of the system the ssh client ran on.

So, to be honest... I guess you have to ask somebody who actually has the knowledge for that... I'm running out of ideas tongue

I've found what's happening.
It's a long story, and this document explains who sets the PATH to what.

http://www.faqs.org/docs/Linux-mini/Path.html

With this script (run locally on your PC) it's possible to run commands as if logged in.

========================================================================
#! /bin/bash

COMMAND=$1

ssh root@router "export PATH=/bin:/sbin:/usr/bin:/usr/sbin && ${COMMAND}"
========================================================================

then you can do

router "ifdown wan"

router "ifup wan"

enjoy!

andrea

The discussion might have continued from here.