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

首頁 > 開發 > Linux Shell > 正文

shell腳本實現同時多臺遠程主機執行命令的代碼分享

2020-07-27 18:57:23
字體:
來源:轉載
供稿:網友

實現需求

在對單臺機器做操作時我們會用“ssh ip”的方式登錄到機器上,可以寫這樣一個工具vssh ip1,ip2,…ipn 來模擬登錄到n 臺服務器,登錄后所有操作相當于同時對n 臺服務器生效。

實現方法

首頁要確保可以通過本地公鑰無密碼登錄遠程主機:

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 -------------------------

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對武林網的支持。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 青神县| 罗江县| 竹北市| 夏河县| 仁布县| 仁寿县| 南丹县| 四会市| 永和县| 翼城县| 藁城市| 铜陵市| 略阳县| 杭锦后旗| 红河县| 盐城市| 蕲春县| 嵩明县| 麻栗坡县| 兴和县| 罗甸县| 鄂伦春自治旗| 清水县| 潜山县| 河南省| 郯城县| 五大连池市| 安多县| 吴江市| 江陵县| 亳州市| 西藏| 万源市| 宝丰县| 兖州市| 呼图壁县| 师宗县| 德令哈市| 太康县| 双鸭山市| 同仁县|