Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automating a Prgm with EXPECT

Status
Not open for further replies.

mehargags

IS-IT--Management
Jun 17, 2003
1
IN
I'll be greatful if Any of u Scriptor's there can help me out automating A program that i need to run on every start up. Its called 24. I must login from this program & it authenticates me to my gateway, only after login i can use my internet. My IP is 172.16.1.107 & the gateway is 172.16.0.1

my userrname & password r being given. no problems of security as no one on this world can use that usrername & password specific to my LAN.
1."24" is the binary i am running. -u is to give it username
2. "GaganPaul" is the username i pass to it & then press ENTER
3. asks for the password in next line
4. i put password "cute" there & press ENTER

----xterm text goes here wot my screen looks lik-----
root@scavan:~# 24 -u GaganPaul
Password:
root@scavan:~#
--------------------------------------


it then logs in & to check i ping yahoo.com. if ping is success i am logged into my gateway. Can u write me a Single file, doesnt need to have args as i can hardcode my username & pass(no security issues here) & just call it in the rc.d to autorun at start up.

I heard "Expect"(a tcl application) can do that user intake kionda thing along with a combination of a "send" command.
My program file is available at Sourceforge.net here
thx alot for bearing me guyz ;)
 
Here is a mostly untested example.
If you need more help with it my email is
nomobaloney@hotmail.com. It should get
you started in any case.

Code:
#!/usr/bin/tclsh
package require Expect

#init variables and debugging
set name ""
set password ""
set pname ""
set destination "[URL unfurl="true"]www.yahoo.com"[/URL]
set name [expr {[string length $name] > 0 ? $name : [lindex $argv 0]}]
set password  [expr {[string length $password] > 0 ? $password : [lindex $argv 1]}]
#exp_internal 1
#end init

#procedure spawns ping and snarfs stats, returns 1 for success -1 for fail.
#accepts ping target first and then any args to ping.
#ex: pinger [URL unfurl="true"]www.yahoo.com[/URL] {-c 4 -I 192.167.34.2 -s 128}
#note if you specify a quiet or alternate response output from ping this will 
#break the procedure
proc pinger {target {p_args ""}} {
global expect_out 
set timeout 20

   eval spawn ping $p_args $target
   set ping_id $spawn_id
   
    expect {
           -re ".*bytes.*:(.*ms)" {
                 send_user "Got response from $target = $expect_out(1,string)\n"
                 close $ping_id 
                 return 1
            }

           eof {
                send_user "Ping quit, last read = $expect_out(buffer)\n"
                return -1
           }

           timeout {
                    send_user "No response from $target in $timeout..quitting\n"
                    close $ping_id 
                    return -1 
            }
      }
}

#sets the timeout period, spawns the main program
#looks for a password propmt, sends the recorded 
#password to the program, then enters a 4 second loop waiting for a 
#positive response from the ping to destination. If connection is up
#then we close(?) and exit. Else we try a few more times before giving 
#up.         
#main()
set timeout 300
    spawn -noecho $pname $name
    set id $spawn_id

           expect {
                   -re ".*assw.*" {
                            send_user "\nRead PW prompt"
                            send -i $id "$password\r"
                            #kludge
                            set i 0
                            while {$i < 3} {
                                   set flg [pinger $destination]
                                   if {$flg > 0} {
                                       send_user &quot;Online..\n&quot;
                                       close $id ; wait
                                   } else {
                                       after 1500;
                                       incr i
                                   }
                            }
                            send_user  &quot;Could not verify connection, quitting\n&quot;
                            close $id ; wait
                      }

                     eof {
                          send_user &quot;Abnormal program termination: last read = $expect_out(buffer)\n&quot;
                          exit
                     }
                     
                     timeout {
                          send_user &quot;Timed out waiting for password prompt\n&quot;
                          exit
                      }
           }

Uncomment exp_internal 1 to see the expect engine's
output as it executes and looks for matches.
The only thing I'm not real clear on is if your
client program needs to stay active in which case
after feeding the password to the program and calling
close you'll have trouble.

The cure is just to call interact and set the timeout to
-1. Doing it this way you could add a section something
like:

Code:
   if {[pinger $destination] > 0} {
        interact {
              &quot;quit&quot; {close $spawn_id ; wait}
         }
   }

Hope this helps you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top