Subject: Re: VPN & SSH-IP-TUNNEL - A test script in Perl.
To: None <netbsd-help@netbsd.org>
From: Gan Uesli Starling <oinkfreebiker@att.net>
List: netbsd-help
Date: 02/24/2002 13:06:20
Alas and alack,

One last time...I hope. Sans introduction, here again is the Perl script for
testing vpn from packag ssh-ip-tunnel on NetBSD 1.5.2. I have shortened
all lines (including line 5 "...columns > 80." to 78 columns. I can see it now
(all of it) on my screen. If it truncates again, something else is doing it.

Also, I added a patch to avoid redundantly showing any entries from 
"ifconfig" so that they may not now show up twice. The script is immediately
below. A sample out put (run just now) follows below that. Look for any future
improvements to this script at my websiste as/per the URL in my signature. I 
will post it there today, not again on this list.

May this be the last time. Here, goes... -- G.U.S.

<IMPROVED PERL SCRIPT>
#!/usr/pkg/bin/perl
# Report status of vpn from ssh-ip-tunnel on NetBSD 1.5.2 
# Put in same path as "vpn" from pkg ssh-ip-tunnel, chmod = 755.
# Append lines "sleep 2s;" and "./vpnck | more" to end of "vpn".
# In vi, set tabstop to 2. Go "<Esc>:set tabstop=2<return>" else columns > 80
# Copyright (c) 2002 Gan Uesli Starling

print "\n%%%%%%% Checking VPN stats for SSH and PPPD %%%%%%%\n";    
 
# INIT SOME STUFF

$vpn_str = "";
@vpn_ary = ();
@net_ary = ();

# TELL ABOUT NETSTAT STUFF

$vpn_str = `netstat -r`; # Get in full.
@vpn_ary = split("\n", $vpn_str); # Split on rows.
$dup_str = ""; # For dup comparion.

print "\nFiltered \"netstat -r\" says: \n"; 

LOOP_1:foreach $line (@vpn_ary) { # For every row...
  if ($line !~ /localhost/) { # ignore unwanted...
    @line = split(" ",($line)); # columnize...
    if ($#line >= 4) { # skip major section headings...
      if ($dup_str !~ /$line[6]/) { # avoid duplication...
        push(@net_ary, $line[6]); # keep iface id columns...
        $dup_str = "$dup_str $line[6]"; # add to no-dup str...
      }

# BEGIN FORMAT -- DO NOT INDENT
format STDOUT =
@<<<<<<<<<<<<<<<<<<@<<<<<<<<<<<<<<<<<<@<<<<<<<<<<<<<<<<<<@<<<<<<<<<<<<<<<<<<
$line[0],$line[1],$line[5],$line[6] 
.
write; # show important stuff...
# FORMAT DONE -- RESUME INDENT

    }
    if ($#line == 0) {
      # Comment out line below to include routing tables below "Internet".
      last LOOP_1 if ($line =~ "XNS"); # break out after "Internet" section.
      print("\n$line[0]\n");
    }
  }
} 

# TELL ABOUT IFCONFIG  STUFF

print "\nFiltered \"ifconfig -a\" says: \n\n";

# Show ifconfig for results of 'netstat -r' filtering.
$dup_str = ""; #
foreach $line (@net_ary) {
  if (($line !~ /Interface/)) { # don't ifconfig heading name...
      $line = `ifconfig $line`; # get for kept interfaces...
    print "$line"; # show it.
    @dup_ary = split(" ", $line); # columnize...
    $dup_str = "$dup_str $dup_ary[0]";  # remember 1st columns...
  }
}

# print "\n\n%%%%%%%%%%%%%% DUP-AVOIDANCE TEST SEPARATOR %%%%%%%%%%%%%%\n\n";

# TELL ABOUT OTHER PPP IFCONFIG STUFF

$ppp_str = `ifconfig -a | grep "ppp"`; # Get ifconfig for all ppp's...
$ppp_str =~ s/flags=.+\n//g; # Lose line after "flags=".
$ppp_str =~ s/\s{2,}.+\n//g; # Lose all indented lines.
$ppp_str =~ s/:/ /g; # Lose the colons.
       
@ppp_ary = split(" ", $ppp_str); # Split into separate ppp's.

foreach $ppp_str (@ppp_ary) { # For each ppp...
  if ($dup_str !~ /$ppp_str/) { # if not dup of above...
    print `ifconfig $ppp_str`; # show the ifconfig.
  }
}

$vpn_str = `/bin/ps -xa | grep ssh`; # Grep ps to match "ssh".
@vpn_ary = split("\n", $vpn_str); # Split on rows.

# TELL ABOUT SSH SERVERS

print "\nSSH server pids: ";
$pids_sshd = ();
foreach $line (@vpn_ary) { # For grep'd ssh-matches...
  @line = split(" ",($line)); # columnize...
  if ($line[4] =~ /sshd/) { # match only servers...
    if ($line[5] =~ /^\s*$/) {$line[5] = "";} # lose blank...
    push(@pids_sshd, ("$line[0] $line[5]")); # keep any matching...
  }
}
print join(", ", @pids_sshd); # show matches.

# TELL ABOUT SSH CLIENTS

print "\nSSH client pids: ";
@pids_ssh = ();
foreach $line (@vpn_ary) { # For grep'd ssh-matches... 
  if ($line =~ /Batchmode/) {$blurb = "Batchmode";} else {$blurb = "";}
  @line = split(" ",($line)); # columnize...
  if (($line[4] =~ /ssh/) && ($line[4] !~ /sshd/)) { # match clients only...
    push(@pids_ssh, ("$line[0] $blurb")); # keep any matching...
  }
}
print join(", ", @pids_ssh); # show matches.  

print "\n\n%%%%%%% Check is complete %%%%%%%\n\n";

# EOF
</IMPROVED PERL SCRIPT>

<TEST-OUTPUT>
gus# vpnck

%%%%%%% Checking VPN stats for SSH and PPPD %%%%%%%

Filtered "netstat -r" says:

Internet:
Destination         Gateway             Mtu                 Interface
default             199.69.200.58       1500                ppp0
192.168.1           link#1              1500                fxp0
thinkpad            00:04:5a:91:ec:05   1500                fxp0
192.168.100.2       192.168.100.1       1500                ppp1
199.69.200.58       45.detroit15rh15rt  1500                ppp0
 
Filtered "ifconfig -a" says:
 
ppp0: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 1500
        inet 12.87.100.45 -> 199.69.200.58 netmask 0xff000000
        inet6 fe80::203:47ff:fea0:4927%ppp0 -> :: prefixlen 64 scopeid 0x3
fxp0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
        address: 00:03:47:a0:49:27
        media: Ethernet autoselect (100baseTX full-duplex)
        status: active
        inet 192.168.1.11 netmask 0xffffff00 broadcast 192.168.1.255
        inet6 fe80::203:47ff:fea0:4927%fxp0 prefixlen 64 scopeid 0x1
ppp1: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 1500
        inet 192.168.100.1 -> 192.168.100.2 netmask 0xfffffffc
        inet6 fe80::203:47ff:fea0:4927%ppp1 -> :: prefixlen 64 scopeid 0x4
 
SSH server pids:
SSH client pids: 3443 Batchmode
 
%%%%%%% Check is complete %%%%%%%
 
gus#

</TEST-OUTPUT>

-- 

Mysterious Starling -- Rarest Extinct Bird
     _
   <(+)__        Gan Uesli Starling
     ((__/)=-    Kalamazoo, MI, USA
      `||`
       ++        http://starling.ws

Newbie-2-Newbie NetBSD Unix How-To Pages at...
http://om-ah-hum.com/share/gus_netbsd_index.html