#!/usr/bin/perl # Definitions my $WEB_LIST = "http://www.llamacom.com/~eanderso/c_hosts.enc"; my $LOCAL_DIR = "$ENV{'HOME'}/.connect"; my $LOCAL_FILENAME = "c_hosts.map"; my $WEB_LIST_LN = "c_hosts.enc"; # Find OpenSSL # Default to it being in path, but override if in known wierd location my $OpenSSL = 'openssl'; for ('/usr/local/ssl/bin/openssl','/usr/local/ssl/bin/ssleay') { if ( -x ) { $OpenSSL = $_;} } # Figure out where we're connecting to my $destination = shift; #Check for (and create) local dir if (! -d $LOCAL_DIR) { mkdir $LOCAL_DIR, 0700 || die "Cannot make $LOCAL_DIR\n "; } chdir $LOCAL_DIR || die "Cannot change dir to $LOCAL_DIR \n"; # delete any existing web host file unlink "$LOCAL_DIR/$WEB_LIST_LN"; unlink <$LOCAL_DIR/web_map*>; #get new file print "Fetching network host file... "; my @args = ("wget", "-q", "-O", "$LOCAL_DIR/$WEB_LIST_LN",$WEB_LIST); system(@args); print " done\n"; #decrypt it my $netfile = `mktemp $LOCAL_DIR/web_map.XXXXXX`; chop $netfile; @args = ($OpenSSL,"enc","-des3","-d","-in","$LOCAL_DIR/$WEB_LIST_LN","-out","$netfile"); print "Enter password to decrypt host map. \n"; print "Decrypting ... "; $retval = 0xffff & system(@args); if ($retval != 0) { print "Decryption error, return value $retval. Wrong password?\n"; exit 1; } print "done \n"; #print "$netfile \n"; #Get the data we want! %our_hash = (); open(NET_FILE,$netfile) || die "Could not open temp file $netfile"; while( ){ ($hostname, $username, $proto, @aliases) = split; # insert the host by its cannonical name $our_hash{$hostname} = { 'protocol' => $proto, 'host' => $hostname, 'user' =>$username }; # and aliases for $alias (@aliases) { $our_hash{$alias} = { 'protocol' => $proto, 'host' => $hostname, 'user' =>$username }; } } ## All that was but a preamble. ## Where do we connect to? ## #my %host_info = $our_hash{$destination}; #print "$host_info{protocol} \n"; if (! defined $our_hash{$destination}{protocol} ) { #presume failed lookup print "No host or alias matching $destination found. Sorry. \n"; exit 1; } # Clean up before connecting unlink $netfile; unlink unlink "$LOCAL_DIR/$WEB_LIST_LN"; do_connect($our_hash{$destination}{protocol}, $our_hash{$destination}{host}, $our_hash{$destination}{user}); #for $tmp ( keys %our_hash){ # print "$tmp, Protocol:$our_hash{$tmp}{protocol} Host:$our_hash{$tmp}{host} User:$our_hash{$tmp}{user} \n"; #} sub do_connect { my $protocol = shift; my $host = shift; my $user = shift; # print "Calling connect for $protocol, $host, $user \n"; #Programming PERL 2nd ed. says this is a common way to do a switch, so maybe this code #will look reasonable to you. It looks funky as hell to me. It's on p. 105 :-) for ($protocol) { /ssh/ and do { ssh_connect($user, $host); last; }; /ssh2/ and do { ssh_connect($user, $host); last; }; /telnet/ and do { telnet_connect($user, $host); last; }; die "Unknown protocol $_ . Can't continue. \n"; } } sub telnet_connect { $user = shift; $host = shift; exec(('telnet','-l',"$user","$host")); } sub ssh_connect { $user = shift; $host = shift; exec(('ssh',"$user\@$host")); }