足球游戏_中国足彩网¥体育资讯$

expect实现批量修改linux密码脚本分享
来源:易贤网 阅读:1482 次 日期:2015-01-30 14:51:11
温馨提示:易贤网小编为您整理了“expect实现批量修改linux密码脚本分享”,方便广大网友查阅!

最近对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

中国足彩网信息请查看IT技术专栏

中国足彩网信息请查看脚本栏目
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 足球游戏_中国足彩网¥体育资讯$ 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:hfpxwx
咨询QQ:526150442(9:00—18:00)版权所有:易贤网
云南网警报警专用图标