Rlogin / RSH / SSH

Hello,

I am looking for a connection method in which i can connect to a remote server but I want to have only one chance to connect to the remote server (not to be asked for iuser name and password again).
If I have provided a wrong password then I want the connection to broke and be routed back to the local server.

I.e

rlogin <server name> -l <profile name>
<profile name> password: <User enters a wrong password>
[compat]: 3004-300 You entered an invalid login name or password.
Connection to <server name> closed.

You could do this with an expect script that took user and host as parameters, prompts for password without echo and then invokes rlogin and tries the password.
If it fails then terminate, otherwise wait till login OK message ("Last login:" in this case) and switch to an interactive session:

#!/usr/bin/expect
##assign the first parameter to address and 2nd to username
set address [lrange $argv 0 0]
set username [lrange $argv 1 1]
 
# Prompt for password (without echo) and store it
stty -echo
send_user -- "Password for $username@$address: "
expect_user -re "(.*)\n"
send_user "\n"
stty echo
set pass $expect_out(1,string)
 
#Spawn rlogin
spawn rlogin $username@$address
 
# Process result
expect {
     -re "Last login:" { interact }
     -re "an invalid password" { puts "$expect_out(buffer)";close; exit}
     -re "(p|P)assword:" {send "$pass\r";exp_continue}
     "Connection refused" {puts "Host error -> $expect_out(buffer)";close; exit}
     "Connection closed"  {puts "Host error -> $expect_out(buffer)";close; exit}
     "no address.*" {puts "Host error -> $expect_out(buffer)";close; exit}
     timeout {puts "Timeout error. Is device down or unreachable??";close; exit}
}
1 Like