Linux shell 腳本批量登錄機器執行命令


0x00:場景:

  服務器管理員,管理的機器是幾十、幾百、幾千數量級別的,

  當某些情況下,只能登錄ssh命令行去執行命令時,難道要手工?當然不是,肯定是要腳本自己去登錄執行命令。

0x01:腳本需要用到expect命令,安裝方法

# yum -y install expect

  

0x02:執行命令腳本,auto_search_file.sh

#!/bin/bash
## file name : auto_search_file.sh

## 判斷輸入參數個數為0,提示並退出
if [ $# -eq 0  ];then
        echo -e "\nUsage: \n\t sh $0 command\n"
        exit
fi
## 判斷 ip_lists.txt 文件是否存在,不存在提示並退出
if [ ! -e ip_lists.txt  ];then
        echo -e "\nError:  Not \"ip_lists.txt\" file\n"
        exit
fi

## 以下, ip_lists_completes.txt和ip_lists_black.txt任一文件內容為空
## 則ip_lists_temp.txt文件也為空
## 插入內容隨意,但最好是IP格式,防止奇怪問題
## 寫127.0.0.1和127.0.0.2只是區分在什么狀態下執行的

##判斷ip_lists_completes.txt文件是否存在,不存在則創建
if [ -e ip_lists_completes.txt ];then
        ## 文件存在,且內容為空則插入內容127.0.0.2
        count_comple=`wc -l ip_lists_completes.txt|awk '{print $1}'`
        if [ ${count_comple} -eq 0 ];then 
                echo "127.0.0.2" >ip_lists_completes.txt
        fi
else
        ##文件不存在則創建,並插入內容127.0.0.1
        echo "127.0.0.1" >ip_lists_completes.txt
fi
##判斷ip_lists_black.txt文件是否存在,不存在則創建
if [ -e ip_lists_black.txt ];then
        ## 文件存在,且內容為空則插入內容127.0.0.2
        count_black=`wc -l ip_lists_black.txt|awk '{print $1}'`
        if [ ${count_black} -eq 0 ];then 
                echo "127.0.0.2" >ip_lists_black.txt
        fi
else
        ##文件不存在則創建,並插入內容127.0.0.1
        echo "127.0.0.1" >ip_lists_black.txt
fi

## 傳入要執行的命令
sh_cmd=$1
## 過濾ip_lists_black.txt黑名單里IP和ip_lists_completes.txt已經執行過的IP
## 未執行的IP放到ip_lists_temp.txt
cat ip_lists.txt |grep -v "`cat ip_lists_black.txt`" |grep -v "`cat ip_lists_completes.txt`" > ip_lists_temp.txt
## 將多行,合並成一行,用空格分隔
ip_lists=`cat ip_lists_temp.txt | tr "\n" " "`

## 遍歷IP列表並執行命令
for ip_list in ${ip_lists}
do
        ## 提示
	echo -e "\n---------- Currect_IP=${ip_list} -----------------"
	echo "Currect_IP=${ip_list}"
        ## 獲取密碼列表里的密碼,tail -1 要最后一個,是最新的密碼
	ip_passwd=`grep ${ip_list} /home/test/get_password/pwd_history.txt |tail -1 | awk -F ":" '{print $2}'`
        ## 如過密碼為空,則調用get_password.sh文件匹配密碼
	if [ -z ${ip_passwd} ];then
		echo "Currect_IP=${ip_list} Password is NULL."
		cd /home/zxl/auto_get_password/
                ## 傳入當前IP匹配密碼
		sh auto_get_password.sh ${ip_list}
                ## 等待1秒,讓緩存寫入文件
		sleep 1
                ## 回到原目錄
		cd -
                ## 再次獲取密碼
		ip_passwd=`grep ${ip_list} /home/test/get_password/pwd_history.txt |tail -1 | awk -F ":" '{print $2}'`
	fi
	
	echo ""
        ## 給expect傳入IP 、密碼來執行命令。 記錄日志tee -a $0.log,$0當前腳本的名字
	expect auto_search_file.exp ${ip_list} ${ip_passwd} "${sh_cmd}" | tee -a $0.log
	## search completes ip to file
	## 記錄執行完成的IP,防止腳本異常退出,下次啟動重頭開始執行
	echo "${ip_list}" >> ip_lists_completes.txt 
done

## 全部執行完成,則刪除ip_lists_completes.txt文件
echo "[ info ] rm ip_lists_completes.txt"
\rm ip_lists_completes.txt
## 提示,執行命令完成
echo "[ OK ]  Exec command done"
 

  *注:auto_get_password.sh如何匹配密碼,查看另一篇文章https://www.cnblogs.com/wutou/p/15738391.html

 0x03:執行命令expect腳本auto_search_file.exp

#!/usr/bin/expect
## file name : auto_search_file.exp

## 檢查傳入參數不等於3個,提示並退出
if { $argc != 3 } {
	#send_user "Usage: expect $argv0 IP Passwd cmd"
	puts "\nUsage: \n\texpect $argv0 IP Passwd cmd\n"
	exit
}

## 傳入3個參數賦值
set ip [lindex $argv 0]
set passwd [lindex $argv 1]
set cmd [lindex $argv 2]

## 用root用戶執行cmd變量里命令
spawn ssh root@$ip "$cmd"
expect {
	## 提示信息里有yes/no 發送yes
	"yes/no"	{send "yes\r";exp_continue}
	## 提示信息里有password 發送密碼
	"*password*"	{send "$passwd\r"}
}
send_user "\n"
expect eof
send_user "\n"

  

0x04: 遠程調用

   因為某些原因,本地執行腳本訪問IP會有限制,比如“安全人員”只允許員工機器登錄很少的IP網段,但是服務可以訪問所有網段。  

  我們可把get_password.sh文件放到服務的某個路徑下,我們從本地傳參給服務器調用。

  這里我們把上面的get_password.sh腳本,放到服務器上test用戶的"家"目錄下,絕對路徑/home/test/auto_search_file/auto_search_file.sh

文件名:l_auto_search_file.sh

#!/bin/bash
## file name : l_auto_search_file.sh
expect 50_auto_search_file.exp "$1" | tee -a $0.log

文件名:50_auto_search_file.exp (因為IP里帶50,所以文件名前面加了50好區分)

#!/usr/bin/expect
## file name : 50_auto_search_file.exp

## 獲取傳入的參數
set cmd  [lindex $argv 0]
set cmd_temp "cd auto_search_file && sh auto_search_file.sh '$cmd'" 
set password "test123"

## 用ssh方式登錄,並執行cmd_temp里命令
## expect語法里,要執行的命令必須要能夠“”括起來,比如"$cmd_temp"
spawn ssh test@192.168.1.50 "$cmd_temp" 
expect "*password" {send "$password\n"}
expect eof

  

0x05:使用

本地使用:

# sh auto_search_file.sh "ls -l"

遠程調用:

# sh l_auto_search_file.sh "ls -l"

 

 

-


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM