大网数据广告活动
服务器运维问答

Shell script from local machine to execute command in remote machine, then continue on the local machine

· 52CE 我爱测

The problem is this: I want to copy files from a remote machine. I am looking for a way to write a shell script that would connect to the remote machine, get the location of the stored file, once I get the location disconnect fro…

问题原文

原帖链接 · gnsr · CC BY-SA 3.0

The problem is this: I want to copy files from a remote machine.

I am looking for a way to write a shell script that would connect to the remote machine, get the location of the stored file, once I get the location disconnect from the remote server and use scp from within the local machine with the obtained location to copy the file.

Specifically, I want to know how from a script, I would connect to a remote machine, do tasks on that machine. Disconnect, and continue execution locally.

已采纳解答

原帖链接 · Mark Griffin · CC BY-SA 3.0

Solution 1:

remote_output="$(ssh user@host "remote command")"
scp "user@$host:$remote_output" /local/path

This works by connecting to the remote machine, running some commands, and copying the output to a variable on your local machine. Say, echoing the file path remotely, capturing it locally.

Solution 2:

ssh user@host "determine_path; cat filename" >local_filename

This works by finding and outputting the file directly from remote, and redirecting it to a file locally. This skips scp. This requires that the steps prior to "cat" don't have output (which you can solve with &>/dev/null).

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-08-28)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。