Get the App
SLTechnology News&Howtos  ›  Database  › 

Expect interactive script

Shulou Source: shulou.com Published: 2022-06-01 04:52:44 10月03日 Update

Notes and instructions for the use of Solaris_change_passwd

1.

The script only tests the English and Chinese versions of Solaris10 (produced by oracle)

two。

The Solaris10 Chinese version system needs to change the system character set to zh_CN.UTF-8

3.

Execute this script as bash

Bash Solaris_change_passwd user name ($1) IP address ($2) user password ($3) user name that needs to be changed ($4) changed user password ($5)

Script variable definition:

#! / bin/bash

USER=$1 # user name

IP=$2 # IP address

PW=$3 # user password

CHE_USER=$4 # username that needs to change the password

CHE_PW=$5 # changed password

Ping-c 3 ${IP} > / dev/null # ping 3 times $IP address does not output display

If ["$?"-ge "1"]; then # if the output is greater than or equal to 1, then

Echo "The network impassability" # output "The network impassability"

Exit 1 # exits this cycle

Fi # ends the if loop

Expect-c "# call expect script

Set timeout 30 # sets the timeout to 30 seconds

Match_max 100000 # sets the maximum matching length to 100000

Spawn ssh $USER@$IP # call the ssh program to log in

Expect {

\ "yes\ / no\" {send\ "yes\ n\"; when exp_continue} # matches yes/no, enter yes. Continue to execute downward

\ "assword:\" {# matches to "assword"

Send\ "${PW}\ n\" # enter $PW

When expect\ "Permission denied\" {# matches "Permission denied"

Send_user\ "\ nUser name password mistake, or no permissions.\ n\"

# output "nUser name password mistake, or no permissions."

Exit 2 # exit 2 Loop

}

}

\ "password:\" {# matches "assword"

Send\ "${PW}\ n\" # enter $PW

When expect\ "Permission denied\" {# matches "Permission denied"

Send_user\ "\ nUser name password mistake, or no permissions.\ n\"

# output "nUser name password mistake, or no permissions."

Exit 3 # exit 3 Loop

}

}

\ "Connection refused\" {# matches to "Connection refused"

Send_user\ "\ nSSH Service is not open\ n\" # output "nSSH Service is not open"

Exit 4 # exit 4 Loop

}

}

Expect\ "* # *\" {send\ "passwd $CHE_USER\ n\"} # when matching to #, enter "passwd $CHE_USER"

Expect {

\ "assword:\" {send\ "$CHE_PW\ n\"; exp_continue} # when matching to assword, enter $CHE_PW to continue execution

\ "password:\" {send\ "$CHE_PW\ n\"; exp_continue} # matches password: enter $CHE_PW to continue execution

}

Expect\ "#\" {send\ "exit\ n\"} # enter exit to exit (kill if you can't)

Expect eof # end expect invocation script

"

Usage: bash gaimi.sh root 192.168.0.2 redhat aa 123123

Tags: User input password output script Loop user name password address Chinese version system Chinese No maximum item Company variable character set Note Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Linux Huawei macOS vpn