Tag Archives: bash

Motion detection for stupid webcams

Last year i made a script for a friend who wanted to detect visually if his garden sprinkler was on or off. A few days ago i saw someone who wanted to see if things where moving in his house. (didn’t trust his landlord i think)
But he only had a dumb/simple/cheap camera .. so it had no motion detection.

I was thinking of my script, and could easily adapt it for this usage.

Most ipcams have somekind of URL/API you can use to capture a image.
Some examples below

# Reolink
wget "http://ipnumber/cgi-bin/api.cgi?cmd=Snap&channel=0&rs=randomstring&user=user&password=password" -O image.jpg

# Foscam
wget "http://ipnumber:88/CGIProxy.fcgi?cmd=snapPicture2&usr=user&pwd=password&cnt=randomstring" -O image.jpg

# Edimax
wget "http://user:password@ipnumber/jpg/image.jpg" -O image.jpg

# Hikvision
wget "http://user:password@ipnumber/ISAPI/Streaming/channels/101/picture" -O image.jpg

So using below script i can capture a image, compare it to the previous, and when it’s above a certain threshold sends a email.

#!/bin/bash
# Only uses wget and image-magick
treshhold=500
fuzzyness=20%
# CHANGE WEBCAM THINGY TO OWN URL AND CREDENTIALS
wget -q "http://webcamip/cgi-bin/api.cgi?cmd=Snap&channel=0&user=user&password=password" -O previous.jpg
while true;  do
wget -q "http://webcamip/cgi-bin/api.cgi?cmd=Snap&channel=0&user=user&password=password" -O current.jpg
value=$(compare -fuzz $fuzzyness previous.jpg current.jpg -metric mae diff.jpg 2>&1 | cut -f1 -d.)
if [ $value -gt $treshhold ] ; then
echo "ping $treshhold"
echo "Something moved" | mail -s "Movement" user@example.com -A diff.jpg
fi
# Comment below if you want to compare against a base line .. not previous image
cat current.jpg > previous.jpg
sleep 60
done

Example previous picture

Example current picture

I got mailed with result

Hints tips:

Use crop to detect only a part.

copy current.jpg to a second file
Use painting black a part and compair with different treshhold fuzzyness to get different hotspots.

Below detects RED, use above ide with crop to detect red/green/blue leds

compare -verbose -metric mae 1.jpg 2.jpg /tmp/1.diff
1.jpg JPEG 2560x1920 2560x1920+0+0 8-bit sRGB 248819B 0.050u 0:00.057
2.jpg JPEG 2560x1920 2560x1920+0+0 8-bit sRGB 248949B 0.030u 0:00.137
Image: 1.jpg
  Channel distortion: MAE
  Channel distortion: MAE
    red: 12517.5 (0.191005)
    green: 11967.1 (0.182607)
    blue: 12492.8 (0.190628)
    all: 12325.8 (0.18808)
1.jpg=>/tmp/1.diff JPEG 2560x1920 2560x1920+0+0 8-bit sRGB 1.19495MiB 1.470u 0:00.197

Energy bill and ledserver

Having a lot of devices and running a Lab wil use a lot of energy. Now with the energy crisis in Europe, i had to take a closer look at whats using power in my house.

I notished some weird usage patterns while measuring.

I’m using a few shelly power plugs, to measure devices and powerstrips.

With these devices you can control devices connected to it.
On/Off/Timer etcetera.
It wil measure the power usage in watts, and it even got a temperature sensor.
I like the fact that it perfectly integrates into your home automation using an extensive API.
curl commands to controll, and even MQTT messaging. Intergrating in Home Assistant is a breeze.

So i was monitoring a bunch of stuff using Nodered/Grafana/Homeassistant and saw some recurring usage.
But being always late to check things, i made use of my ledserver i’ve build a long time ago.

This ledserver consists of a Raspberry Pi Zero, with a led string and a API written in python.

Below is autostarted on the Raspberry

( I made this ledserver for work, it showed the status of servers and services. Beside that every colleage had a range which he could use for his own scripts. I made some little bash script templates to have led funtions standard in your bash profile.

#!/usr/bin/python


# apt-get install python-flask
#

import Adafruit_WS2801
import Adafruit_GPIO.SPI as SPI
import struct

from flask import Flask, render_template, request
app = Flask(__name__)

PIXEL_COUNT = 32
SPI_PORT   = 0
SPI_DEVICE = 0

pixels = Adafruit_WS2801.WS2801Pixels(PIXEL_COUNT, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))

pixels.clear()
pixels.show()

