Expect scripting telnet stop on bad username or password


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Expect scripting telnet stop on bad username or password
# 1  
Old 10-10-2012
Expect scripting telnet stop on bad username or password

I am trying to build and expect script to log into multiple aix boxes and change password. I need for the script to terminate if it cannot log into a server because the username or password is wrong.
Code:
#!/usr/bin/expect
set timeout 1
set host [lindex $argv 0]
set user [lindex $argv 1]
set password [lindex $argv 2]
set uh "Unknown host"
set ivl "\[compat\]\: You entered an invalid login name or password"
set lg "login"
set pwd "Password:"
set prp "$host"
proc login {user password} {
expect "Login:"
send "$user\r"
expect "Password:"
send "$password\r"
}
spawn telnet $host
expect {
$uh {
send_user "Unknown Host\n"
}
$lg {
send $user\r ; exp_continue
}
$pwd {
send $password\r
expect {
"#" {send hostname\r}
"$" {send hostname\r}
">" {send hostname\r}
$ivl { send_error "Invalid Username or Password\n" }
}
}
}
expect closed
exit 0


Last edited by Franklin52; 10-11-2012 at 05:25 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 10-10-2012
This is an example to try:
Code:
#!/usr/bin/expect
set timeout 1
set host     [lindex $argv 0]
set user     [lindex $argv 1]
set password [lindex $argv 2]

proc login {user password} {
  expect -re "(Login|login).*:.*"            { exp_send "$user\r"; exp_continue } \
         -re "(Password|password).*:.*"      { exp_send "$password\r"; exp_continue } \
         -re "(#|\$|>)"                       { return 0 } \
         eof                                 { puts "***Error connecting to($host)."; return 1 }
         timeout                             { puts "***Error connecting to($host)."; return 1 }
  }
}

spawn telnet $host
set login_results [login $user $password]

# If successful connection, continue, else exit denoting error occured.
if { $connect_results == 0 } {

  # Successful telnet session so exit with zero status
  exit 0
}
# Error connecting to host so exit with non-zero status
exit 1


There is another expect telnet example here:
Expect - Wikipedia, the free encyclopedia
Expect scripts for automatic login with telnet and SSH Sasha's weblog
# 3  
Old 10-12-2012
Error on the expect

The scrip keeps coming up with an error.

invalid command name "}"
while executing
"}"
(file "./pwd2.exp" line 16)
# 4  
Old 10-12-2012
Ok, Try this:
Code:
#!/usr/bin/expect
set timeout 10
set host     [lindex $argv 0]
set user     [lindex $argv 1]
set password [lindex $argv 2]

proc login {host user password} {
  expect {
    "nkown host"                        { puts "***Host($host) is unkown."; return 1 }
    "onnection refused"                 { puts "***Connection was refused to host($host)."; return 1 }
    "Escape character is '^]'."         { exp_continue }
    -re "Login:.*$|login:.*$"           { exp_send "$user\r"; exp_continue }
    -re "Password:.*$|password:$"       { exp_send "$password\r"; return 0 }
    timeout                             { puts "***Telnet timed out waiting for host($host)."; return 1 }
  }
}

catch { spawn -noecho telnet $host }
set login_results [login $host $user $password]

# If successful connection, continue, else exit denoting error occured.
if { $login_results == 0 } {
  puts "\r***Connected; You are on the command line..."
  interact
  # Successful telnet session so exit with zero status
  exit 0
}
# Error connecting to host so exit with non-zero status
exit 1

This User Gave Thanks to spacebar For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing username and password to a script running inside "expect" script

Hi I'm trying to run a script " abc.sh" which triggers "use.sh" . abc.sh is nothing but a "expect" script which provides username and password automatically to the use.sh script. Please find below the scripts: #abc.sh #!/usr/bin/expect -f exec /root/use.sh expect "*name*" send... (1 Reply)
Discussion started by: baddykam
1 Replies

2. Shell Programming and Scripting

Username and password

Hi I am new to using unix and am struggling with a script i am writing. What i am trying to do is get a user to enter a username, check the original file i created with username and pin to see if their is a corresponding entry. Next ask the user to enter the pin and see if this matches... (5 Replies)
Discussion started by: somersetdan
5 Replies

3. Shell Programming and Scripting

Expect script - going in loops can't stop

Hi First of all I tried lot of forums to create my first expect script. But I am totally stumped at the last step. I am no Linux Admin or ever trained in Linux. I just tried to create the script logically History : I need to to change my password across lot of servers in Linux over ssh ... (4 Replies)
Discussion started by: radioactive9
4 Replies

4. Shell Programming and Scripting

Scripting help/advise on hiding/masking username/password

Hi, I currently have a UNIX script with a function that uses a username and password to connect to the database, retrieve some information and then exit. At the moment, am getting the username and password from a hidden plain text file and permission set to -r--------, i.e. read only to who... (1 Reply)
Discussion started by: newbie_01
1 Replies

5. Shell Programming and Scripting

Not able to pass Username and Password in telnet session

I have created a shell script to telnet remote machine. Here is the problem I am not able to pass the login username and password to the telnet session. I have searched forum and got few other methods to achieve this. But I need to know what's wrong in the below script. username="root"... (2 Replies)
Discussion started by: uxpassion
2 Replies

6. Ubuntu

Ubuntu Server 810 Telnet ** I know its Bad.. But **

I have a real need to telnet into my new Ubuntu 8.10 server and have Googled my eyes out with ideas that don't work. I CAN telnet out of the server, but I CANNOT telnet into it (connection refused error). I have updated my resolv.conf file but its expert time. ANY IDEAS??? THANKS!!! I knew I was... (13 Replies)
Discussion started by: gbxfan
13 Replies

7. Shell Programming and Scripting

strange expect script behavior, or am i misunderstanding expect scripting?

Hello to all...this is my first post (so please go easy). :) I feel pretty solid at expect scripting, but I'm running into an issue that I'm not able to wrap my head around. I wrote a script that is a little advanced for logging into a remote Linux machine and changing text in a file using sed.... (2 Replies)
Discussion started by: v1k0d3n
2 Replies

8. Shell Programming and Scripting

Webpage to Telnet via Perl and Expect: Telnet problem?

Somewhat long story: I have a simple Perl CGI script that uses Expect to Telnet to a device and grab some data, and then spits it back to Perl for display on the Webpage. This works for many devices I've tried, but one device just fails, it keeps rejecting the password on this device, only... (1 Reply)
Discussion started by: jondo
1 Replies

9. UNIX for Dummies Questions & Answers

how to stop telnet access to AIX box

how do I remove telnet access to my AIX (5.3) machine? I just want people (admins) to connect to the box using ssh? Thanks. (2 Replies)
Discussion started by: johnbrickell
2 Replies

10. Shell Programming and Scripting

username password in script

Can we write a script to telnet to a unix server from unix with the username and password hardcoded in the script?? something like ssh a@b -p password ??? (5 Replies)
Discussion started by: roshanjain2
5 Replies
Login or Register to Ask a Question