Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute command on remote server via ssh

Tags:

linux

shell

ssh

I am attempting to execute a command on a remote linux server via an ssh command on a local server like this:

ssh myremoteserver 'type ttisql'

where ttisql is an executable on the path of my remote machine.

The result of running this is:

bash: line 0: type: ttisql: not found

When I simply connect first and do:

ssh myremoteserver

and then enter the command:

[myuser@myremoteserver~]$: type ttisql

I get back the path of the ttisql exe as I would expect.

The odd thing is that when I execute the first command in my beta environment it works as expected and returns the path of the exe. In the beta scenario, machine A is connecting to remote machine B but both machines are onsite and the ssh command connects to the remote machine quickly.

The problem is encountered in our production environment when machine A is local and machine B is offsite and the ssh command takes a second or two to connect.

The only difference I can see is the time it takes the production ssh to connect. The path on the remote system is correct since the command works if entered after the initial connection.

Can anyone help me understand why this simple command would work in one environment and not the other? Could the problem be related to the time it takes to connect via ssh?

like image 349
jlteksolutions Avatar asked Nov 10 '13 20:11

jlteksolutions


People also ask

Can you send commands through SSH?

Yes, with ssh you can send commands directly to another system.


2 Answers

Your PATH is setup differently when your shell is interactive (= when you are logged in on the server), and when not interactive (running commands with ssh).

Look into the rc files used by your shell, for example .bashrc, .bash_profile, .profile (depends on your system). If you set PATH at the right place, then ttisql can work when you run it via ssh.

Another solution is to use the absolute path of ttisql, then it will not depend on your PATH setup.

like image 27
janos Avatar answered Oct 04 '22 11:10

janos


The environment can be different in a non-interactive session (ssh command) from an interactive session (ssh, then command). Try echo $PATH in both cases.

ssh myremoteserver 'echo $PATH'

vs

ssh myremoteserver
[myuser@myremoteserver~]$: echo $PATH

If they differ, look in all startup script for some differentiated behavior based on $PS1 or $-

like image 164
damienfrancois Avatar answered Oct 04 '22 10:10

damienfrancois