Linux bulk Change password via expect

Source: Internet
Author: User

Company recently to a batch of CDN server, need to change the password regularly, originally wanted to use automation tools to achieve this function, but recently busy no time to do, so the first way to modify the script, the Internet search for a bit, so the reference network script did a test, the results of an afternoon, Pit a little bit more, in the following I will try to explain clearly, in order to avoid new people like me to drop the pit, not much to say, the text begins!

System environment: CentOS Linux release 7.3.1611 (Core)

Selinux:disable

Execution Directory:/home

One, requirements: running in a Linux environment, TCL and expect support required
Principle: Using the Expect interactive function, log on to the specified number of servers to modify the password
a total of 2 program Files passwd.sh and Passwdaction, In addition users need to build an IP address list file, one IP per line, I named Ip_list.txt here

1. Check if the system has expect and TCL

[Email protected] home]# RPM-QA expect
Expect-5.45-14.el7_1.x86_64
[Email protected] home]# Rpm-qa tcl
Tcl-8.5.13-8.el7.x86_64
You can see that the system has both software installed, if not available with Yum install-y expect TCL installation.

password.sh Script File Contents:

#!/bin/bash

If ["$" = ""] | | ["$" = ""] | | ["$" = "--help"] | | ["$" = "-H"]
Then
echo "Usage:passwd.sh/path/ip_list.txt/path/passwdaction"
Exit
Fi
Cat $ | While Read line
Do
# if ==null
[-Z $line] && continue
$ $line;
Done
Echo-e ' \ n \ password has been changed! \ n '

The above code indicates that two parameters are required, $ and $, and if the argument is empty, prompt to use Passall.sh/path/ip_list.txt/path/passone (note that/path is the directory where the script file is located, please modify it according to your own situation)

The variable 1 is then read ip_list.txt data, and then read the variable 2 is the Passwdaction script. (I understand that the great God has objections welcome guidance)

Passwdaction the contents of the script

#!/usr/bin/expect-f


#设置连接远程用户的账号
Set Loginuser "Root"
#设置连接远程用户的密码
Set Loginpass {idcicp123}

#设置需要修改密码的用户
Set Passuser "Root"
#设置新的密码
Set Newpass "idcicp123456"
#获取IP地址列表的IP
Set ipaddr [lrange $argv 0 0]
#设置连接超时时间
Set Timeout 300
Set Cmd_prompt "]#|~]?"

#拦截远程服务器

Spawn ssh [email protected] $ipaddr

#超时时间为300s

Set Timeout 300


Expect {
-re "is sure you want to continue connecting (yes/no)?" {
Send "yes\r"
}-re "Password:" {
Send "$loginpass \ r"
}-re "Permission denied, please try again." {
Exit
}-re "Connection refused" {
Exit
} Timeout {
Exit
} EOF {
Exit
}
}

Expect {
-re "Password:" {
Send "$loginpass \ r"
}
-re $cmd _prompt {
Send "\ r"
}
}

#以下标红的地方, please find yourself a test or change the password in the local test prompt is this, if not please change the red content to the content of the system prompt, otherwise it will cause the script card here.

Send "passwd $passuser \ R";
Expect {
"New password:" {
Send "$newpass \ r"
}
"Passwd:only root can specify a user name." {
Exit
}
}

Expect {
"Retype new password:" {
Send "$newpass \ r"
}
}


Expect-re $cmd _prompt
Send "\ r"
Exit
#interact

Ii. Examples of usage:
[Email protected] home]#/home/passwd.sh/home/ip_list.txt/home/passwdaction
Description: Passwd.sh and passwdaction in the command line please use the above code, and make the corresponding changes can be
Ip_list.txt is an IP list file that needs to be built by the user, with the following examples:
192.168.1.5
192.168.1.6
192.168.1.7

Reference connection: http://blog.csdn.net/chinalinuxzend/article/details/2480228

: http://www.docin.com/p-678752172.html

Linux bulk Change password via expect

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.