expect實現(xiàn)批量修改linux密碼腳本分享
更新時間:2014年12月30日 11:11:38 投稿:junjie
這篇文章主要介紹了expect實現(xiàn)批量修改Linux密碼腳本分享,本文直接給出實現(xiàn)代碼,需要的朋友可以參考下
最近對linux批量執(zhí)行的腳本很感興趣,在網(wǎng)上到處找有關(guān)expect批量執(zhí)行腳本,今天就給大家共享一個批量修改密碼的腳本.
腳本內(nèi)容:
復(fù)制代碼 代碼如下:
#!/usr/bin/expect
if { $argc<2 } {
send_user "usage: $argv0 <host file> <cmd file> \n"
exit
}
# 機(jī)器列表數(shù)據(jù)格式: IP 端口 舊密碼 新密碼
set hostfile [ open [lindex $argv 0] ]
# 命令列表數(shù)據(jù)格式: 一條命令一行
set cmdfile [ open [lindex $argv 1] ]
# 數(shù)據(jù)文件分割符,默認(rèn)為空格
set part "\ "
# 過濾關(guān)鍵字
set key_password "password:\ "
set key_init "\(yes/no\)\?\ "
set key_confirm "'yes'\ or\ 'no':\ "
set key_ps "*]#\ "
set key_newpassword "UNIX password:\ "
set timeout 30
log_file ./exprct.log
match_max 20480
while {[gets $hostfile _hosts_] >= 0} {
set hosts [string trim $_hosts_]
set str_index [string first $part $hosts]
set host [string trim [string range $hosts 0 $str_index]]
set temp [string trim [string range $hosts [expr $str_index + 1] [string length $hosts]]]
set str_index [string first $part $temp]
if { $str_index == -1 } {
set port 22
set pass $temp
set newpass $temp
} else {
set port [string trim [string range $temp 0 $str_index]]
set temp_pass [string trim [string range $temp [expr $str_index + 1] [string length $temp]]]
set str_index [string first $part $temp_pass]
set pass [string trim [string range $temp_pass 0 $str_index]]
set newpass [string trim [string range $temp_pass [expr $str_index + 1] [string length $temp_pass]]]
}
spawn ssh -p $port $host
while {1} {
expect {
"$key_password" {
send "$pass\r"
}
"$key_init" {
send "yes\r"
}
"$key_confirm" {
send "yes\r"
}
"$key_ps" {
while {[gets $cmdfile cmd] >= 0} {
send "$cmd\r"
expect {
"$key_ps" {
continue
}
"$key_newpassword" {
send "$newpass\r"
expect "$key_newpassword" {
send "$newpass\r"
expect "$key_ps"
continue
}
}
}
}
seek $cmdfile 0 start
send_user "\r"
break
}
timeout {
puts "$host timeout\n"
break
}
}
}
send "exit\r"
close
wait
}
close $hostfile
close $cmdfile
exit
相關(guān)文章
Shell腳本判斷Linux系統(tǒng)是32位還是64位的幾種方法分享
這篇文章主要介紹了Shell腳本判斷Linux系統(tǒng)是32位還是64位的幾種方法分享,本文共給出了4種方法實現(xiàn)系統(tǒng)位數(shù)在判斷,需要的朋友可以參考下2014-12-12
shell腳本自動檢測網(wǎng)絡(luò)掉線和自動重連
這篇文章主要介紹了shell腳本自動檢測網(wǎng)絡(luò)掉線和自動重連,這篇文章介紹的是自動檢測連接網(wǎng)絡(luò)的解決方法,需要的朋友可以參考下2019-12-12
如何使用 Shell 腳本執(zhí)行 .NET Core 應(yīng)用
這篇文章主要介紹了如何使用 Shell 腳本執(zhí)行 .NET Core 應(yīng)用,幫助大家更好的理解和使用shell 腳本,感興趣的朋友可以了解下2020-09-09

