From: Hailey N. <Hai...@Su...> - 2003-10-01 18:09:00
|
Hi, Please show me how to do nested telnet sessions using Expect.pm module? 1- telnet to machine A 2- from A to mcahine B 3- from M to another machine C ----- use Expect; my $exp = Expect->spawn("telnet $somehost") or die "Cannot spawn telnet: $!\n"; ... ----- Your help is greatly appreciated. Please reply directly to me since I am not on the alias. Hailey. |
From: Chris M. <mut...@mc...> - 2003-10-01 19:05:56
|
Hey, First you will have to login to A. That involves what you have, plus you= will have to $exp->expect() for whatever telnet responds with when it asks for the password. Then use a= $exp->send() to send the password. Then $exp->expect() for the prompt on A. Once you are logged into A, use a= $exp->send() to call telnet $B on A to login to B, $exp->expect() for the password prompt, and then $exp->send() it. Once= logged into B, do the same to login to C, and the same for D, and the same for ... -Chris On 10/1/03 at 11:07 AM Hailey Nguyen wrote: >Hi, > >Please show me how to do nested telnet sessions using Expect.pm module? > >1- telnet to machine A >2- from A to mcahine B >3- from M to another machine C > >----- >use Expect; > >my $exp =3D Expect->spawn("telnet $somehost") > or die "Cannot spawn telnet: $!\n"; > >... > >----- >Your help is greatly appreciated. Please reply directly to me since >I am not on the alias. > >Hailey. > > |
From: Hailey N. <Hai...@Su...> - 2003-10-01 20:33:58
|
Hi Chris, Thanks so much for the help! I was able to telnet into the hostA then the next one it keeps looping around the statement below and won't quit. If I take out the "\n"" character it won't loop but it won't go further either. $fh->send("telnet $hostsB\n"; Help Help Help ... thanks again. H ----------- !/bin/perl use Expect; $Expect::Log_Stdout = 1; # variables define here # ... my $exp = Expect->spawn("telnet $hostA") or die "Cannot spawn telnet: $!\n"; my $spawn_ok; $exp->log_file("testlog"); $exp->expect($timeout, [ qr'login: $', sub { $spawn_ok = 1; my $fh = shift; $fh->send("$username\n"); exp_continue; } ], [ 'Password: $', sub { my $fh = shift; print $fh "$password\n"; exp_continue; } ], [ '# $', sub { $spawn_ok = 1; my $fh = shift; $fh->send("telnet $hostB\n"); <================ exp_continue; } ], [ 'login: $', sub { my $fh = shift; print $fh "$username\n"; exp_continue; } ], [ 'Password: $', sub { my $fh = shift; print $fh "$password\n"; exp_continue; } ], [ eof => sub { if ($spawn_ok) { die "ERROR: premature EOF in login.\n"; } else { die "ERROR: could not spawn telnet.\n"; } } ], [ timeout => sub { die "No login.\n"; } ], '-re', qr'[#] $' # wait for shell prompt, then exit expect ); $exp->hard_close(); ------------------------- > > First you will have to login to A. That involves what you have, plus you > will have to $exp->expect() for whatever telnet responds with when it > asks for the password. Then use a $exp->send() to send the password. > Then $exp->expect() for the prompt on A. Once you are logged into A, use > a $exp->send() to call telnet $B on A to login to B, $exp->expect() for > the password prompt, and then $exp->send() it. Once logged into B, do > the same to login to C, and the same for D, and the same for ... > > -Chris > > > On 10/1/03 at 11:07 AM Hailey Nguyen wrote: > >> Hi, >> >> Please show me how to do nested telnet sessions using Expect.pm module? >> >> 1- telnet to machine A >> 2- from A to mcahine B >> 3- from M to another machine C >> >> ----- >> use Expect; >> >> my $exp = Expect->spawn("telnet $somehost") >> or die "Cannot spawn telnet: $!\n"; >> >> ... >> >> ----- >> Your help is greatly appreciated. Please reply directly to me since >> I am not on the alias. >> >> Hailey. >> >> > |
From: Hailey N. <Hai...@Su...> - 2003-10-02 20:13:10
|
Hi Chris, >>> First you will have to login to A. That involves what you have, plus >>> you will have to $exp->expect() for whatever telnet responds with when >>> it asks for the password. Then use a $exp->send() to send the password. >>> Then $exp->expect() for the prompt on A. Once you are logged into A, >>> use a $exp->send() to call telnet $B on A to login to B, $exp->expect() >>> for the password prompt, and then $exp->send() it. Once logged into B, >>> do the same to login to C, and the same for D, and the same for ... Still struggling with this. As soon as I logged into B from A I lose control of that session/connection. So $exp->send() won't do anything. Did you write something like this before. Can I have an example? Note: A know B but doesn't know C - in another words, to get to C you have to go through B. Thanks a million, Hailey >>>> >>>> Please show me how to do nested telnet sessions using Expect.pm module? >>>> >>>> 1- telnet to machine A >>>> 2- from A to mcahine B >>>> 3- from B to machine C >>>> >>>> ----- >>>> use Expect; >>>> >>>> my $exp = Expect->spawn("telnet $somehost") >>>> or die "Cannot spawn telnet: $!\n"; >>>> >>>> ... >>>> >>>> ----- >>>> Your help is greatly appreciated. Please reply directly to me since >>>> I am not on the alias. >>>> |
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 ##### |
From: Hailey N. <Hai...@Su...> - 2003-10-03 01:23:09
|
I can't thank you enough! Made my day - wait my week actually! Thank You so much Chris, Hailey >> 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. > |