天天看點

shell 腳本 批量更改linux主機密碼

腳本功能

實作了批量自動更改密碼,輸入的ip位址進行分組,然後循環放在背景并行執行。使用了随機密碼,然後在最後循環結束的時候,放入到xmima.txt 文檔中。

使用了expect 這個來進行免互動的操作,如果執行出錯,需要先安裝expect 這個安裝包

#!/bin/bash
#
#********************************************************************
#Author:		liuhao
#QQ: 			1921160095
#Date: 			2019-11-06
#FileName:		ip.sh
#Description:		The test script
#Copyright (C): 	2019 All rights reserved
#********************************************************************
read -p "請輸入起始ip: " beginip
read -p "請輸入結束ip: " endip
read -p "請輸入使用者: " user
read -p "請輸入密碼: " ymima
begarray=(${beginip//./ })
endarray=(${endip//./ })
if [ "${begarray[0]}" = "${endarray[0]}" -a "${begarray[1]}" = "${endarray[1]}" -a "${begarray[2]}" = "${endarray[2]}" ]; then
	while [ "${begarray[3]}" -le "${endarray[3]}" ];do
		if ping -c1 -w1 ${endarray[0]}.${endarray[1]}.${endarray[2]}.${begarray[3]} &> /dev/null ; then
		xmima=`cat /dev/urandom | head -c 10 | md5sum | head -c 6`
expect <<eof
set timeout 10
spawn ssh [email protected]${endarray[0]}.${endarray[1]}.${endarray[2]}.${begarray[3]}
expect {
"yes/no" {send "yes\n";exp_continue}
"password" {send "$ymima\n"}
}
expect "]#" { send "echo $xmima |passwd --stdin $user\n" }
send "exit\n"
expect eof
eof
			echo ${endarray[0]}.${endarray[1]}.${endarray[2]}.${begarray[3]}  xmima is $xmima >> xmima.txt
		else
			echo  ${endarray[0]}.${endarray[1]}.${endarray[2]}.${begarray[3]} host is unreachable >> error.txt
		fi 
		let begarray[3]++
	done
else
	echo 僅支援變動最後一位1~254
fi 

           

繼續閱讀