通过 ssh 在远程服务器上执行命令 [英] Execute command on remote server via ssh

查看:44
本文介绍了通过 ssh 在远程服务器上执行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过本地服务器上的 ssh 命令在远程 linux 服务器上执行命令,如下所示:

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

ssh myremoteserver '输入 ttisql'

ssh myremoteserver 'type ttisql'

其中 ttisql 是我远程机器路径上的可执行文件.

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

运行结果如下:

bash:第 0 行:类型:ttisql:未找到

bash: line 0: type: ttisql: not found

当我首先连接并执行以下操作时:

When I simply connect first and do:

ssh myremoteserver

ssh myremoteserver

然后输入命令:

[myuser@myremoteserver~]$: 输入 ttisql

[myuser@myremoteserver~]$: type ttisql

我按预期返回了 ttisql exe 的路径.

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

奇怪的是,当我在 beta 环境中执行第一个命令时,它按预期工作并返回 exe 的路径.在 beta 场景中,机器 A 连接到远程机器 B,但两台机器都在现场,并且 ssh 命令快速连接到远程机器.

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.

当机器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.

我能看到的唯一区别是生产 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?

推荐答案

你的 PATH 在你的 shell 是交互式的(= 当你登录到服务器时)和非交互式时的设置不同(使用 ssh 运行命令).

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).

查看你的shell使用的rc文件,例如.bashrc.bash_profile.profile(取决于你的系统).如果您在正确的位置设置了 PATH,那么当您通过 ssh 运行它时,ttisql 可以工作.

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.

另一种解决方案是使用ttisql的绝对路径,这样就不会依赖于你的PATH设置.

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

这篇关于通过 ssh 在远程服务器上执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