Useful scripts

dpx

Member

Thanks: 4
Messages: 58

Since I am new here I am still trying to wrap my head around basic package management, who installs what, from where etc. So here are two one liners that help me a lot:

List all explicitly installed packages from 'unknown-repository' (so lists all ports?), sorted by size:
Code:
pkg query -e '%a = 0' "%n;%v;%sh;%R;%sb" | grep -i unknown-repository | sort --field-separator=';' -g -k5 | cut -f 1,2,3,4 -d';' | column -s';' -t
Same but for known repositories (so lists all binary packages?):
Code:
pkg query -e '%a = 0' "%n;%v;%sh;%R;%sb" | grep -v unknown-repository | sort --field-separator=';' -g -k5 | cut -f 1,2,3,4 -d';' | column -s';' -t
Sizes are not ideal since I don't count all the dependencies but it is good enough to give a bird view of the system userland.
 

ronaldlees

Aspiring Daemon

Thanks: 266
Messages: 670

A command line I use often, to corral bunches of files into a directory:

find . -type f -name "*.txz" -exec cp {} /usr/home/ron/collection \;


It's old as dirt, and pretty obvious, but maybe not to some noobies. It searches, starting at the dot dir, for all files with the "txz" extension, and puts them into the ~/collection directory.
 

graudeejs

Son of Beastie

Thanks: 686
Messages: 4,611

Some while ago I was fed up with all the linux package management tools. Every one of of them works differently. So I decided to write simple wrapper called repkg.
Here's link to repository: https://github.com/graudeejs/repkg

The idea is to have common interface that will abstract package management in a way that would be similar to pkgng.
 

juan9182

Active Member

Thanks: 19
Messages: 166

A command line I use often, to corral bunches of files into a directory:

find . -type f -name "*.txz" -exec cp {} /usr/home/ron/collection \;


It's old as dirt, and pretty obvious, but maybe not to some noobies. It searches, starting at the dot dir, for all files with the "txz" extension, and puts them into the ~/collection directory.
is the swiss army knife for me, works across FreeBSD and Linux systems..classic but util
 

graudeejs

Son of Beastie

Thanks: 686
Messages: 4,611

As a web systems developer I often need to dump production database. However to be able to do that, I also need to ssh to production sever (due to firewall on DB server).
This is very cumbersome.
To make my life easier I've developed two scripts that utilize my password store.

db.sh allows me to access production db from remote host by simply typing
Code:
db.sh db/production
dump_db.sh allows me to dump production db from remote host
Code:
dump_db.sh db/production > dump.sql.gpg
To improve performance, I'm compressing dumped SQL with gzip server side, then ungzip on local PC. Instantly dump is gpg encrypted with my public key (because I'm on laptop and I don't fully trust hardware based encryption)

The password store file contains simple configuration.
Something like
Code:
db_password
ssh: user@example.com
host: in-the-cloud.eu-west-1.rds.amazonaws.com
adapter: mysql
username: db_username
database: db_name
port: 3306
P.S.
While I'm here I want to mention Nitrokey that ensures that my private keys stay safe.
 

shepper

Aspiring Daemon

Thanks: 233
Messages: 701

I have a couple of scripts that I use to retrieve U.S Weather service information. I prefer these over Gnome/KDE/Xfce4 weather applets because they retrieve from U.S rather than the hard coded source in Northern Europe. Also does not constantly poll a site saving cpu cycles and bandwidth.
This one retrieves Aviation weather from the nearest airport that offers weather services. My nearest airport is in Yakima,WA (KYKM). Alternative US airport codes are here.
Code:
#!/bin/sh
curl -sk http://tgftp.nws.noaa.gov/data/observations/metar/decoded/KYKM.TXT | \
fold -w 78 -s
echo ""
printf "<Enter to Close>"; read nothing
I've coupled it with a x11/yad entry that places a small cloud in my panels system tray and it generates this:
Code:
YAKIMA AIR TERMINAL, WA, United States (KYKM) 46-34N 120-32W 324M
Oct 26, 2017 - 05:53 PM EDT / 2017.10.26 2153 UTC
Wind: from the NNE (020 degrees) at 5 MPH (4 KT):0
Visibility: 10 mile(s):0
Sky conditions: clear
Temperature: 66.9 F (19.4 C)
Dew Point: 34.0 F (1.1 C)
Relative Humidity: 29%
Pressure (altimeter): 30.29 in. Hg (1025 hPa)
ob: KYKM 262153Z 02004KT 10SM CLR 19/01 A3029 RMK AO2 SLP256 T01940011
cycle: 22

