Difference between revisions of "Useful Scripts"
Line 27: | Line 27: | ||
Ping a host, if ping is ok do nothing, if ping fails do something. In this case we bring up all the tun interfaces using netstart and rc.local and reload. | Ping a host, if ping is ok do nothing, if ping fails do something. In this case we bring up all the tun interfaces using netstart and rc.local and reload. | ||
+ | |||
+ | Make sense of FreeBSD and OpenBSD dhcpd leases; | ||
+ | |||
+ | use Time::Local; | ||
+ | |||
+ | open(LEASE, "/var/db/dhcpd/dhcpd.leases"); | ||
+ | foreach $line (<LEASE>) { | ||
+ | chomp($line); | ||
+ | $data = 1 if $line =~ /^lease /; | ||
+ | $data = 0 if $line =~ /^}/; | ||
+ | |||
+ | if ($data) { | ||
+ | if ($line =~ /^lease/) { | ||
+ | $ip = (split(" ", $line))[1]; | ||
+ | } elsif ($line =~ /^ starts/) { | ||
+ | ($date, $time) = (split(" ", $line))[2,3]; | ||
+ | ($y, $m, $d) = split("/", $date); | ||
+ | ($H, $M, $S) = split(":", $time); | ||
+ | $start = timelocal($S,$M,$H,$d,$m-1,$y); | ||
+ | |||
+ | } elsif ($line =~ /^ ends/) { | ||
+ | ($date, $time) = (split(" ", $line))[2,3]; | ||
+ | ($y, $m, $d) = split("/", $date); | ||
+ | ($H, $M, $S) = split(":", $time); | ||
+ | $stop = timelocal($S,$M,$H,$d,$m-1,$y); | ||
+ | } elsif ($line =~ /^ hardware ethernet/) { | ||
+ | $mac = (split(" ", $line))[2]; | ||
+ | $mac =~ s/;//; | ||
+ | } elsif ($line =~ /^ client-hostname/) { | ||
+ | $client = (split(/\"/, $line))[1]; | ||
+ | } | ||
+ | } else { | ||
+ | print localtime($start) . "\t" . localtime($stop) . "\t$ip\t$mac\t$client\n" if $stop >= $now; | ||
+ | $ip = ""; $start = ""; $stop = ""; $mac = ""; $client = ""; | ||
+ | } | ||
+ | } | ||
+ | close(LEASE); | ||
+ | |||
+ | This will parse and organize the leases for better visibility. |
Revision as of 10:59, 15 January 2019
Script to check a file with hosts in that file. We will be using the ping command, if we can't ping the host we can do some magic;
Lets say the file guardfile.txt contains 1.1.1.1
#!/bin/sh cat /usr/home/anexit/guard/guardfile.txt | while read line do if [ ! -z $line ]; then PINGCOUNT=2 PING=$(ping -c $PINGCOUNT $line | grep received | cut -d ',' -f2 | cut -d ' ' -f2) if [ $PING -eq 0 ]; then echo "Attempting to establish connection..: $line" sh /etc/netstart sh /etc/rc.local else echo "Connection is live.: $line" fi fi done
This script was used as a con job when using the an ssh tunnel. SSH supports tunnels but when they crash there is no mechanism at the time of this writing to keep or bring interfaces back online. Man SSH and look for;
SSH-BASED VIRTUAL PRIVATE NETWORKS
Theory;
Ping a host, if ping is ok do nothing, if ping fails do something. In this case we bring up all the tun interfaces using netstart and rc.local and reload.
Make sense of FreeBSD and OpenBSD dhcpd leases;
use Time::Local;
open(LEASE, "/var/db/dhcpd/dhcpd.leases"); foreach $line (<LEASE>) {
chomp($line); $data = 1 if $line =~ /^lease /; $data = 0 if $line =~ /^}/;
if ($data) { if ($line =~ /^lease/) { $ip = (split(" ", $line))[1]; } elsif ($line =~ /^ starts/) { ($date, $time) = (split(" ", $line))[2,3]; ($y, $m, $d) = split("/", $date); ($H, $M, $S) = split(":", $time); $start = timelocal($S,$M,$H,$d,$m-1,$y);
} elsif ($line =~ /^ ends/) { ($date, $time) = (split(" ", $line))[2,3]; ($y, $m, $d) = split("/", $date); ($H, $M, $S) = split(":", $time); $stop = timelocal($S,$M,$H,$d,$m-1,$y); } elsif ($line =~ /^ hardware ethernet/) { $mac = (split(" ", $line))[2]; $mac =~ s/;//; } elsif ($line =~ /^ client-hostname/) { $client = (split(/\"/, $line))[1]; } } else { print localtime($start) . "\t" . localtime($stop) . "\t$ip\t$mac\t$client\n" if $stop >= $now; $ip = ""; $start = ""; $stop = ""; $mac = ""; $client = ""; }
} close(LEASE);
This will parse and organize the leases for better visibility.