• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Useful scripts

dpx

Member

Thanks: 3
Messages: 53

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: 185
Messages: 549

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: 666
Messages: 4,615

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: 13
Messages: 141

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: 666
Messages: 4,615

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: 194
Messages: 612

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: 666
Messages: 4,615

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 ;)