対話型 CHAT スクリプトでは、echo と read のキーワードを使用して、プロンプトの表示とユーザー入力の取得を行います。ユーザー入力は、接頭辞 $ によって識別される変数として格納されます。以下は、前項で紹介したスクリプトの対話型バージョンの例です。
# Interactive Chat Script Example 1 # # Set the line regarding the remote site configuration # Due to UUCP limitations some systems only accept cs7 # setline cs7 parodd send RETURN expect "ogin:" 10 onerror send BREAK repeat 3 # Display prompt for login id echo "Enter your PPP login id: " # Read the login id from standard input and store in $login read $login # Send the login id to the remote host send "$login" # Set expected response, wait up to 40 seconds expect "word: " 40 # Display prompt for password echo "Enter your PPP password: " # Read the password from standard input and store in $password read $password # Send the password to the remote host send "$password" |
次の例は、ユーザーと動的チャレンジ応答認証システムの対話を管理する、より複雑な CHAT スクリプトです。
# Interactive Chat Script Example 2
#
# Set the line regarding the remote site configuration
# Due to UUCP limitations some systems only accept cs7
# setline cs7 parodd
send RETURN
expect "ID:" 10 onerror send BREAK repeat 3
# Display prompt for user number id
echo "Enter your user ID number: "
# Read the user number id from standard input and store in $id
read $id
# Send the user number id to the remote host
send "$id"
# Set expected response, wait up to 10 seconds
# Receive the 6 character challenge code
expect "Challenge: ${challenge,6}" 10
# Display prompt for the challenge response
echo "Enter the response for Challenge ${challenge}: "
# Read the user number id from standard input
# and store in $response
read $response
# Send the challenge response to the remote host
send "$response"
# Set expected response, wait up to 20 seconds
expect "${host}:" 20
# Display status and name of remote host on successful connection
echo "Connected to ${host}¥n"
# Start dialog with remote PPP
send "ppp"
|
この例では、スクリプトはユーザー識別子を読み取ってサーバーに送信します。文字列 Challenge: で始まるサーバーの応答が 10 秒間待機され、チャレンジ値の次の 6 文字を読み取ります。
次に、スクリプトはチャレンジ値を表示し、ユーザーの応答を待機し、この応答をサーバーに送信します。応答が受け付けられると、接続が確立されます。
中括弧 ({}) は、文字列の一部として他の文字とともに示される変数を区切るために使用します。