@app.route("/led/<deviceName>/<color>")
def action(deviceName, color):
        if deviceName == 'reset':
                print ("reset")
                pixels.clear()
        print (deviceName)
        led = int(deviceName)
        s = color
        r = int(s[ :2], 16)
        b = int(s[2:4], 16)
        g = int(s[4: ], 16)

        pixels.set_pixel_rgb(led, r,g,b)
        pixels.show()

        templateData = {
                'rled'  : r,
                'bled'  : b,
                'gled'  : g,
                'deviceName'  : deviceName,
        }
        return render_template('index.html', **templateData)

@app.route("/control/<controlcommand>")
def actioncommand(controlcommand):
        if controlcommand == 'clear':

                print("clear")
                pixels.clear()
                pixels.show()

        templateData = {
                'controlcommand'  : controlcommand,
        }
        return render_template('index.html', **templateData)

@app.route("/range/<start>/<stop>/<color>")
def rangecommand(start,stop,color):
        s = color
        r = int(s[ :2], 16)
        b = int(s[2:4], 16)
        g = int(s[4: ], 16)
        startled = int(start)
        stopled = int(stop)
        while (startled < stopled):
          pixels.set_pixel_rgb(startled, r,g,b)
          startled=startled + 1
        pixels.show()

        templateData = {
                'rangecommand'  : rangecommand,
        }


        return render_template('index.html', **templateData)



if __name__ == "__main__":
   app.run(host='0.0.0.0', port=8080, debug=True)

Now you can control the leds with a simple curl command:

curl http://ledserver:8080/range/startled/endled/colorinrgb
curl http://ledserver:8080/led/lednumber/colorinrgb
curl http://ledserver:8080/control/clear

So today i made a little script to show power usage.

I’m reading the current power usage from a LS120 Youless

Youless LS120 device, which you can connect to your P1 connector.

With below bash script i’m reading the webinterface and update the ledstring.
I was using this ledserver for general notification usage. Below a 2 minute hack ..

