#!/perl -w use strict; use Net::Telnet; # Cavaets: # # 1. This simple example assumes that the login, password, and command # prompts are all able to be matched by the Net::Telnet defaults - if # not, you'll need to add the appropriate parameters to the login() # command when logging in to host2. # # 2. I'm not sure $! is going to be meaningful in my die() statements, # but what the heck - insert your own debugging if it's not working. # :-) # First host my $host1 = 'hostname1'; my $user1 = 'user1'; my $pass1 = 'password1'; # Second host my $host2 = 'hostname2'; my $user2 = 'user2'; my $pass2 = 'password2'; my $session = new Net::Telnet (host => $host1) or die "Couldn't create: $!"; $session->login($user1, $pass1) or die "Couldn't login to $host1: $!"; $session->print("telnet $host2") or die "Couldn't telnet to $host2: $!"; $session->login($user2, $pass2) or die "Couldn't login to $host2: $!"; # Obviously, put your own commands in here - $session is now talking # to host2 via host1. my @output = $session->cmd('uptime') or die "Couldn't run command: $!"; print "@output"; exit;