1

I have a script called "test" on a remote server's home folder that contains the following lines:

#!/bin/bash

tengbe=`ifconfig | grep -B1 192.168 | awk 'NR==1 { print $1 }' | sed 's/://g'`

Basically, it just stores the name of the interface of the local server into a variable once the script is called. When I ssh into the remote server to call the script, I get an error:

ssh remoteserver-IP './test'
./test: line 3: ifconfig: command not found

What might be wrong with my script? I have seen various answers that does not offer a solution to my issue.

2
  • The system on which you are running the script does not have ifconfig installed, check the output of which ifconfig Commented Feb 21, 2017 at 9:30
  • By default ifconfig is not in your PATH if you are not root. You are running a remote script on the remote machine. If you want to run a remote script on your local machine, you need to copy it over. Commented Feb 21, 2017 at 9:35

1 Answer 1

1

Try:

$ ssh remotehost ifconfig
bash: ifconfig: command not found

ifconfig isn't in your PATH on the remote host. You can prove it with:

$ which ifconfig
/sbin/ifconfig
$ ssh remotehost 'echo $PATH'
(returns lots of dirs, none of which is /sbin)

To get around this either specify the full path to ifconfig:

$ ssh remotehost /sbin/ifconfig

... or configure $PATH before calling it:

$ ssh remotehost 'PATH=$PATH:/sbin ifconfig'

... or edit $HOME/.bashrc (or alternatives -- read about your shell's initialisation process) to add /sbin to $PATH all the time.

For security, in a script it's usually better to specify absolute paths, maybe via variables. So in your script:

#!/bin/bash
IFCONFIG=/sbin/ifconfig
tengbe=$(${IFCONFIG} | grep -B1 192.168 | awk 'NR==1 { print $1 }' | sed 's/://g')

Note, I've replaced your backticks with $() - this isn't required, but it's a good habit to adopt - What is the benefit of using $() instead of backticks in shell scripts?

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for the answer, the PATH variable was the trick. Thanks a lot for your solution, it works. I still need to adopt to using the newer model of variable expansion as you have highlighted within your post. Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.