天天看點

expect互動式腳本

Solaris_change_passwd使用注意事項及說明

1. 

腳本隻測試了Solaris10的英文及中文版(oracle公司出品)

2. 

Solaris10中文版系統需要将系統字元集改為zh_CN.UTF-8

3. 

以bash執行此腳本

bash Solaris_change_passwd 使用者名($1) IP位址($2) 使用者密碼($3) 需要更改密碼的使用者名($4) 更改的使用者密碼($5)

腳本變量定義:

#!/bin/bash

USER=$1          #使用者名

IP=$2            #IP位址

PW=$3            #使用者密碼

CHE_USER=$4     #需要更改密碼的使用者名

CHE_PW=$5       #更改的密碼

ping -c 3 ${IP} >/dev/null                                      #ping 3次 $IP位址不輸出顯示

if [ "$?" -ge "1" ] ;then                                       #如果輸出結果大于等于1,則

echo "The network impassability"                                #輸出“The network impassability”

exit 1                                                          #退出此次循環

fi                                                              #結束if循環

expect -c "                                                     #調用expect腳本

   set timeout 30                                               #設定逾時間為30秒

   match_max 100000                                             #設定比對最大長度為100000

   spawn ssh $USER@$IP                                          #調用ssh程式登入

      expect {                

         \"yes\/no\" { send \"yes\n\" ; exp_continue }          #比對到yes/no時,輸入yes.繼續向下執行

         \"assword: \" {                                        #比對到“assword”時

            send \"${PW}\n\"                                    #輸入$PW

            expect \"Permission denied\" {                      #比對到“Permission denied”時

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

                                                                #輸出“nUser name password mistake, or no permissions.”

            exit 2                                              #退出2循環

            }

         \"密碼: \" {                                            # 比對到“assword”時

         send \"${PW}\n\"                                       # 輸入$PW

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

            exit 3                                              #退出3循環

            } 

         }

         \"Connection refused\" {                               #比對到“Connection refused”

         send_user \"\nSSH Service is not open\n\"              #輸出“nSSH Service is not open”

         exit 4                                                 #退出4循環

         }        

      expect \"*#*\" { send \"passwd $CHE_USER\n\" }            #比對到#号時,輸入“passwd $CHE_USER”      

      expect {        

         \"assword:\" { send \"$CHE_PW\n\" ; exp_continue }     #比對到assword時,輸入 $CHE_PW,繼續向下執行

         \"密碼:\" { send \"$CHE_PW\n\"; exp_continue }         #比對到密碼:時,輸入 $CHE_PW,繼續向下執行

      expect\"#\" { send \"exit\n\" }                           #輸入exit退出   (實在不行就幹掉)

expect eof                                                      #結束expect調用腳本

"

用法:bash gaimi.sh root 192.168.0.2 redhat aa 123123

本文轉自 Edenwy  51CTO部落格,原文連結:http://blog.51cto.com/edeny/1763051,如需轉載請自行聯系原作者