手机
当前位置:查字典教程网 >脚本专栏 >linuxshell >expect实现批量修改linux密码脚本分享
expect实现批量修改linux密码脚本分享
摘要:最近对linux批量执行的脚本很感兴趣,在网上到处找有关expect批量执行脚本,今天就给大家共享一个批量修改密码的脚本.脚本内容:复制代码...

最近对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 "$passr"

}

"$key_init" {

send "yesr"

}

"$key_confirm" {

send "yesr"

}

"$key_ps" {

while {[gets $cmdfile cmd] >= 0} {

send "$cmdr"

expect {

"$key_ps" {

continue

}

"$key_newpassword" {

send "$newpassr"

expect "$key_newpassword" {

send "$newpassr"

expect "$key_ps"

continue

}

}

}

}

seek $cmdfile 0 start

send_user "r"

break

}

timeout {

puts "$host timeoutn"

break

}

}

}

send "exitr"

close

wait

}

close $hostfile

close $cmdfile

exit

【expect实现批量修改linux密码脚本分享】相关文章:

nginx日志切割脚本分享

实时查看系统流量的Shell脚本分享

实现释放CentOS系统内存的Shell脚本分享

简单的Linux查找后门思路和shell脚本分享

从apnic提取ip信息脚本分享

Shell脚本实现批量下载网络图片代码分享

linux安装php扩展脚本分享

linux自动重启tomcat脚本分享

vtune自动安装脚本分享

linux删除无效链接文件脚本分享

精品推荐
分类导航