如何使用SSH从远程服务器获得命令的结果?
我在Linux Bash(版本16.04.4 LTS)工作。 我正在使用SSH连接到远程服务器,在那里我想从文件列表(最新文件版本)中找到特定文件,然后仅检索版本。 例如,在远程服务器(server.com)上考虑此文件:
file-30.0-2.tar.xz
file-30.0-3.tar.xz
file-30.0-4.tar.xz
file-30.0-5.tar.xz
file-30.0-7.tar.xz
file-30.0-10.tar.xz
如果我直接在远程服务器中登录,并且如果尝试以下命令,则可以工作。
ls file-*.tar.xz | sort -V | tail -n1 | grep -o '[[:digit:]]\+.[[:digit:]]\+.[[:digit:]]\+'
输出:30.0-10
当我从另一台服务器上工作时,我正在使用SSH连接到远程服务器:
#!/bin/bash
set -euxo pipefail
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o PreferredAuthentications=publickey [email protected] "cd /myDir; \\
ls file-*.tar.xz | sort -V | tail -n1 | grep -o '[[:digit:]]\+.[[:digit:]]\+.[[:digit:]]\+')"
此命令正常工作,并且我获得的输出与以前相同(30.0-10)。
但是我希望能够将结果(30.0-10)传递给脚本外部,并在本地服务器中使用。
有什么想法吗?
I am working in Linux bash (version 16.04.4 LTS).
I am using ssh to connect to a remote server where I want to find for a specific file from a list of files (most recent file version) and then retrieve only the version.
For example, considering this files on the remote server (server.com):
file-30.0-2.tar.xz
file-30.0-3.tar.xz
file-30.0-4.tar.xz
file-30.0-5.tar.xz
file-30.0-7.tar.xz
file-30.0-10.tar.xz
If I login directly in the remote server and If I try the following command it works.
ls file-*.tar.xz | sort -V | tail -n1 | grep -o '[[:digit:]]\+.[[:digit:]]\+.[[:digit:]]\+'
Output: 30.0-10
As I am working from another server I am using ssh to connect to remote server like this:
#!/bin/bash
set -euxo pipefail
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o PreferredAuthentications=publickey [email protected] "cd /myDir; \\
ls file-*.tar.xz | sort -V | tail -n1 | grep -o '[[:digit:]]\+.[[:digit:]]\+.[[:digit:]]\+')"
This command it's working and I get the same output as before (30.0-10).
But I want to be able to pass that result (30.0-10) to outside the script and use it in the local server.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需将脚本的输出分配给变量,然后以后使用该变量:
Simply assign the output of your script to a variable and later use that variable:
远程打印null划界文件列表,通过反向主要版本和修补版本进行排序,因此首先列出了最新版本
:
printf'%s \\ 0''/my/my/dir/file-*[[[[[[[[[[[[[[[[[[[[[[[[[[ :digit:]]。tar.xz'
:打印与模式匹配的文件列表。| sort -zt- -k2nr,3nr
:排序-z
null-delimited条目,-t-
with dash - - - 作为字段定界符,-K2NR,第2个字段上的3NR
数字上最大,第一,与第三个字段相结合,也最大。Print null delimited file list remotely, sort it by reverse major version, and patch version, so the latest is listed first
How it works:
printf '%s\\0' '/my/dir/file-*[[:digit:]].tar.xz'
: Prints null-delimited list of files matching the pattern.| sort -zt- -k2nr,3nr
: Pipe to sort-z
null-delimited entries,-t-
with dash-
as field delimiter,-k2nr,3nr
on 2nd field numerically, largest first, combined with 3rd field numerically and also largest first.