#!/usr/bin/perl # # File : v1.4_enter_telnet_ssh.pl # Author : Premson Rodriguez # Desc : Spawns multiple connections using telnet & ssh # Date : 18-Oct-2003 # # use strict; use Expect; &usage unless @ARGV; $|=1; $Expect::Log_Stdout=1; my($stdin) = Expect->exp_init(\*STDIN); my($stdout) = Expect->exp_init(\*STDOUT); my($host1) = ""; my($host2) = ""; my($user1) = ""; my($pwd1) = ""; my($user2) = ""; my($pwd2) = ""; my($prompt) = "\$ "; my($extra) = "who > wholist"; my($i, $flag); my($last_processed) = -1; my($comm1) = "telnet"; my($comm2) = "ssh -l"; my($space) = " "; &parseCmdLine; &connect; sub connect { (my $exp=Expect->spawn("$comm1$space$host1")) ? (print "Entering ($host1)\n") : (die "ERR: Spawn failed: $?\n"); #$exp->exp_internal(1); $exp->log_stdout(0); ($exp->expect(15, '-re', qr/((login\:)|(login\:\ ))/i)) ? (print "Entering Login Info\n") : (die "ERR: Telnet Failed on ($host1)\n"); print "Authenticating username ($user1)\n"; print $exp "$user1\r"; #$exp->expect(15, '-re', qr'[$#>:] $') || die "ERR expecting username\n"; ($exp->expect(15, '-re', qr/((Password\:)|(Password\:\ ))/i)) ? (print "Entering User Info\n") : (die "ERR: Error expecting ($user1)\n"); print "Authenticating password\n"; print $exp "$pwd1\r"; ($exp->expect(10, '-re', qr/^login\:/i)) ? (die "ERR in authenticating user ($user1)\n") : ($exp->expect(15, '-re', qr'[$#>:] $')); print "Authentication of user ($user1) Successful\n"; print "Executing command ($comm2$space$user2$space$host2)\n"; print $exp "$comm2$space$user2$space$host2\r"; ($exp->expect(15, '-re', qr/((password\:)|(password\:\ ))/i)) ? (print "Prompted for Password by ($user2)\n") : (die "ERR: Error expecting ($user2)\n"); print "Sending Password for user ($user2)\n"; print $exp "$pwd2\r"; if ($exp->expect(15, '-re', qr/((password\:)|(password\:\ ))/i)) { die "ERR: Authentication Failed for ($user2)\n"; } else { $exp->expect(15, '-re', qr'[$#>:] $') || die "Unable to get prompt\n"; print "Authentication of user ($user2) Successful\n"; } print "Executing second command ($extra)\n"; print $exp "$extra\r"; $exp->expect(15, '-re', qr'[$#>:] $') || die "ERR expecting second command.\n"; return 0; } sub parseCmdLine { foreach $i (0...$#ARGV) { if ( $ARGV[$i] =~ /(^\-(\w)$)/ ) { $flag = $1; $_ = $flag; SWITCH: { /i/ and &usage, last SWITCH; /s/ and $host1 = $ARGV[$i+1], $last_processed += 2, last SWITCH; /d/ and $host2 = $ARGV[$i+1], $last_processed += 2, last SWITCH; /u/ and $user1 = $ARGV[$i+1], $last_processed += 2, last SWITCH; /p/ and $pwd1 = $ARGV[$i+1], $last_processed += 2, last SWITCH; /n/ and $user2 = $ARGV[$i+1], $last_processed += 2, last SWITCH; /o/ and $pwd2 = $ARGV[$i+1], $last_processed += 2, last SWITCH; } } } } sub usage { print <