About me: My name is Solène Rapenne, pronouns she/her. I like learning and sharing knowledge. Hobbies: '(NixOS BSD OpenBSD Lisp cmdline gaming security QubesOS internet-stuff). I love percent and lambda characters. OpenBSD developer solene@.

Contact me: solene+www at dataswamp dot org or @solene@bsd.network (mastodon). If for some reason you want to support my work, this is my paypal address: donate@perso.pw.

Consider sponsoring me on Patreon to help me writing this blog and contributing to Free Software as my daily job.

Simple scripts I made over time

Written by Solène, on 19 July 2021.
Tags: #openbsd #scripts #shell

Comments on Fediverse/Mastodon

1. Introduction §

I wanted to share a few scripts of mine for some time, here they are!

2. Scripts §

Over time I'm writing a few scripts to help me in some tasks, they are often associated to a key binding or at least in my ~/bin/ directory that I add to my $PATH.

2.1. Screenshot of a region and upload §

When I want to share something displayed on my screen, I use my simple "screen_up.sh" script (super+r) that will do the following:

  • use scrot and let me select an area on the screen
  • convert the file in jpg but also png compression using pngquant and pick the smallest file
  • upload the file to my remote server in a directory where files older than 3 days are cleaned (using find -ctime -type f -delete)
  • put the link in the clipboard and show a notification

This simple script has been improved a lot over time like getting a feedback of the result or picking the smallest file from various combinations.

test -f /tmp/capture.png && rm /tmp/capture.png
scrot -s /tmp/capture.png
pngquant -f /tmp/capture.png
convert /tmp/capture-fs8.png /tmp/capture.jpg
FILE=$(ls -1Sr /tmp/capture* | head -n 1)

MD5=$(md5 -b "$FILE" | awk '{ print $4 }' | tr -d '/+=' )

ls -l $MD5

scp $FILE perso.pw:/var/www/htdocs/solene/i/${MD5}.${EXTENSION}
echo "$URL" | xclip -selection clipboard

notify-send -u low $URL

2.2. Uploading a file temporarily §

Second most used script of mine is a uploading file utility. It will rename a file using the content md5 hash but keeping the extension and will upload it in a directory on my server where it will be deleted after a few days from a crontab. Once the transfer is finished, I get a notification and the url in my clipboard.


if [ -z "$1" ]
        echo "usage: [file]"
        exit 1
MD5=$(md5 -b "$1" | awk '{ print $NF }' | tr -d '/+=' )

scp "$FILE" perso.pw:/var/www/htdocs/solene/f/${NAME}

echo -n "$URL" | xclip -selection clipboard

notify-send -u low "$URL"

2.3. Sharing some text or code snippets §

While I can easily transfer files, sometimes I need to share a snippet of code or a whole file but I want to ease the reader work and display the content in an html page instead of sharing an extension file that will be downloaded. I don't put those files in a cleaned directory and I require a name to give some clues about the content to potential readers. The remote directory contains a highlight.js library used to use syntactic coloration, hence I pass the text language to use the coloration.


if [ "$#" -eq 0 ]
        echo "usage: language [name] [path]"
        exit 1

cat > /tmp/paste_upload <<EOF
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <link rel="stylesheet" href="default.min.css">
        <script src="highlight.min.js"></script>

        <pre><code class="$1">

# ugly but it works
cat /tmp/paste_upload | tr -d '\n' > /tmp/paste_upload_tmp
mv /tmp/paste_upload_tmp /tmp/paste_upload

if [ -f "$3" ]
    cat "$3" | sed 's/</\&lt;/g' | sed 's/>/\&gt;/g' >> /tmp/paste_upload
    xclip -o | sed 's/</\&lt;/g' | sed 's/>/\&gt;/g' >> /tmp/paste_upload

cat >> /tmp/paste_upload <<EOF

</code></pre> </body> </html>

if [ -n "$2" ]

FILE=$(date +%s)_${1}_${NAME}.html

scp /tmp/paste_upload perso.pw:/var/www/htdocs/solene/prog/${FILE}

echo -n "https://perso.pw/prog/${FILE}" | xclip -selection clipboard
notify-send -u low "https://perso.pw/prog/${FILE}"

2.4. Resize a picture §

I never remember how to resize a picture so I made a one line script to not have to remember about it, I could have used a shell function for this kind of job.


if [ -z "$2" ]

convert -resize "$PERCENT" "$1" "tn_${1}"

3. Latency meter using DNS §

Because UDP requests are not reliable they make a good choice for testing network access reliability and performance. I used this as part of my stumpwm window manager bar to get the history of my internet access quality while in a high speed train.

The output uses three characters to tell if it's under a threshold (it works fine), between two threshold (not good quality) or higher than the second one (meaning high latency) or even a network failure.

The default timeout is 1s, if it works, under 60ms you get a "_", between 60ms and 150ms you get a "-" and beyond 150ms you get a "¯", if the network is failure you see a "N".

For example, if your quality is getting worse until it breaks and then works, it may look like this: _-¯¯NNNNN-____-_______ My LISP code was taking care of accumulating the values and only retaining the n values I wanted as history.

Why would you want to do that? Because I was bored in a train. But also, when network is fine, it's time to sync mails or refresh that failed web request to get an important documentation page.


dig perso.pw @  +timeout=1 | tee /tmp/latencecheck

if [ $? -eq 0 ]
        time=$(awk '/Query time/{
                if($4 < 60) { print "_";}
                if($4 >= 60 && $4 <= 150) { print "-"; }
                if($4 > 150) { print "¯"; }
        }' /tmp/latencecheck)
        echo $time | tee /tmp/latenceresult
        echo "N" | tee /tmp/latenceresult
    exit 1

4. Conclusion §

Those scripts are part of my habits, I'm a bit lost when I don't have them because I always feel they are available at hand. While they don't bring much benefits, it's quality of life and it's fun to hack on small easy pieces of programs to achieve a simple purpose. I'm glad to share those.