From: Chris M. <mut...@mc...> - 2003-10-02 22:21:08
|
Hi, >Did you write something like this before. Can I have an example? Yes I did. Sure, here it is. This is more than an example, It is a fully working implementation of a so= called "looping login" scheme. It works for an arbitrary number of hosts. It was also designed to work= with ssh, and not telnet. You will have to go through and make the relevant changes to make it work= with telnet. Optionally, you might consider using ssh -if possible-, as it is more= secure than telnet. This will log you into all of the given hosts and leave you with a $exp= that is valid on the final host. This is a subroutine. The usage explanation of it is at the bottom of the= code segment. I hope this helps. -Chris Muth #### #### START OF CODE #### #!/usr/bin/perl5.8 -w use Expect; # for scripted I/O # req global; $exp, $number_of_hosts, $hostname[], $password[], sub multi_login { my($i,$j); for ($i=3D0;$i<$number_of_hosts;$i++){ if ($i=3D=3D0){ $exp->spawn("/usr/local/bin/ssh",$hostname[$i]); } else { $exp->send("/usr/local/bin/ssh ".$hostname[$i]."\n"); } $retval =3D $exp->expect(30,'-re','word:\s$','Connection refused','No= route to host','Name or service not known'); if (defined $retval){ if ($retval !=3D 1) { # $retval is 2 or 3. if ($retval =3D=3D 2){ # $retval is 2, "Connection refused". if ($i > 0){ for ($j=3D0;$j<$i;$j++){ $exp->send("logout\n"); } } print "connection refused for ".$hostname[$i]; $exp->hard_close(); exit 1; } else { if ($retval =3D=3D 3){ #$retval is 3, "No route to host". if ($i > 0){ for ($j=3D0;$j<$i;$j++){ $exp->send("logout\n"); } } print "there is no route to host ".$hostname[$i]; $exp->hard_close(); exit 1; } else { # $retval is 4, 'Name or sevice not known' if ($i > 0){ for ($j=3D0;$j<$i;$j++){ $exp->send("logout\n"); } } print "Name or service not known for host= ".$hostname[$i]; $exp->hard_close(); exit 1; } } } # $retval is 1, we got the password prompt. } else { # $retval is undefined, ssh didn't respond with anything= within 30 seconds. if ($i > 0){ for ($j=3D0;$j<$i;$j++){ $exp->send("logout\n"); } } print "ssh did not respond"; $exp->hard_close(); exit 1; } $exp->send($password[$i]."\n"); $retval =3D $exp->expect(30,'# ','Permission denied'); if (defined $retval){ if ($retval =3D=3D 2){ # $retval is 2, we got a permission denied= message. if ($i > 0){ for ($j=3D0;$j<$i;$j++){ $exp->send("logout\n"); } } print "permission denied for login to ".$hostname[$i]; $exp->hard_close(); exit 1; } # $retval is 1, we got the prompt. } else { # Got neither a prompt, nor a permission deined message. if ($i > 0){ for ($j=3D0;$j<$i;$j++){ $exp->send("logout\n"); } } print "error on login to ".$hostname[$i]; $exp->hard_close(); exit 1; } } } # instiantiate an Expect object that we will use. $exp =3D new Expect; $exp->raw_pty(1); # for use by multi_login(), how many hosts to login to. $number_of_hosts =3D 3; # List all of the hosts to login to. Start at 0. $hostname[0] =3D 'host0'; $hostname[1] =3D 'host1'; $hostname[2] =3D 'host2'; # List all of the passwords for the hosts to be logged into. Again, start= at 0. $password[0] =3D 'pass0'; $password[1] =3D 'pass1'; $password[2] =3D 'pass2'; # Call multi_login(), it will use $exp, $number_of_hosts, $hostname[], and= $password[]. multi_login(); # From here on out, $exp is valid on hostname[<max>]. # Just use it as normal. # Modify the prompt to something easy to match, and easy on bandwidth. $exp->send("PS1=3D\"# \"\nexport PS1\n"); $exp->expect(30,'# '); $exp->expect(30,'# '); $exp->send("ls -l\n"); $exp->expect(30,'# '); # this will make expect wait, as sleep() does not work with expect scripts. $exp->expect(5,'this call will make expect sleep'); $exp->hard_close(); print "\n"; exit 0; ##### ##### END OF CODE ##### |