#!/usr/bin/perl # # File : v3.2_enter_telnet_ssh_opt.pl # Author : Premson Rodriguez # Desc : Spawns multiple connections to Opt Network # Date : 18-Oct-2003 # # Added: # 27-Oct-2003 : Added log_stdout to extract info on outputs 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($host3) = ""; my($user1) = ""; my($pwd1) = ""; my($user2) = ""; my($pwd2) = ""; my($user3) = ""; my($pwd3) = ""; my($extra) = "/usr/local/bin/ssh -V"; my($i, $flag); my($alternateTelnet) = ""; 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(20, '-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(20, '-re', qr'[$#>:] $') || die "ERR expecting username\n"; ($exp->expect(20, '-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(20, '-re', qr/^login\:/i)) ? (die "ERR in authenticating user ($user1)\n") : ($exp->expect(20, '-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(20, '-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(20, '-re', qr/((password\:)|(password\:\ ))/i)) { die "ERR: Authentication Failed for ($user2)\n"; } else { $exp->expect(20, '-re', qr'[$#>:] $') || die "Unable to get prompt\n"; print "Authentication of user ($user2) Successful\n"; } print "Executing third command ($comm2$space$user3$space$host3) from host ($host2)\n"; print $exp "$comm2$space$user3$space$host3\r"; if ($exp->expect(20, '-re', qr/(yes\/no)/i)) { print "SSH Public Key ... accepting connection for ($user3)\n"; print $exp "yes\r"; if ($exp->expect(20, '-re', qr/((password\:)|(password\:\ ))/i)) { print "Prompted (first time) for Password by ($user3)\n"; print "Sending Password for user ($user3)\n"; print $exp "$pwd3\r"; if ($exp->expect(20, '-re', qr/old/i)) { die "SSH ERR: Prompted for Password Change .... please change and come back.\n"; } $exp->log_stdout(1); ########### Added on 27th Oct 2003 print "Authentication of user ($user3) Successful\n"; print "Executing command ($extra)\n"; $exp->expect(20, '-re', qr'[$#>:] $') || die "ERR expecting shell(1)\n"; print $exp "$extra\r"; $exp->expect(20, '-re', qr'[$#>:] $') || die "ERR expecting third command(1).\n"; $exp->log_stdout(0); ########### Added on 27th Oct 2003 $alternateTelnet = "1"; } else { print "SSH ERR: Error Connecting to host ($host3) using user ($user3)\n"; $alternateTelnet = "2"; }} if ($alternateTelnet == 1) { print "Successful completion on ($host3) using user ($user3)\n"; } else { if ($exp->expect(20, '-re', qr/((password\:)|(password\:\ ))/i)) { print "Prompted for Password by ($user3)\n"; print "Sending Password for user ($user3)\n"; print $exp "$pwd3\r"; if ($exp->expect(20, '-re', qr/old/i)) { die "SSH ERR: Prompted for Password Change .... please change and come back\n"; } if ($exp->expect(20, '-re', qr/disabled/i)) { die "SSH ERR: Account is disabled .... please change and come back\n"; } $exp->expect(20, '-re', qr'[$#>:] $') || die "ERR expecting shell\n"; $exp->log_stdout(1); ########### Added on 27th Oct 2003 print "Authentication of user ($user3) Successful\n"; print "Executing command ($extra)\n"; print $exp "$extra\r"; $exp->expect(20, '-re', qr'[$#>:] $') || die "ERR expecting third command(2).\n"; $exp->log_stdout(0); ########### Added on 27th Oct 2003 } else { print "SSH ERR: Error Connecting host ($host3) using user ($user3)\n"; $alternateTelnet = "2"; } } if ($alternateTelnet == 2) { $exp->expect(20, '-re', qr'[$#>:] $') || die "ERR Unable to get prompt in host ($host3)\n"; print "Trying Telnet ($comm1$space$host3)\n"; print $exp "$comm1$space$host3\r"; if ($exp->expect(20, '-re', qr/unknown/i)) { die "TELNET ERR: Unknown Host\n"; } ($exp->expect(20, '-re', qr/((login\:)|(login\:\ ))/i)) ? (print "Entering Login Info\n") : (die "ERR: Telnet Failed on ($host3)\n"); print "Authenticating username ($user3) through telnet\n"; print $exp "$user3\r"; ($exp->expect(20, '-re', qr/((Password\:)|(Password\:\ ))/i)) ? (print "Entering User Info\n") : (die "ERR: Error expecting ($user3)\n"); print "Authenticating password\n"; print $exp "$pwd3\r"; ($exp->expect(20, '-re', qr/^login\:/i)) ? (die "ERR in authenticating user ($user3) ... incorrect password\n") : ($exp->expect(20, '-re', qr'[$#>:] $')); ($exp->expect(20, '-re', qr/disabled/i)) ? (die "ERR in authenticating user ($user3) ... account is disabled\n") : ($exp->expect(20, '-re', qr'[$#>:] $')); $exp->log_stdout(1); ########### Added on 27th Oct 2003 print "Authentication of user ($user3) Successful\n"; print "Executing command ($extra)\n"; print $exp "$extra\r"; $exp->expect(20, '-re', qr'[$#>:] $') || die "ERR expecting third command(3).\n"; $exp->log_stdout(0); ########### Added on 27th Oct 2003 } 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; /r/ and $host3 = $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; /l/ and $user3 = $ARGV[$i+1], $last_processed += 2, last SWITCH; /m/ and $pwd3 = $ARGV[$i+1], $last_processed += 2, last SWITCH; } } } } sub usage { print <