2021-05-12 14:32:11
Linux叢集批次執行命令
2020-06-16 16:58:54
因為工作需要,需要修改叢集中機器的設定,一台一台的修改太浪費時間,就想能不能通過自動化指令碼批次執行命令,嘗試寫一個,自己shell不熟悉,寫的有點膚淺,請見諒。
if [ "$#" -ne 2 ];then
echo "USAGE:$0 -f host_file cmd"
exit -1
fi
file_name=$1
cmds=$2
filepath=$(cd `dirname $0`; pwd)
host_file="$filepath/$file_name"
if [ ! -e $host_file ];then
echo "$host_file not exit;"
exit 0
fi
cat $host_file | while read line
do
echo $line>>result
ssh -n -p3600 $line source ~/.bash_profile;$cmds >> result
#"source ~/.bash_profile;$cmds" > result
if [ $? -eq 0 ] ; then
echo "host:$line,$cmds done!"
else
echo "host:$line error: " $?
fi
done
執行方式xxxx.sh host_file ‘cmd’
其中xxx.sh是指令碼名稱,host_file是需要操作機器的ip地址列表,需要可以免密碼登陸;cmd是需要操作的命令,如果命令之間存在空格,則需要用引號包起來。
note:
- 1.ssh和cmd需要寫在同一行
- 2.ssh -n使用本地作為標准輸入
Redirects stdin from /dev/null (actually, prevents reading from stdin). This must be used when ssh is run in the background. A common trick is to use this to run X11 programs on a remote machine. For example, ssh -n shadows.cs.hut.fi emacs & will start an emacs on shadows.cs.hut.fi, and the X11 connection will be automatically forwarded over an encrypted channel. The ssh program will be put in the background. (This does not work if ssh needs to ask for a password or passphrase; see also the -f option.)
本文永久更新連結地址:http://www.linuxidc.com/Linux/2017-12/149556.htm
相關文章