<Enter to Close>
Code:
# Start weather system tray applet
(sleep 2 && \
yad --notification --image=weather-overcast \
 --text="Yakima, WA Weather" --no-middle \
 --command='xterm +sb -g 72x16-0+38 \
 -T "Yakima, WA Weather" \
 -e "/home/jsh/scripts/weather.sh"') &
A similiar script can be used to obtain a Terminal Area forecast:
Code:
#!/bin/sh
# This is a simple script that downloads current weather conditions and zone
# forecast from the National Weather Service and formats the output.
#
# To change the forecast zone, replace wa/waz027 with another forecast zone.
# See <http://weather.noaa.gov/pub/data/forecasts/zone/> for a list.
#
curl -sk http://tgftp.nws.noaa.gov/data/forecasts/zone/wa/waz027.txt| \
fold -w 78 -s
echo ""
printf "<Enter to Close>"; read nothing
Lastly, a script to run an animated gif of the 4 latest radar images using graphics/imagemagick

Code:
#!/bin/sh

# This is a simple script that downloads an animated gif of
# the latest 4 radar images

# This script is configured for the Pendelton, OR radar site
#
# To change the site edit "NCR/PDT_loop.gif".

animate -loop 0 -title "PDT Radar Loop" https://radar.weather.gov/ridge/lite/NCR/PDT_loop.gif
An interactive map for US radar sites is here.
 
Last edited:

graudeejs

Son of Beastie

Thanks: 686
Messages: 4,611

Here's trick how I attach my GELI encrypted disks using sysutils/password-store:
Code:
$ pass show geli/password/in/password-store | head -n 1 | sudo geli attach -p -k - /dev/da0
This way password is passed via stdin as keyfile for geli.
To initialize geli safely, first generate password with password store, then run something like:
Code:
$ pass show geli/password/in/password-store | head -n 1 | sudo geli init -P -K - /dev/da0
Also to ensure that I don't forget how to attach disk, I simply added command into comments of password store entry.

Note that if you're to save password in keyfile, ensure that it ends with newline since it's not stripped from command line ;)
 

graudeejs

Son of Beastie

Thanks: 686
Messages: 4,611

If you're using ProtonVPN and ever tried to setup OpenVPN on FreeBSD you might notice that if you generate config for linux there are two lines that don't work on FreeBSD
Code:
up /etc/openvpn/update-resolv-conf
down /etc/openvpn/update-resolv-conf
A quick google search will unveil https://github.com/masterkorp/openvpn-update-resolv-conf, however my problem with this script is that it uses bash.

To solve this bug, I forked repo and rewrote code to work with sh on FreeBSD https://github.com/graudeejs/openvpn-update-resolv-conf-freebsd/blob/master/update-resolv-conf.sh


Warning. It's not fully tested as I didn't bother to test all possible usecases, however for my ProtonVPN setup it works like charm. If you have problems with it feel free to file a bug report in github.
 

_martin

Aspiring Daemon

Thanks: 130
Messages: 696

Speaking of handy one-liners: from time to time I find myself in a single boot console, often without proper terminal settings and practically unable to use standard (vi) editor. That's where ed friend comes in very handy. Let's say I need to edit config file and change the line "Port 22" to "Port 666":

printf "/^Port/s/Port 22/Port 666/\nw\nq" | ed -s test.conf

Tested on FreeBSD, Linux, HP-UX and Solaris.
 

Sensucht94

Well-Known Member

Thanks: 310
Messages: 335

some useful commands I always keep in my ~/.bin directory:

- uptime in mins:
uptime | awk -F, '{sub(".*up ",x,$1);print $1}' | sed -e 's/^[ \t]*//'

- installed package count:
doas pkg info | wc -l | sed -e 's/^[ \t]*//'

- zroot/ROOT/default free %:
df -hk | egrep 'ROOT|/$' | awk '{print$5}'

I have yet to find a way a easy way to measure free RAM percentage in FreeBSD, which doesn't involve operations between vm.stats.vm.v_*_count sysctl values. All the more I' m not sure I truly understood the relationship between inactive, free, wired and cached memory. NetBSD's mount_procfs(8) brings some additional nodes for compatibility with Linux, so as that when the pseudo-filesystem is mounted (default) , i can always rely on /proc/meminfo to grep all needed info, included free mem percentage with something like:
$(awk -F ':|kB' '/MemFree:/ {printf $2}' /proc/meminfo) / 1024

For FreeBSD however, I found a very cool script online, that I'm going to upload immediately beneath (as 'mem.txt')
To just get the free mem percentage, you can pipe it through awk and cut like that :
mem.sh | awk 'FNR==18 {print $6}' | cut -c 1-3


Off-Topic: graudeejs, looking at your profile photo, are you Ibara from OpenBSD? I'm a great fan of his ports including oksh, mg, and streamlink :cool:
 

Attachments

Top