国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Linux > 正文

shell腳本實(shí)現(xiàn)同時(shí)多臺(tái)遠(yuǎn)程主機(jī)執(zhí)行命令的代碼分享

2019-10-26 18:51:03
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

實(shí)現(xiàn)需求

在對(duì)單臺(tái)機(jī)器做操作時(shí)我們會(huì)用“ssh ip”的方式登錄到機(jī)器上,可以寫這樣一個(gè)工具vssh ip1,ip2,…ipn 來(lái)模擬登錄到n 臺(tái)服務(wù)器,登錄后所有操作相當(dāng)于同時(shí)對(duì)n 臺(tái)服務(wù)器生效。

實(shí)現(xiàn)方法

首頁(yè)要確保可以通過(guò)本地公鑰無(wú)密碼登錄遠(yuǎn)程主機(jī):

ssh-copy-id [-i [identity_file]] [user@]machine

shell腳本

#!/bin/bash# -------------------------------------------------------------------------------# Author:   Loya.Chen# Description: Execute commands on multiple remote hosts at the same time.# -------------------------------------------------------------------------------set -eUsage() {  echo "Usage: $0 host1 host2 ... 'command'"}if [ $# -lt 2 ] ;then  Usage  exit 0else  cmd=${!#}filogfile=$(mktemp)i=1success=0failed=0for ip in $@;do   if [ $i -eq $# ];then    break  fi  ssh $ip $cmd &> $logfile  if [ $? -eq 0 ];then    #((success++))    success=$(($success+1))    echo -e "/n/033[32m$ip | success /033[0m /n"    cat $logfile  else    ((failed++))    echo -e "/n/033[31m$ip | failed /033[0m/n "    cat $logfile  fi  ((i++))doneecho -e '/n-------------------------'echo -e "/033[32msuccess: $success | failed: $failed /033[0m"echo '-------------------------'

示例

$ bash vssh 10.0.0.11 10.0.0.12 'free -m'10.0.0.11 | success        total    used    free   shared  buffers   cachedMem:     2871    156    2715     0     8     36-/+ buffers/cache:    111    2760Swap:     2047     0    204710.0.0.12 | success        total    used    free   shared  buffers   cachedMem:      980    615    365     0     12     69-/+ buffers/cache:    533    447Swap:     2047     0    2047-------------------------success: 2 | failed: 0 -------------------------

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)武林站長(zhǎng)站的支持。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 昌黎县| 津市市| 大同县| 亚东县| 渑池县| 浦东新区| 调兵山市| 长顺县| 日喀则市| 武乡县| 翁牛特旗| 嵊泗县| 莲花县| 河南省| 黎川县| 仪征市| 呼伦贝尔市| 博湖县| 永寿县| 延长县| 庄河市| 谷城县| 恭城| 金昌市| 海兴县| 宾阳县| 仲巴县| 禄丰县| 加查县| 麻江县| 嘉禾县| 玉林市| 修水县| 峡江县| 长海县| 肇庆市| 绵阳市| 鄯善县| 朝阳县| 藁城市| 扎兰屯市|