简体   繁体   English

如何通过在machineA和machineC上运行shell脚本来执行命令

[英]How to execute command from shell script on machineB and machineC by running it in machineA

I have three machines as mentioned below - 我有以下提到的三台机器-

machineA
machineB
machineC

machineB and machineC has this directory - machineB和machineC具有此目录-

/bat/data/snapshot_t1/

Now I need to run my below shell script from machineA and figure out the latest directory in machineB and machineC (by logging into those machine I guess) under this path /bat/data/snapshot_t1/ . 现在,我需要从machineA运行下面的shell脚本,并在/bat/data/snapshot_t1/路径下找到machineB和machineC中的最新目录(通过登录我猜的那些机器)。

Below is my shell script in which I know how to get the latest directory using find command along with sort and tail. 下面是我的Shell脚本,在其中我知道如何使用find命令以及sort和tail来获取最新目录。

Now I am supposed to run the below shell script from machineA and then find out the latest directory in machineB and machineC.. How to do that? 现在,我应该从machineA运行下面的shell脚本,然后在machineB和machineC中找到最新的目录。

#!/bin/bash

readonly FILERS_LOCATION=(machineB machineC)
readonly DIRECTORY_LOCATION=/bat/data/snapshot_t1/

# login to machineB and find out the latest directory in machineB
dir1=`find $DIRECTORY_LOCATION -type d | sort | tail -1`; echo $dir

# login to machineC and find out the latest directory in machineC
dir2=`find $DIRECTORY_LOCATION -type d | sort | tail -1`; echo $dir

How do I login into machineB and machineC from the shell script which is running in machineA and then execute certain commands on machineB and machineC and get the result back and store it in some variable, let's say dir1 and dir2. 我如何从在machineA中运行的shell脚本登录到machineB和machineC,然后在machineB和machineC上执行某些命令,然后将结果返回并存储在某个变量中,例如dir1和dir2。

Suppose the login name is david and let's say I have the keys setup as well on machineB and machineC. 假设登录名是david ,假设我在machineB和machineC上也设置了密钥。

You can copy machineA 's public key to david@machineB and david@machineC search ssh-keygen and ssh-copy-id 您可以将machineA的公钥复制到david@machineBdavid@machineC搜索ssh-keygenssh-copy-id

After you setup the keys, you can run any command from machineA without manually keying in any password. 设置密钥后,您可以从machineA运行任何命令,而无需手动键入任何密码。 ssh david@machineB hostname

to get dir1 . 获取dir1

dir1=$(ssh david@machineB find $DIR -type d | sort | tail -1)

Use SSH to execute the command remotely and pipe the result into another program/shell script to process it. 使用SSH远程执行命令,并将结果通过管道传递到另一个程序/ shell脚本中进行处理。 ssh user@host yourscript.sh | anotherscript.sh ssh user@host yourscript.sh | anotherscript.sh . ssh user@host yourscript.sh | anotherscript.sh

ssh user@host yourscript.sh executes your command on the remote machine and prints the result on stdout , so you can capture it and redirect it to a file ( ssh user@host yourscript.sh > result.txt ) to process later or directly pipe it into another script ( ssh user@host yourscript.sh | anotherscript.sh ). ssh user@host yourscript.sh在远程计算机上执行命令并在stdout上打印结果,因此您可以捕获它并将其重定向到文件( ssh user@host yourscript.sh > result.txt )以稍后处理或直接处理将其通过管道ssh user@host yourscript.sh | anotherscript.sh到另一个脚本( ssh user@host yourscript.sh | anotherscript.sh )。

You can check $? 您可以检查$? variable for the exit status of the remote command. 远程命令的退出状态的变量。

you can use ssh 你可以使用ssh

command is ssh -p port_num user@host 命令是ssh -p port_num user @ host

port_num is the port number by default it is 22 . port_num是端口号 ,默认为22 user is the userName host is the ip or hostName . user是userName主机是ip或hostName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM