2021-10-30

bash/sh incrementing for loop

  i=0; while [ $i -le 255 ]
 do
 echo evvdyn$i:nodhcp:10.11.16.$i::
 i=`expr $i + 1`
 done

2021-10-11

Send a process to the background - "fork" - the command missing from Linux proper

These will send the process to the background and send a "wall" broadcast when complete.

$ cat fork
#!/usr/bin/bash
echo $*
(/usr/bin/nohup /usr/local/etc/fork2 $* > /tmp/nohup$$.txt &)&


$ cat fork2
#!/usr/bin/bash
/usr/bin/wall $* completed

Ping until failure - AND - Wait for a ping

These two Perl scripts fork to the background and call each other in turn to help track important systems.


 -------------------------- ping2failure --------------------
#!/usr/bin/perl
#
$MAIL_CMD = "/bin/mail";
$PING = "/bin/ping";
$dofork = 1;

$target = $ARGV[0];

die "No target.\n" if ($target eq "");

if ($dofork) {
if (open(PS, "/bin/ps ax | /bin/grep \"ping2failure $target\" | /bin/grep -v grep |")) {
    @psax = <PS>;
    close(PS);
    #print "@psax";
    for(@psax) {
        s/^\s+//g;
        #print $_;
        if (/ping2failure $target/) {
            #print "$_";
            ($pid, @stuff) = split(/\s+/);
            #print "$$ pid is $pid and $stuff[0]\n";
            if ($pid != $$) {
                die "$target already being waited on\n";
                 # no reason to run if already doing this
            }
        }
    }
}


# deal with stdin, stdout, stderr, control terminal, and put self
# in background
$TIOCNOTTY = 0x20007471;
close(stdin);
open(stdout, ">/dev/null");
open(stderr, ">/dev/null");
if (open(tty, "/dev/tty")) {
    ioctl(tty, $TIOCNOTTY, 0);
    close(tty);
}
if (fork) {
    exit 0;
}
}


$pings = 0;
$go = 1;
while ($go) {
        $xrand = system("/usr/bin/shuf -i 7-25 -n1");
        chomp($xrand);
        sleep($xrand);
        open(DOPING,"$PING -c 5 -W 45 $target |");
        @hello = <DOPING>;
        close(DOPING);
        for (@hello) {
                chomp;
                $x = $_;
                if ($x =~ /0 received/) {
                        open(DOPING,"$PING -c 5 -W 45 $target |");
                        @hello2 = <DOPING>;
                        close(DOPING);
                        for (@hello2) {
                                chomp;
                                $x = $_;
                                if ($x =~ /0 received/) {
                                        $go = 0;
                                        }
                                }
                        }
                }
        $pings++;
}



#system("/usr/bin/logger -p local1.emerg ping2failure $target is now off-line");
system("/usr/bin/logger -p local1.emerg ping2failure $target is now off-line ");
system("/usr/bin/wall ping2failure $target is now off-line ");




------------------ wait4ping ---------------------------


#!/usr/bin/perl
#
$MAIL_CMD = "/usr/bin/mail";
$PINGCMD = "/usr/bin/ping";

$target = $ARGV[0];

die "No target.\n" if ($target eq "");

if (open(PS, "/bin/ps ax | /bin/grep \"wait4ping $target\" | /bin/grep -v grep |")) {
    @psax = <PS>;
    close(PS);
    #print "@psax";
    for(@psax) {
        s/^\s+//g;
        #print $_;
        if (/wait4ping $target/) {
            #print "$_";
            ($pid, @stuff) = split(/\s+/);
            #print "$$ pid is $pid and $stuff[0]\n";
            if ($pid != $$) {
                die "$target already being waited on\n";
                 # no reason to run if already doing this
            }
        }
    }
}


# deal with stdin, stdout, stderr, control terminal, and put self
# in background
$TIOCNOTTY = 0x20007471;
close(stdin);
open(stdout, ">/dev/null");
open(stderr, ">/dev/null");
if (open(tty, "/dev/tty")) {
    ioctl(tty, $TIOCNOTTY, 0);
    close(tty);
}
if (fork) {
    exit 0;
}


$pings = 0;
while (1) {
        open(DOPING,"$PINGCMD -c 1 -W 45 $target |");
        @hello = <DOPING>;
        close(DOPING);
        last if ($hello[4] =~ /\,\s+1\s+received\,/);
        $pings++;
        $xrand = system("/usr/bin/shuf -i 17-37 -n1");
        chomp($xrand);
        sleep($xrand);
}

#system("/usr/bin/logger -p local1.emerg wait4ping $target is now on-line");
system("/usr/bin/logger -p local1.emerg wait4ping $target is now on-line ");
system("/usr/bin/wall wait4ping $target is now on-line ");


Fedora (et al) linux - enable/disable or start/stop the graphical user interface

Enable
 /usr/bin/systemctl set-default graphical.target

Disable
/usr/bin/systemctl set-default multi-user.target

Start
/usr/bin/systemctl start graphical.target

Stop
/usr/bin/systemctl stop graphical.target

Enable USB-to-serial-to-USB direct able connection to you Fedora and many other Linux flavors

/usr /bin/systemctl restart getty\@ttyUSB0

The backslash makes sure the command line uses the @ sign literally.

Enable telnet on Fedora

dnf -y install telnet-server telnet
systemctl enable telnet.socket
systemctl start telnet.socket



- yes I don't care if it's not best practices. The occasional use of telnet is a nice fallback when things get sideways.


Use all the space of a Micro-SD card of a Raspberry Pi running Fedora 34 (or possibly higher)

 To expand the root partition

growpart /dev/mmcblk0 3
pvresize /dev/mmcblk0p3
lvextend -r -l +100%FREE  /dev/mapper/fedora_fedora-root
xfs_growfs -d /


Matrox Monarch 5-minute video recordings assembled for Youtube.

This is a short bash scirpt I used to compose 5-minute mov files into one mpg file to upload to Youtube. We have a Matrox Monarch that rec...