linux - 连接两台服务器并获取数据

标签 linux shell

我正在编写一个脚本来连接到服务器计算机并从中获取一些数据。我的命令顺序如下

ssh -tt user@server1 ssh -tt user@server2  
cd dir1/dir2  
ls -1t name* | head 1

从服务器退出后应打印最后一个命令的结果

最佳答案

要在其他服务器上运行命令,您需要将其用作 ssh 的参数。

man ssh(缩写为 [...]):

Synopsis

ssh [...] destination [command]

[...]

If command is specified, it is executed on the remote host instead of a login shell.

为了说明,在第一步中,只有第二个命令被转换为这种形式。

ssh -tt user@server1
ssh -tt user@server2 'cd dir1/dir2; ls -1t name* | head 1'

现在第二个也必须被引用并用作第一次登录的参数:

ssh -tt user@server1 "ssh -tt user@server2 'cd dir1/dir2; ls -1t name* | head 1'"

确保转义双引号中的空格字符,例如“\$”

关于linux - 连接两台服务器并获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47143022/

相关文章:

python - 如何在 Python 中获取特定程序的当前 CPU、GPU 和 RAM 使用情况?

linux - 什么是绝对路径名与相对路径名

ruby - 如何从 shell 运行 Ruby gem?

正则表达式匹配排除其他一些词

linux - "Visiting a page"使用 cron 作业

linux - linux shell 中的 python 脚本,用于检查命令行是否写入文件

linux - 更好的解析输入的方法

shell - 在 Raspberry PI 2 上使用 Ubuntu MATE 15.04 引导至 shell

linux - 在 Linux 中,如何从 ascii 文件中去除所有高位但保留其余位? - 像 ISTRIP 但有文件

linux - 查找带有可选子表达式的命令正则表达式