Quantcast
Channel: Active questions tagged expect - Unix & Linux Stack Exchange
Viewing all articles
Browse latest Browse all 159

Exit out from loop on incorrect password in expect TCL shell script

$
0
0

I have written a code snippet in TCL expect to connect from Server 1 to Server 2 using ssh. Users already may have set passwordless communication between Server 1 and Server 2, or it may ask for a password. Code should handle all these three below possible scenarios:

  1. If passwordless login is enabled between Server 1 and Server 2
  2. No Passwordless login is enabled, Password: prompt is displayed
  3. If user enters incorrect Password, a second Password: prompt is expected where ctrl+c will be entered and will exit.

Please find below the code

#!/usr/bin/expect -flassign $argv 1 2 spawn ssh -o StrictHostKeyChecking=no $1@$2expect {"*]#" { send -- "sleep 0\n" }  #Prompt when passwordless is active and user have logged in"Password: " {send -- "$2\n"   #Password prompt when no passwordless is active.             exp_continue }"Password: " {send -- "^C" }   # Second Password Prompt when the wrong password is entered                                        in previous step. From here code should exit with a message       } expect "*]#"                # After SSH is successful

I am not able to handle the second Password: prompt for an incorrect password where the code should exit with an appropriate message to the user. I am running the script ./testssh.exp username password.


Viewing all articles
Browse latest Browse all 159

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>