简体   繁体   English

Shell脚本可在远程执行命令,然后退出Shell可在本地执行下一步操作

[英]Shell script to execute commands in remote and then exit shell to perform next operation in local

I'm writing a shell script to perform a series of actions in remote. 我正在编写一个Shell脚本以在远程执行一系列操作。 Then I want to come back to local and perform the next series of actions. 然后,我想回到本地并执行下一系列动作。 When I use exit, I'm exiting from the shell script instead of logging out of the remote machine. 当我使用exit时,我将退出shell脚本,而不是退出远程计算机。

set -x

ssh $1

cd /var/log/sysstat/

for (( i = 11; i <= 19; i++ ))

do

 sar -f $i >> /home/sirish.aditya/cpu_11-19.csv

 sar -f $i -r >> /home/sirish.aditya/mem_11-19.csv

done

exit 

mkdir /Users/sirish.aditya/workSpace/cpustats_srm/$1

scp sirish.aditya@$1:/home/sirish.aditya/mem_11-19.csv /Users/sirish.aditya/workSpace/cpustats_srm/$1

scp sirish.aditya@$1:/home/sirish.aditya/cpu_11-19.csv /Users/sirish.aditya/workSpace/cpustats_srm/$1

-- -

ssh sirish.aditya@remote_machine 'bash -s' < memory.sh remote_machine

Can somebody help on this? 有人可以帮忙吗?

Thanks in advance 提前致谢

You can extract the part of the remote execution to external script and then conditional piping it to exit function. 您可以将远程执行的一部分提取到外部脚本中,然后有条件地通过管道将其exitexit功能。

For example: 例如:

exec_in_remote.sh : exec_in_remote.sh

#!/bin/bash

cd /var/log/sysstat/    
for (( i = 11; i <= 19; i++ ))
do
 sar -f $i >> /home/sirish.aditya/cpu_11-19.csv
 sar -f $i -r >> /home/sirish.aditya/mem_11-19.csv
done

And then in your main script: 然后在您的主脚本中:

set -x
ssh $1 'bash -s' < exec_in_remote.sh

mkdir /Users/sirish.aditya/workSpace/cpustats_srm/$1
scp sirish.aditya@$1:/home/sirish.aditya/mem_11-19.csv /Users/sirish.aditya/workSpace/cpustats_srm/$1
scp sirish.aditya@$1:/home/sirish.aditya/cpu_11-19.csv /Users/sirish.aditya/workSpace/cpustats_srm/$1

And run it as you mentioned above: 并按照上面提到的方式运行它:

ssh sirish.aditya@remote_machine 'bash -s' < memory.sh remote_machine

Try this, 尝试这个,

set -x

ssh $1 <<'EOT'

cd /var/log/sysstat/

for (( i = 11; i <= 19; i++ ))

do

 sar -f $i >> /home/sirish.aditya/cpu_11-19.csv

 sar -f $i -r >> /home/sirish.aditya/mem_11-19.csv

done
EOT

mkdir /Users/sirish.aditya/workSpace/cpustats_srm/$1

scp sirish.aditya@$1:/home/sirish.aditya/mem_11-19.csv /Users/sirish.aditya/workSpace/cpustats_srm/$1

scp sirish.aditya@$1:/home/sirish.aditya/cpu_11-19.csv /Users/sirish

Uses a here doc to send the commands you are trying to run remotely. 使用此处文档发送您要远程运行的命令。

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

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