如何脚本多个ssh和scp命令,各个系统 [英] How to script multiple ssh and scp commands to various systems

查看:360
本文介绍了如何脚本多个ssh和scp命令,各个系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想剧本涉及多个ssh和scp呼叫命令序列。在日常生活中,我发现自己手工执行此任务的:

I would like to script a sequence of commands involving multiple ssh and scp calls. On a daily basis, I find myself manually performing this task:

从本地系统,ssh来SYSTEM1结果
在SYSTEM1结果的mkdir / tmp目录/数据
从SYSTEM1,ssh来SYSTEM2结果
在SYSTEM2结果的mkdir / tmp目录/数据
从SYSTEM2,SSH到系统3

From LOCAL system, ssh to SYSTEM1
mkdir /tmp/data on SYSTEM1
from SYSTEM1, ssh to SYSTEM2
mkdir /tmp/data on SYSTEM2
from SYSTEM2, SSH to SYSTEM3

从系统3 SCP文件:/数据SYSTEM2:/ tmp目录/数据结果
出口SYSTEM2结果
从SYSTEM2 SCP文件:/数据和SYSTEM2:/ tmp目录/数据SYSTEM1:/ tmp目录/数据结果
RM -fr SYSTEM2:/ tmp目录/数据结果
出口SYSTEM1结果
从SYSTEM1 SCP文件:/数据和SYSTEM1:/ tmp目录/数据LOCAL:/数据结果
RM -fr SYSTEM1:/ tmp目录/数据

scp files from SYSTEM3:/data to SYSTEM2:/tmp/data
exit to SYSTEM2
scp files from SYSTEM2:/data and SYSTEM2:/tmp/data to SYSTEM1:/tmp/data
rm -fr SYSTEM2:/tmp/data
exit to SYSTEM1
scp files from SYSTEM1:/data and SYSTEM1:/tmp/data to LOCAL:/data
rm -fr SYSTEM1:/tmp/data

我做的这个过程至少一天一次,大约需要5-10分钟不同的系统,然后清理事后之间发生。我真的想在bash脚本自动完成这一点,但我的业余尝试至今都没有成功。正如你可能会怀疑,该系统的通信受到限制,这意味着地方只能看到系统1,系统2只能看到系统1和系统3,系统3只能看到系统2,等你的想法。什么是做到这一点的最好方法是什么?此外,系统1为许多其他系统的轮毂,以便SYSTEM2必须由用户来指示(系统3将始终具有相同的相对IP /主机名相比任何SYSTEM2)

I do this process at LEAST once a day and it takes approximately 5-10 minutes going between the different systems and then cleaning up afterwards. I would really like to automate this in a bash script but my amateur attempts so far have been unsuccessful. As you might suspect, the systems communication is constrained, meaning LOCAL can only see System1, System2 can only see System1 and System3, system3 can only see system2, etc. You get the idea. What is the best way to do this? Additionally, System1 is a hub for many other systems so SYSTEM2 must be indicated by the user (System3 will always have the same relative IP/hostname compared to any SYSTEM2).

我想只是把这些命令按正确的顺序在一个shell脚本,然后手动提示时,在输入密码(这已经是效率的巨大收益),但无论是方法不工作或我的执行脚本是错误的。此外,我也想有,将采取一种模式,其中系统2来连接,为数据的模式复制脚本的命令行参数,并为本地系统上的数据的目标位置。

I tried just putting the commands in the proper order in a shell script and then manually typing in the passwords when prompted (which would already be a huge gain in efficiency) but either the method doesn't work or my execution of the script is wrong. Additionally, I would want to have a command line argument for the script that would take a pattern for which 'system2' to connect to, a pattern for the data to copy, and a target location for the data on the local system.

./grab_data system2 *05-14* ~/grabbed-data

我做了一些搜索,我想我的下一个步骤将是让每个执行本地任务系统上的脚本,然后通过SSH命令,从各自的系统远程执行脚本。有没有更好的办法?什么命令我应该看看用什么将是这个自动化的一般方法这种嵌套的ssh和scp的问题?

I did some searching and I think my next step would be to have scripts on each system that perform the local tasks, and then execute the scripts via ssh commands from the respective remote system. Is there a better way? What commands should I look at using and what would be the general approach to this automating this sort of nested ssh and scp problem?

我意识到我的描述可能有点绕口,请让澄清,我并没有恰当地描述的任何区域。

I realize my description may be a bit convoluted so please ask for clarification on any area that I did not properly describe.

感谢。

推荐答案

您可以通过在其他ssh连接(请参阅<隧道ssh连接简化这个过程中有很多href=\"http://superuser.com/questions/262926/must-i-sftp-to-an-intermediate-server/262940#262940\">this previous答案)。我愿意做它的方式是建立在本地系统上的.ssh / config文件中以下条目:

You can simplify this process a lot by tunneling ssh connections over other ssh connections (see this previous answer). The way I'd do it is to create an .ssh/config file on the LOCAL system with the following entries:

Host SYSTEM3
        ProxyCommand    ssh -e none SYSTEM2 exec /usr/bin/nc %h %p 2>/dev/null
        HostName        SYSTEM3.full.domain
        User            system3user

Host SYSTEM2
        ProxyCommand    ssh -e none SYSTEM1 exec /usr/bin/nc %h %p 2>/dev/null
        HostName        SYSTEM2.full.domain
        User            system2user

Host SYSTEM1
        HostName        SYSTEM1.full.domain
        User            system1user

(这是假设两者的中间宿主有netcat的安装为在/ usr /斌/ NC - 如果没有,你可能不得不寻找/安装的网关标准输入和放大器的一些等价的方式;标准输出到一个TCP会话)

(That's assuming both intermediate hosts have netcat installed as /usr/bin/nc -- if not, you may have to find/install some equivalent way of gatewaying stdin&stdout into a TCP session.)

有了这个设置,你可以使用 SCP系统3:/数据/数据​​本地,它会自动通过SYSTEM1和SYSTEM2隧道(并索要密码三个SYSTEMn的才能 - 这可能是一个有点混乱,特别是如果你输错一个)

With this set up, you can use scp SYSTEM3:/data /data on LOCAL, and it'll automatically tunnel through SYSTEM1 and SYSTEM2 (and ask for the passwords for the three SYSTEMn's in order -- this can be a little confusing, especially if you mistype one).

这篇关于如何脚本多个ssh和scp命令,各个系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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