#!/bin/bash
while true; do
number=$(echo $(curl -s http://youlessip/a | grep Watt | head -1 | awk '{ print $1 }') / 100  | bc)
curl -s http://ledserver:8080/control/clear
curl -s http://ledserver:8080/range/00/$number/010101
sleep 10
done
Using 9 leds = 9xx watt

Challenged myself into programming a photo quiz

Got bored, and challenged myself …

How about a quess the picture from our photo collection??
(123580 photos .. )
So i show a random picture, and when i press ESC it will show some information about the picture …
Quess the year and the event

Well i gave myself 15 minutes to program something ..

I was watching a tv show meanwhile .. but i managed to come up with this …

This script is showing a picture, when you press ESC it wil show some details.After that it will select another random picture.

Improvements : reading tags and other metadata from my photo database, to give more information.

#!/bin/bash
while true ; do
shuf -n 1 allpix > /tmp/randompix
pix=$(cat /tmp/randompix | cut -f2 -d\> | cut -f1 -d\<)
dir=$(cat /tmp/randompix | cut -f4 -d\> | cut -f1 -d\< |cut -f3 -d\; | sed 's#\\#/#g')
displaypath=/mnt/${dir}/${pix}
info=$(cat /tmp/randompix | rev | cut -f-4 -d\\ | rev | cut -f1 -d \<)
convert -background black -fill white  -size 1920x1080 -pointsize 24 label:"$info" info.jpg
pqiv -i --fullscreen "$displaypath"
pqiv -i --fullscreen info.jpg
done

Which gave me … this

Welllll .. it toke 20 minutes .. so i lost
(Must have been the wine)

Homewizard Watermeter

Today i got my watermeter in.

It’s a little device you can place on your watermeter.

You get a plastic clip/holder which you can use to place the device on your watermeter. You can easily remove the device to read the values.

The device measures the little round gauge, so it has no idea what the current values are. Add the current values of your meter to the output of the device.

The device works by measuring the rotation of the red part.

By default it sends the information over the internet to a server. Your phone connects to this server and the app wil give you the graphs.

If you want your own intergration, you have to enable “local api”
See image on the right.
When you want realtime data, you have to connect a usb-c power supply.
When using batteries, the device wil only connect to wifi once per 5 minutes, and you can’t use the API.

I wrote a little test script in bash to draw graphs using mqtt and Nodered.

#!/bin/bash
calib=29.621
data=$(curl -s http://10.1.1.176/api/v1/data)
totalwater=$(echo $data | cut -f9 -d\" | tr -d ':,')
currentwater=$(echo $data | cut -f11 -d\" | tr -d ':,}')
totalwater=$( echo $totalwater + $calib | bc)
mosquitto_pub -h 127.0.0.1 -m "$totalwater" -t watermeter/total
mosquitto_pub -h 127.0.0.1 -m "$currentwater" -t watermeter/current

Variable calib is what my watermeter was when i attached the device.
It wil add this to the newly measured values.

Home Assistant found the device using autodiscovery

I have to look into adjusting for offset in HA

Spotify export

Nice .. you can request a data export from spotify, much like google’s takeout.

I wil export your streaming history, playlists, library and more.

Login spotify on a desktop, goto privacy settings and request data.

Nice to generate some statistics from this data.

For now the top 50 played artists (nr of times, artist)

790     "Rammstein"
507     "Fred Morrison"
478     "Lincoln Hilton"
437     "Avicii"
420     "Shooglenifty"
347     "Treacherous Orchestra"
323     "Battlefield Band"
321     "Breabach"
295     "Nightwish"
254     "The Fraser Shaw Trust"
236     "Michael McGoldrick"
207     "Peatbog Faeries"
207     "Calum Stewart"
203     "Lúnasa"
197     "Martyn Bennett"
194     "Jeff Wayne"
193     "The Prodigy"
173     "Kíla"
172     "Ross Ainslie and Jarlath Henderson"
170     "Buena Vista Social Club"
153     "Ross Ainslie"
150     "Dàimh"
138     "Bliss"
125     "Hans Zimmer"
124     "Rare Air"
118     "Michael McCann"
107     "Kyle Tuttle"
107     "Beinn Lee"
105     "Bourne & Macleod"
 89     "Wolfstone"
 88     "Ímar"
 83     "Afro Celt Sound System"
 81     "Gordon Duncan"
 81     "Armin van Buuren"
 80     "Face The West"
 79     "Tyurgen Kam"
 79     "Aly Bain"
 72     "Keltik Elektrik"
 71     "Duncan Chisholm"
 66     "Liz Carroll"
 63     "Project Smok"
 63     "Blazin' Fiddles"
 62     "Dagda"
 61     "Trail West"
 59     "Julian Winding"
 57     "Solar Fields"
 57     "Dougie McCance"
 56     "John McSherry"
 56     "AES DANA"
 52     "Gaelic Storm"

Get top 50 script

 cat  *json | grep artistName  | sort | uniq -c | sort -nr | head -50 | cut -f1,4,5 -d\" | sed s/,$//g

Collection of useful Linux scripts/commands

Sorting my fileserver i found a lot of Bash/Linux scripts maybe useful.

Some are tool usage only, maybe I’ll remove this page.
But this being my own log/notebook, who knows

Port knocker .. opens port 22 when you send a tcp packet to port 1600 first. Note: when portscanning it wil close again when accessing 1601 or 1599. Note2: NOT active on my server .. duh!

(Open with a browser or telnet/netcat)

iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -m recent --rcheck --name SSH -j ACCEPT
iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 1599 -m recent --name SSH --remove -j DROP
iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 1600 -m recent --name SSH --set -j DROP
iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 1601 -m recent --name SSH --remove -j DROP
iptables -A INPUT -m tcp -p tcp --dport 22 -j DROP

Dump and share your log, you get a short link to share

cat /var/log/Xorg.0.log | nc termbin.com 9999

Multiboot? This will reboot into another OS

#!/bin/bash
WINDOWS_TITLE=`grep -i "^menuentry 'Windows" /boot/grub/grub.
grub-reboot "$WINDOWS_TITLE"
reboot

Unreadable json?

cat file | python3 -m json.tool

screen stuff

Start in screen
Start screen in "detached" mode. This creates a new session but doesn't attach to it. This is useful for system startup scripts.

/usr/bin/screen -d -m -S backup /usr/local/bin/backup.sh
screen -r backup
Screensize
ctrl-a :fix # Screensize fix

Memory leak check

pmap <pid> | tail -1
Memory leak .. pmap opslaan en vergelijken met een pmap later.

Listen to remote microphone local playback

ssh -C monitor@remoteserver arecord -f dat | aplay -f dat

Root login to named account

ssh-copy-id root@remote-server (one time only)

then:

ssh root@remoteserver

Lookup named account

cat /var/log/secure | grep publickey | cut -f6 -d: | while read ; do ssh-keygen -lf ~/.ssh/authorized_keys | grep $REPLY ;done
2048 SHA256:l85g/NvPnEy85UVfJ5LJw3NvPnEy85UVfJ5LJw3NvPnEy85UVfJ5LJw3 haanstoot@minka (RSA)

Get all VMs from RHEV/Ovirt

curl -X GET -H "Accept: application/xml" -u username@domain:PaSSwoRd --cacert rhevm.cer https://rhvserver:443/api/vms  | grep -i "<name>" | grep -v "           " | cut -f2 -d\> | cut -f1 -d\<

Get all disks from RHV

log into ovirt engine and postgres
psql -d engine -U postgres -c 'select vm_names,disk_profile_name,storage_id,disk_id,image_guid,disk_alias from public.all_disks;'

Gluster heal info

gluster volume info | egrep "Volume Name|Type"  | grep -B1 Replicate | grep "Volume Name" | cut -c14- | while read ;do gluster volume heal $REPLY info > /tmp/$REPLY.out ; done
ls /tmp/*out  | while read ;do cat $REPLY | grep entries | grep -v "Number of entries: 0" >/dev/null && echo $REPLY ;done | while read heal ; do cat $heal ;done | tac |sed -e '/Number of entries: 0/I,+1 d' | tac | uniq

Some work tips i’ve posted

VI
Vim comments
toevoegen
ga op de eerste regel van de te commenten regel staan
ctrl-v (visual)
naar beneden tot eind regel (met cursor of pagedown)
ctrl-i
#
esc (kan even duren)
verwijderen
ga op de eerste regel van de te uncommenten regel staan
ctrl-v (visual)
naar beneden tot eind regel (met cursor of pagedown)
x
esc
Als user config geedit en geen schrijfrechten?
:!bash
chmod 666 bestand
:wq
chmod bestand naar wat hij was
Input / verwerk door bash vanuit vi
:0,5 !sort # eerste 5 regels door sort heen halen

:r !date # datum in je tekst document
Vim tabs / multiple files
vi /etc/hosts /etc/services
:n 
:prev
VIM7 tabs
vim -p /etc/hosts /etc/profile
:tabe /etc/services
:tabn
:tabp
Key mappen
:map <F7> :tabp <CR>
:map <F8> :tabn <CR>
zet deze in je $HOME/.vimrc ... zonder de eerste ":"
Vi redirect
:r date               # datum in text 
:1,5!sort -n          # sorteren van eerste 5 regels met een extern commando (kan ook met shift v een visual gedeelte selecteren)
:0,$!cut -f1 -d:      # alleen field 1 van text overhouden (delimiter :)
Vi inspringen bij loop code
selecteer met shift-v de regels die moeten inspringen.
daarna > of < gebruiken om te tabben
Speciale charakters met vi bekijken
:set list
een tab zier er dan uit als ^I
Bash
sudo vergeten in commando
service apache restart
sudo !!
Bash truck vorige commando
doet vorig commando met aangepast keyword
systemctl status ovirt-ha-broker.service
^status^start
{} expand
directory's archief30_tmp t/m archief35_tmp recursive directories chmod 2775 zetten
find achief{30..35}_tmp -type d -exec chmod 2775 {} \;

of

mkdir tmp{1..3}
echo pr{ut,utser}s
1 regel uit een script starten zonder copy-paste (bijvoorbeeld in een remote-console sessie)
grep ipa command uit cobber.ks en voor deze uit
( /usr/sbin/ipa-client-install --domain=mgtdomain --enable-dns-updates -w password --realm=domain --server=server.domain --hostname=hypervisor.domain --unattended --force-ntpd )
cat cobbler.ks | grep ipa | bash
Bash karakters omdraaien
Linkerhand rechterhand coordinatie probleem smile
upadte
ga op de d staan en druk ctrl-t
Bash laatste woord vorige regel
ls -latrd /var/data/extra/backup
chmod 775 <ESC(punt)>
Ssh forward bij een running sessie
[haanstoot@xxx202 ~]$ 
~C (tilde C)
-L8080:localhost:80 (redirect localhost port 80 naar eigen machine poort 8080
-D9999 (Dynamic socks forward, zie andere post in deze wiki)
CTRL-C werkt niet?
ctrl\
SSH .ssh/config
KeepAlive yes <---------- dont die on me
ServerAliveInterval 60 <---------- dont die on me
Host *.domain <-- voor alle hosts in radlan fast login en geen vraag over key
   StrictHostKeyChecking no 
   UserKnownHostsFile=/dev/null

host *.domain <-- domain is altijd mijn user
        user haanstoot
        GSSAPIAuthentication no <-- speedup

host pruts*.domain
        user pi
Parallel tasks
4 parallel jobs
find jpg -type f -name \*.jpg -print0 | xargs -0 -n1 -P4 ./convert.sh
clusterssh truck
clusterssh alle svgs <--- naar alle svgs
sudo su -
cd /bricks/*/store <--- cd naar deze als bestaat
cd /rhgs/brick0*/store <-- cd naar deze als bestaat (nu sta je in atelier directories als deze bestaat als root in de svgs)
pwd | grep store || logout <--- geen atelier dir waar je staat? dan logout
id | grep haanstoot && logout <-- net logout? dus eigen user ... dan logout
eindresultaat ... als root in atelier volumes op svgs waar ze bestaan
Start in screen
/usr/bin/screen -d -m -S backup /usr/local/bin/backup.sh
screen -r backup
Start screen in "detached" mode. This creates a new session but doesn't attach to it. This is useful for system startup scripts.
Sudo vergeten
systemctl restart httpd (wil niet als user)
sudo !!
(doet "sudo systemctl restart httpd")
Reuse arguments

Bijvoorbeeld:
# touch file1 file2 file3 file4
# chmod 777 !*

Voert uit: chmod 777 file1 file2 file3 file4
5 meeste disk gebruikers human readable
du -hsx * | sort -rh | head -5
rm groot bestand duurt lang
: > /mappen2/archieven/test.img
Check memory bankjes
sudo dmidecode| grep  -i -B1 "Form Factor" (B1 betekend BEFORE 1 regel meenemen, A is AFTER)

        Size: 8192 MB
        Form Factor: DIMM
--
        Size: 8192 MB
        Form Factor: DIMM
--
        Size: No Module Installed
        Form Factor: DIMM
--
Set time and restart NTP service
sudo service ntpd stop && sudo ntpdate ntp1.domain && sudo ntpdate ntp2.domain && sudo service ntpd start
Aliases en terminal kleuren
In een terminal f*ckedup colors?
komt door de alias
alias ls='ls --color'
deze even uitzetten / niet gebruiken?
\ls
Werkt voor alle aliassen
Snelle delete
Niet onderstaande gebruiken voor grote dirs
rm -rf /path/bla
Maar
mkdir /tmp/leeg ; rsync -a --delete /tmp/leeg /path/bla
Bash best practices
BashBestPractices
Script log replay
script --timing=/tmp/time.txt /tmp/script.log
scriptreplay -t /tmp/time.txt /tmp/script.log

There are toooo many cool sniplets .. only adding when i’m bored.

Finding files on my fileserver

I use several tools to find files on my server.

Loads of stuff on my main fileserver.
(Graph is a great tool called DUC) https://duc.zevv.nl/

Besides a search engine, i have a file finder.
Due to the massive amount of data, i like to find things by other means than knowing the directory structure.

I can find files by filename, but also by contents.

I’ll talk about find by contents first.

I’ve got loads of documents in Pdf, HTML, txt, doc, sheets , wordperfect etcetera.
Those documents i can find using a tool named Namazu.
This is quite a old tool, but i’m using it for a long time and it still works great.
I didn’t find a better replacement yet.
(But i’ve been looking into : elasticsearch, Solr, Lucene)

http://www.namazu.org/ is easy to install, but if you want the tool to scrape different kinds of documents you have to add some additional software.

My multipurpose printer can scan pages in pdf.
Those are only embedded jpg’s in a pdf container.
I will talk about how i handle these later.

My current start page :
This index contains 267,763 documents and 14,036,762 keywords.
Search example of namazu

Some things to consider when implementing namazu:

  • tweak the file types to scrape, it makes no sense to scrape binaries
  • tweak the directories to scrape (example below)
    • 0 1 * * 1 fash /usr/bin/mknmz -f /etc/namazu/mknmzrc –output-dir=/namazu/ /mnt/private/paperwork/ /mnt/private/information/ /mnt/private/Art\ en\ hobby\ Projects/ /mnt/private/Music\ Projects/ /mnt/private/bagpipe-music-writer/ –exclude=XXX –exclude=/mnt/binaries > /tmp/namazu.log 2>&1
  • you can set a parameter in the config for search only, this disables downloading the found link in the results!

Before Namazu i used HtDig.

Screenshot htdig

HtDIg also can scrape remote websites, Namazu can’t.

Preparing PDF for indexing:

I’ve written some scripts to make PDFs containing scanned text scrape-able.
( https://gitlab.com/fash/inotify-scanner-parser )
What it does:

  • My scanner puts a scanned pdf on my fileserver in a certain directory
  • Inotify detects a written file
  • it will copy the file, run OCR on it (tesseract) and writes a txt file (scapeable)
  • After that the text will be embedded (overlay) on the PDF, so now it becomes searchable/scrapeable
  • When certain keywords are found, it will sort documents in subdirs
Example from a scanned jpg, i can find OCR words!
(note .. the overlay is exact on the found words)

Finding files by name:

For finding files a made a little webpage like this:

It is a simple webpage grabbing through a list of files.
It takes the first keyword and does a grep, it takes a second keyword to match also.
I can select different file databases to search. (This case is private)
Between search and private i can give the number of entries to print.
So i can do
Search “ansible” NOT “tower” 50 entries from the public fileset

Crontab:

20 5 * * * /usr/bin/find /mnt/shark*  > /var/www/html/findfiles/sharkoon
10 4 * * * /usr/bin/find /tank/populair > /var/www/html/findfiles/populair
20 4 * * * /usr/bin/find /tank/celtic > /var/www/html/findfiles/celtic
etc

And a php script (dirty fast hack, never came around it to make it a final version)

<html><head><title></title></body>
<font face="Tahoma"><small>|keyword|(keyword)|search|(nr results)|NOT SECOND KEYWORD|share|</small><BR>
Search: <form method="post" action="/findfiles/?"><input type="Text" name="words" size=10 value=""><input type="Text" name="words2" size=10 value=""><input type="Submit" name="submit" value="search"><input type="Text" name="nrlines" size=3 value=""><input type="checkbox" name="not" unchecked>
<SELECT NAME=findfile>
<OPTION VALUE=private>private
<OPTION VALUE=celtic>celtic
<OPTION VALUE=populair>populair
<OPTION VALUE=dump>public
<OPTION VALUE=sharkoon>sharkoon
</SELECT>
</form>
<P><PRE>
<?php
$words2=$_POST['words2'];
$words=$_POST['words'];
$filefile=$_POST['filefile'];
$findfile=$_POST['findfile'];
$nrlines=$_POST['nrlines'];
$not=$_POST['not'];


if ($words2=="xsearch") { $findfile="other"; $words2=""; }
if ($nrlines) {  } else { $nrlines=100; }
if ($words && $words2=="") {
$words = preg_replace("(\r\n|\n|\r)", "", $words);
$words = preg_replace("/[^0-9a-z]/i",'', $words);
$command = "/bin/cat $findfile |/bin/grep -i $words |head -$nrlines";
$blah=shell_exec($command);
$blah=str_replace($words, "<b><font color=red>$words</font></b>",$blah);
print $blah;
}
if (($words) and ($words2)) {
$words = preg_replace("(\r\n|\n|\r)", "", $words);
$words = preg_replace("/[^0-9a-z.]/i",'', $words);
$words2 = preg_replace("(\r\n|\n|\r)", "", $words2);
$words2 = preg_replace("/[^0-9a-z.]/i",'', $words2);
if ($not=="on") {
$command = "/bin/cat $findfile |/bin/grep -i $words | /bin/grep -iv $words2 |head -$nrlines";
} else {
$command = "/bin/cat $findfile |/bin/grep -i $words | /bin/grep -i $words2 |head -$nrlines";
}
$blah=shell_exec($command);
$blah=str_replace($words, "<b><font color=red>$words</font></b>",$blah);
$blah=str_replace($words2, "<b><font color=red>$words2</font></b>",$blah);
print $blah;
}
?>
</PRE>
</body></html>

Scraping podcast which uses a javascript to obfuscate mp3 links

wget-ting the page only gave me flat html, but no readable links.

We need the rendered version, phantomjs wil help

wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2

printsource.js

var system = require('system');
var page   = require('webpage').create();
var url    = system.args[1];
page.open(url, function () {
  console.log(page.content);
  phantom.exit();
});

Run phantomjs

phantomjs-2.1.1-linux-x86_64/bin/phantomjs printsource.js  https://xxxxxxxx/show/xxxx > out

So now i got the rendered page, get mp3’s and titles, for this example

cat out | sed 'N;s/\n/,/' | cut -f2,7 -d\" | while read line ; do
mp3=$( echo $line | cut -f1 -d\")
title=$( echo $line | cut -f3 -d\&gt; | tr -d '/&lt;&gt;[]]\!,;' | tr -d "'" | sed s/CDATA//g | sed s#title##g | sed s/:/-/g )
echo "$mp3 $title"
wget $mp3 -O "$title.mp3"
done

bash downloadscript
done

ZFS replace disk

I’m using ZFS for my main fileserver, this pool was created over 10 years ago.
Meanwhile i’ve: Swapped broken disks, switched disks for bigger ones and effectively resized my storage 2 or 3 times. Never had any corruption.

Yesterday i say a warning that one of the disks in the pool was OFFLINE.
Today i replaced it using below command’s

  • Put the disk in OFFLINE mode (if needed, mine was already offline)
    • zpool offline tank sdb
  • Remove disk from system
    • echo 1 | sudo tee /sys/block/sdb/device/delete
  • Remove the disk physically
  • Insert the replacement disk. And copy headers/structure from another disk
    • sgdisk –replicate=/dev/sdb /dev/sda
    • sgdisk –randomize-guids /dev/sdb
  • Run the zpool replace command. 
    • zpool replace tank /dev/sdb
  • Use online command to activate disk (no needed in my case, it already did that)
Tips:

# My labels with serials fell off :(
dd if=/dev/sdb of=/dev/null -> blinky led .. 

# What is the serial?
sudo hdparm -i /dev/sdb | grep Serial

Output

root@latex:~# sgdisk --replicate=/dev/sdb /dev/sda
The operation has completed successfully.
root@latex:~# sgdisk --randomize-guids /dev/sdb
The operation has completed successfully.
root@latex:~# zpool replace tank /dev/sdb
root@latex:~# zpool status
  pool: tank
 state: DEGRADED
status: One or more devices is currently being resilvered.  The pool will
        continue to function, possibly in a degraded state.
action: Wait for the resilver to complete.
  scan: resilver in progress since Wed May 18 11:31:21 2022
    5.64T scanned out of 14.4T at 331M/s, 7h42m to go
    1.88T resilvered, 39.16% done
config:

        NAME             STATE     READ WRITE CKSUM
        tank             DEGRADED     0     0     0
          raidz1-0       DEGRADED     0     0     0
            sda          ONLINE       0     0     0
            replacing-1  REMOVED      0     0     0
              old        REMOVED      0     0     0
              sdb        ONLINE       0     0     0  (resilvering)
            sdc          ONLINE       0     0     0

errors: No known data errors

New NFO generator using yad

old post : https://www.henriaanstoot.nl/2021/02/08/kodi-movies-and-metadata/

Added poster art generator at the bottom of this page

Made a new NFO generator to get private movies properly scraped in kodi.

What does it do?

  • Genererates NFO for Kodi
  • Goes recursive though given directories
  • Skips movies with existing nfo
  • Previews the movie using VLC (So you can see what it is you are adding information for)
  • Tries to make a nice title from filename and year
  • Gets year from metadata
  • Fills tags from yad forms
  • next version i will add a poster generator from another nfo script i made. (It allready works, but i want to add a frame selector.
  • Adds new genre/actors/places if they dont exists
#!/bin/bash
# Needs mediainfo and yad
#find /media/geisha/private/Media/video/ -type f | egrep -i "mkv$|avi$|mpg$|mp4$|ogv$" | while read ; do
find $1 -type f | egrep -i "mkv$|avi$|mpg$|mp4$|ogv$" | while read ; do
# check if nfo exists
short=$(echo  "$REPLY" | rev | cut -f-2 -d/ | rev)
file=$( echo "$REPLY" | rev | cut -f2 -d. | rev).nfo
filename=$( basename "$REPLY" | cut -f1 -d. )
if [ -f "$file" ] ; then
	echo "$file found .. so skipping"
else
	echo "Not found $file"
nohup vlc "$REPLY" &
# check if find date
year=$(mediainfo "$REPLY" | grep Encoded |head -1 2>/dev/null | awk '{ print $5 }' | cut -c-4)
yeartitle="$year - "
if [ "$year" == "" ] ; then 
	year=$(echo $file | tr -cd '2[0-9][0-9][0-9]')
	if [ ! "$year" == "" ] ; then 
		#no year in filename
		yeartitle=""
	else
		yeartitle="$year - "
	fi
	
fi
# create nfo
forminfo=$(yad --title="nfo form $short" --text="Please enter:" --form --field="Title" --field="Year" --field="Placenew" --field="Genrenew" "$yeartitle $filename" "$year" "" "" --form --columns=2 --item-
separator="," --field="Place":CB --field="Genre":CB "$(paste -s -d"," < places)" "$(paste -s -d"," < genres)")
#echo $forminfo
#2020 - |2020|placenem|genrene|Hilversum|Vacation|
title=$(echo $forminfo | cut -f1 -d\|)
year=$(echo $forminfo | cut -f2 -d\|)
placenew=$(echo $forminfo | cut -f3 -d\|)
genrenew=$(echo $forminfo | cut -f4 -d\|)
place=$(echo $forminfo | cut -f5 -d\|)
genre=$(echo $forminfo | cut -f6 -d\|)
# Plot
plot=$(yad --form --field="Text::TXT" --geometry="600x200")
#plot=$( echo $plot | cut -f1 -d\\)
#plot=$( echo $plot | cut -f1 -d|)
# Actors
actors=$(yad --list  --geometry="200x480"  --print-all --column= --column=:chk --column= --column=:chk $( cat actors | cut -f1 -d" "  |while read user; do echo -n "$user false " ;done ) )
echo $actors | grep "NEW|TRUE" >/dev/null 
if [ $? -eq 0 ] ; then
vi actors
actors=$(yad --list  --geometry="200x480"  --print-all --column= --column=:chk --column= --column=:chk $( cat actors  | cut -f1 -d" " |while read user; do echo -n "$user false " ;done ) )
fi
if [ ! "$placenew" == "" ] ; then
	place="$placenew"
	echo "$placenew" >> places
fi
if [ ! "$genrenew" == "" ] ; then
	genre="$genrenew"
	echo "$genrenew" >> genres
fi
(
echo '<?xml version="1.0" encoding="utf-8" standalone="yes"?>'
echo "<movie>"
echo "  <plot>$plot</plot>"
echo "  <outline />"
echo "  <title>$title</title>"
echo "  <year>$year</year>"
echo "  <country>$place</country>"
cat actors | cut -f1 -d" " | while read actorname ; do
	echo $actors | grep "$actorname|TRUE"  >/dev/null
		if [ $? -eq 0 ] ; then
		name=$(grep $actorname actors | cut -f2 -d\" )
		echo "<actor>"
		echo "  <name>$actorname</name>"
		echo "  <role>$name</role>"
		echo "</actor>"
	fi
done
echo "  <genre>$genre</genre>"
echo "  <art>"
echo "  </art>"
echo "</movie>" 
) > "$file"  
fi
killall vlc
done

And some files

> actors
Firstname "Firstname Lastname"
> genres
Vacation
Pruts
> places
Netherlands

Poster generator (select poster thumbnail to use)

Generates 3 poster images and 3 thumbs to select.
Thumbs are 10 seconds into the movieclip, midway and on 2/3

short=$(basename $1 | cut -f1 -d.)

three=$(( $(mediainfo --Inform="Video;%Duration%" $1) / 3000 ))
rm -f thumb*.jpg poster*.jpg
first=10
#ffmpeg -ss $first -i $1 -vf scale=320:-1 -frames:v 1 -q:v 2 thumb1.jpg  -hide_banner -loglevel error
ffmpeg -ss $first -i $1 -frames:v 1 -q:v 2 thumb1.jpg  -hide_banner -loglevel error
second=$(( $three  ))
ffmpeg -ss $second -i $1 -frames:v 1 -q:v 2 thumb2.jpg  -hide_banner -loglevel error
third=$(( $three * 2 ))
ffmpeg -ss $third -i $1 -frames:v 1 -q:v 2 thumb3.jpg  -hide_banner -loglevel error
convert thumb1.jpg -resize x1000 -gravity center -crop 666x1000 poster1.jpg
convert thumb1.jpg -resize x320 -gravity center -crop 212x320 thumb1s.jpg
convert thumb2.jpg -resize x1000 -gravity center -crop 666x1000  poster2.jpg
convert thumb2.jpg -resize x320 -gravity center -crop 212x320 thumb2s.jpg
convert thumb3.jpg -resize x1000 -gravity center -crop 666x1000  poster3.jpg
convert thumb3.jpg -resize x320 -gravity center -crop 212x320 thumb3s.jpg


out=$(yad --form --title="Select poster thumbnail"  --geometry="+0+0" \
       --columns="3" \
       --field="!thumb1s-1.jpg! :fbtn" "echo poster1-1.jpg"  \
       --field="!thumb2s-1.jpg! :fbtn" "echo poster2-1.jpg"  \
       --field="!thumb3s-1.jpg! :fbtn" "echo poster3-1.jpg"
)
if [ ! "$out" == "|||" ] ; then
choice=$(echo $out | sed s/\ \|\|\|//g | rev | cut -f1 -d" " | rev)
echo cp $choice ${short}-poster.jpg
fi