如何使用SSH从远程服务器获得命令的结果?

发布于 2025-02-05 10:21:00 字数 991 浏览 1 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陪你搞怪i 2025-02-12 10:21:00

只需将脚本的输出分配给变量,然后以后使用该变量:

result="$(./yourscript)"
echo "Result from SSH = $result"

Simply assign the output of your script to a variable and later use that variable:

result="$(./yourscript)"
echo "Result from SSH = $result"
っ左 2025-02-12 10:21:00

远程打印null划界文件列表,通过反向主要版本和修补版本进行排序,因此首先列出了最新版本

read -r -d '' latest_archive < <(
  ssh \
    -o UserKnownHostsFile=/dev/null \
    -o StrictHostKeyChecking=no \
    -o PreferredAuthentications=publickey \
    [email protected] \
    printf '%s\\0' '/my/dir/file-*[[:digit:]].tar.xz' | sort -zt- -k2nr,3nr
)

  • 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

read -r -d '' latest_archive < <(
  ssh \
    -o UserKnownHostsFile=/dev/null \
    -o StrictHostKeyChecking=no \
    -o PreferredAuthentications=publickey \
    [email protected] \
    printf '%s\\0' '/my/dir/file-*[[:digit:]].tar.xz' | sort -zt- -k2nr,3nr
)

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文