Linux 通过 ssh 在远程服务器上执行命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19894962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Execute command on remote server via ssh
提问by jlteksolutions
I am attempting to execute a command on a remote linux server via an ssh command on a local server like this:
我正在尝试通过本地服务器上的 ssh 命令在远程 linux 服务器上执行命令,如下所示:
ssh myremoteserver 'type ttisql'
ssh myremoteserver '输入 ttisql'
where ttisql is an executable on the path of my remote machine.
其中 ttisql 是我的远程机器路径上的可执行文件。
The result of running this is:
运行结果如下:
bash: line 0: type: ttisql: not found
bash:第 0 行:类型:ttisql:未找到
When I simply connect first and do:
当我首先连接并执行以下操作时:
ssh myremoteserver
ssh 远程服务器
and then enter the command:
然后输入命令:
[myuser@myremoteserver~]$: type ttisql
[myuser@myremoteserver~]$:输入ttisql
I get back the path of the ttisql exe as I would expect.
正如我所期望的那样,我得到了 ttisql exe 的路径。
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.
奇怪的是,当我在 beta 环境中执行第一个命令时,它按预期工作并返回 exe 的路径。在 beta 场景中,机器 A 连接到远程机器 B,但两台机器都在现场,并且 ssh 命令快速连接到远程机器。
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.
当机器A在本地而机器B在异地并且ssh命令需要一两秒钟才能连接时,在我们的生产环境中遇到了该问题。
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.
我能看到的唯一区别是生产 ssh 连接所需的时间。远程系统上的路径是正确的,因为如果在初始连接后输入该命令就可以工作。
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?
谁能帮我理解为什么这个简单的命令可以在一个环境中工作而不是在另一个环境中工作?问题是否与通过 ssh 连接所需的时间有关?
采纳答案by janos
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
).
你PATH
是不同的设置,当你shell是交互式(=当您登录到服务器上),当没有互动(与运行命令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
.
查看 shell 使用的 rc 文件,例如.bashrc
, .bash_profile
, .profile
(取决于您的系统)。如果您设置PATH
在正确的位置,那么ttisql
当您通过ssh
.
Another solution is to use the absolute path of ttisql
, then it will not depend on your PATH
setup.
另一种解决方案是使用 的绝对路径ttisql
,则它不会取决于您的PATH
设置。
回答by damienfrancois
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 command
) 与交互式会话 ( ssh
, then command
) 中的环境可能不同。echo $PATH
两种情况都试试。
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 $-
如果它们不同,请在所有启动脚本中查找基于$PS1
或$-