Expect in Linux to bulk modify user password scripts

Source: Internet
Author: User
Tags trim ssh

1, before using expect, need to install two RPM package,

The code is as follows Copy Code

# RPM-IHV expect-5.43.0-8.el5.i386.rpm
# RPM-IHV expect-devel-5.43.0-8.el5.i386.rpm


2, the batch modifies the password the script.

The code is as follows Copy Code

#!/usr/bin/expect
#yemaosheng. com
If {$ARGC <2} {
Send_user "Usage: $argv 0 Exit
}

# machine List data format: IP port old password new password
Set hostfile [Open [lindex $argv 0]]
# command List data format: A single command line
Set cmdfile [Open [lindex $argv 1]]

# data file delimiter, default to Space
Set part "\"

# Filter Keywords
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 $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 $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 $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


3, the batch modifies the password the script.

Determine expect position with Whereis expect

The code is as follows Copy Code

[Root@rac1 ~]# Whereis expect
Expect:/usr/bin/expect

#!/usr/bin/expect
#设置变量
Set Timeout 10
Set USERNAME etnet
Set PASSWORD 123456
#一个循环, describe which machines are operating
foreach Host {
192.168.151.89
192.168.151.90
} {
Spawn SSH
-L Root ${host}
#ssh首次登陆的验证, Exp_continue will continue to perform the next cycle
Expect {
"No)"? {send "yes\r"; Exp_continue}
"Password:" {send "123456\r"}
}
#每个expect捕获一个提示符, send sends a command that ends with \ r.
Expect "]*"
Send "passwd ${username}\r"
Expect "Password:"
Send "${password}\r"
Expect "Password:"
Send "${password}\r"
Expect "]*"
Send "exit\r"
}


Add: Expect usage

1. [#!/usr/bin/expect]

This line tells the operating system that the code in the script uses that Shell to execute. Here's the expect in fact and Linux under Bash, Windows under the CMD is a kind of thing.

Note: This line needs to be in the first line of the script.

2. [Set timeout 30]

Basically know English knows this is set timeout time, now you just remember his unit of timing is: seconds. Timeout-1 is never timed out

3. [Spawn ssh-l username 192.168.1.1]

Spawn is the expect internal command that can be executed after entering the expect environment, and cannot find the spawn command if it is not installed expect or executed directly under the default shell. So do not use the "which spawn" order to find the Spawn command. Like the dir in Windows is an internal command, which is brought by the shell and you cannot find a dir.com or Dir.exe executable file.

Its main function is to add a shell to the SSH running process to pass the interactive instruction.

4. [Expect "Password:"]

Here expect is also an internal command of expect, a bit dizzy, expect shell command and internal command is the same, but not a function, the habit is good. The meaning of this command is to determine whether the last output contains "password:" string, if there is a return immediately, or wait for a period of time to return, where the waiting length is the previous set of 30 seconds

5. [Send "ispass\r"]

This is the interactive action, which is equivalent to the manual input of the password.

Hint: the end of the command string do not forget to add "\ r", if there is an abnormal waiting state can be checked.

6. [Interact]

After the completion of the implementation of the interactive State, the control authority to the console, this time can be manually operated. If you do not have this login completed, you will quit, rather than stay on the remote terminal. If you just log in past execution

7. Array of $ARGV parameters

The expect script can accept arguments passed from bash. You can use [lindex $argv n] to get, n starting from 0, respectively, the first, the second, the third .... Parameters

Related Article

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.