expect实现批量修改linux密码脚本分享
linux  /  管理员 发布于 7年前   201
最近对linux批量执行的脚本很感兴趣,在网上到处找有关expect批量执行脚本,今天就给大家共享一个批量修改密码的脚本.
脚本内容:
复制代码 代码如下:
#!/usr/bin/expect
if { $argc<2 } {
send_user "usage: $argv0 <host file> <cmd file> \n"
exit
}
# 机器列表数据格式: IP 端口 旧密码 新密码
set hostfile [ open [lindex $argv 0] ]
# 命令列表数据格式: 一条命令一行
set cmdfile [ open [lindex $argv 1] ]
# 数据文件分割符,默认为空格
set part "\ "
# 过滤关键字
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
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号