#!/usr/bin/perl # # File : v3.3_enter_telnet_ssh_opt.pl # Desc : Spawns multiple Telnet/SSH connections # Date : 18-Oct-2003 # Author : Premson Rodriguez # # Added: # 27-Oct-2003 # 28-Oct-2003 # - Added log_stdout to extract info on outputs # - Added log_stdout to telnet module to see the error prompted during authentication # - Modified command to include ssh and the path of ssh # - Increased speed of the program by reducing timeouts wherever necessary # 30-Oct-2003 # - Removed Ternary Operators since die was giving problems with outputing messages to logs # ############################################################################################ 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 ; ssh -V ; /usr/local/sbin/sshd -v ; ps -ef -o comm | grep sshd | sed -n 1p"; 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); ## Use this only when you need extreme verbose $exp->log_stdout(0); ($exp->expect(10, '-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(10, '-re', qr'[$#>:] $') || die "ERR expecting username\n"; ($exp->expect(10, '-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(10, '-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(10, '-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(10, '-re', qr/((password\:)|(password\:\ ))/i)) { die "ERR: Authentication Failed for ($user2)\n"; } else { $exp->expect(10, '-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(10, '-re', qr/(yes\/no)/i)) { print "SSH Public Key ... accepting connection for ($user3)\n"; print $exp "yes\r"; if ($exp->expect(10, '-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(10, '-re', qr/old/i)) { print "SSH ERR: Prompted for Password Change .... please change and come back.\n"; die "EXITING PROGRAM : SSH ERR: Prompted for Password Change .... please change and come back.\n"; } elsif ($exp->expect(10, '-re', qr/password/i)) { print "SSH ERR in authenticating user ($user3) ... incorrect user or password.\n"; die "EXITING PROGRAM : SSH ERR in authenticating user ($user3) ... incorrect user or password.\n"; } else { $exp->expect(10, '-re', qr'[$#>:] $'); } $exp->log_stdout(1); ########### Added on 27th Oct 2003 print "Authentication of user ($user3) Successful\n"; print "Executing command ($extra)\n"; $exp->expect(10, '-re', qr'[$#>:] $') || die "ERR expecting shell(1)\n"; print $exp "$extra\r"; $exp->expect(10, '-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(10, '-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(10, '-re', qr/old/i)) { print "SSH ERR: Prompted for Password Change .... please change and come back\n"; die "EXITING PROGRAM : SSH ERR: Prompted for Password Change .... please change and come back\n"; } elsif ($exp->expect(10, '-re', qr/disabled/i)) { print "SSH ERR: Account is disabled .... please change and come back\n"; die "EXITING PROGRAM : SSH ERR: Account is disabled .... please change and come back\n"; } elsif ($exp->expect(10, '-re', qr/password/i)) { print "SSH ERR: Incorrect password .... please change and come back\n"; die "EXITING PROGRAM : SSH ERR: Incorrect password .... please change and come back\n"; } else { $exp->expect(10, '-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(10, '-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(10, '-re', qr'[$#>:] $') || print "SSH ERR: No response through SSH for ($host3)\n"; print "Trying Telnet ($comm1$space$host3)\n"; print $exp "$comm1$space$host3\r"; if ($exp->expect(10, '-re', qr/unknown/i)) { print "TELNET ERR: Unknown Host\n"; die "EXITING PROGRAM : TELNET ERR: Unknown Host\n"; } if ($exp->expect(10, '-re', qr/((login\:)|(login\:\ ))/i)) { print "Entering Login Info\n"; } else { print "EXITING PROGRAM : ERR: Telnet Failed on ($host3)\n"; die "ERR: Telnet Failed on ($host3)\n"; } print "Authenticating username ($user3) through telnet\n"; print $exp "$user3\r"; if ($exp->expect(10, '-re', qr/((Password\:)|(Password\:\ ))/i)) { print "Entering User Info\n"; } else { print "ERR: Error expecting password for ($user3)\n"; die "EXITING PROGRAM ERR: Error expecting password for ($user3)\n"; } print "Authenticating password\n"; print $exp "$pwd3\r"; if ($exp->expect(15, '-re', qr/((^login\:)|(^login\:\ )|(password))/i)) { print "TELNET ERR in authenticating user ($user3) ... incorrect user or password\n"; die "EXITING PROGRAM : TELNET ERR .... incorrect user or password\n"; } elsif ($exp->expect(15, '-re', qr/disabled/i)) { print "TELNET ERR in authenticating user ($user3) ... account is disabled\n"; die "EXITING PROGRAM : TELNET ERR ... account is disabled\n"; } else { $exp->expect(10, '-re', qr'[$#>:] $'); } $exp->log_stdout(1); ########### Added on 28th Oct 2003 print "Authentication of user ($user3) Successful\n"; print "Executing command ($extra)\n"; print $exp "$extra\r"; $exp->expect(10, '-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 <