expect - maintain the interactive program.it will talk to other process via script. it is mainly used in the shell scripting to send the password & user name. These name come from send/expect.
expect >> wait for string from the process
send >> send the string to the process
while the interaction if expect get the string match with assword. it will send the password value.
expect \"*?assword:*\"
send -- \"$PASS\r\"
spawn >> start process
To write Bournce - Again Shell script To update the server details Using - expect -send -spwan command
#!/bin/bashHOST="remote-hostname"
USER="remote-user"
PASS="remore-user-password"
CMD=$@
VAR=$(expect -c "
spawn ssh -o StrictHostKeyChecking=no $USER@$HOST $CMD
match_max 100000
expect \"*?assword:*\"
send -- \"$PASS\r\"
send -- \"\r\"
expect eof
")
echo "==============="
echo “shell script Output”l
echo $VAR
Run the Shell Script
#sh execute.sh 'exim -bpc'
How To use spawn command
spawn ssh -o StrictHostKeyChecking=no $USER@$HOST $CMD
-bash: spawn: command not found
spawn command start with expect or you will get the error message
Correct Format
expect -c “spawn ssh -o StrictHostKeyChecking=no $USER@$HOST $CMD”
Wrong Format
spawn ssh -o StrictHostKeyChecking=no $USER@$HOST $CMD”
-bash: spawn: command not found
.
Main_shell_script #!/bin/sh while IFS=: read -r f1 f2 f3 do ./expect.sh $f1 $f2 $f3; done < server_details |
expect.sh expect script #!/usr/bin/expect set host [lindex $argv 0]; set user [lindex $argv 1]; set password [lindex $argv 2]; set timeout 5 #set the correct prompt here set prompt ":|#|\\\$" eval spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no $user@$host expect "*assword:*" send "$password\r" expect "$prompt" send "grep -i error /var/log/messages\r" expect "$prompt" send "exit\r" expect eof |
Server Information # cat server_details server3:username:password server4:username:password |
0 Comments