Paul Sutton

Personal Blog

Bash scripting 24 – firewalls

I am still experimenting with gopher, however this got me thinking about system security, so I have installed.

If you are not sure what you are doing with this, please consult the man page

man ufw

So this describes ufw as

ufw - program for managing a netfilter firewall

As I am ssh'd in to the pi, then the firewall needs to be enabled, however, first make sure that that port 22 (or which ever port you are using for ssh) is allowed. I have to do this as being logged in via ssh, emabling the firewall could block ssh preventing access.

sudo ufw allow 22

Then check the status

sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
22                         ALLOW       Anywhere                  
22 (v6)                  ALLOW       Anywhere (v6)             

To confirm that port 22 is allowed, then enable

sudo ufw enable

Links

Tags

#Bash,#Bashscripting,#BashScripting#Security,#Firewall


Mastodon ShellLabs Join Mastodon
AI statement : Consent is NOT granted to use the content of this blog for the purposes of AI training or similar activity. Consent CANNOT be assumed, it has to be granted.

Donate using Liberapay

Bash scripting 23 – users

To add new users to a system, we can use the command. You will need to have root privileges for this, so you need to prefix with sudo, e.g.

**Permissions, the initial user you create when setting up the pi, has sudo or admin privileges. Additional users will not. Unless you take steps to give that user those permissions.**

sudo useradd <user>

The rest of the process is interactive, in this example I am adding the user gopher to the raspberry Pi4 system we are also using for testing cron.

sudo adduser gopher
Adding user `gopher' ...
Adding new group `gopher' (1001) ...
Adding new user `gopher' (1001) with group `gopher (1001)' ...
Creating home directory `/home/gopher' ...
Copying files from `/etc/skel' ...
New password: 
Retype new password: 
passwd: password updated successfully
Changing the user information for gopher
Enter the new value, or press ENTER for the default
	Full Name []: Gopher
	Room Number []: 0
	Work Phone []: 0
	Home Phone []: 0
	Other []: 0
Is the information correct? [Y/n] y
Adding new user `gopher' to supplemental / extra groups `users' ...
Adding user `gopher' to group `users' ...

I can now login to the user gopher on the Raspberry Pi with

$ ssh gopher@192.168.1.202
gopher@192.168.1.202's password: 
Linux raspberrypi 6.12.20+rpt-rpi-v8 #1 SMP PREEMPT Debian 1:6.12.20-1+rpt1~bpo12+1 (2025-03-19) aarch64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
gopher@raspberrypi:~ $ 

To change the password on a GNU/Linux system, use

passwd

To delete a user, use the userdel command, I am not going into this here, so as with other commands, there is a man page

man userdel
userdel - delete a user account and related files

Tags

#Bash,#Bashscripting,#BashScripting#users,#adding


Mastodon ShellLabs Join Mastodon
AI statement : Consent is NOT granted to use the content of this blog for the purposes of AI training or similar activity. Consent CANNOT be assumed, it has to be granted.

Donate using Liberapay

ESA BioMass Mission

The mission

Carrying a novel P-band synthetic aperture radar, the Biomass mission is designed to deliver crucial information about the state of our forests and how they are changing, and to further our knowledge of the role forests play in the carbon cycle.
  • 2/5/2025 – The launch was successful.

Launch Date : 29th April 2025

Tags

#ESA,#BioMass,#Mission,#Forest


Mastodon ShellLabs Join Mastodon
AI statement : Consent is NOT granted to use the content of this blog for the purposes of AI training or similar activity. Consent CANNOT be assumed, it has to be granted.

Donate using Liberapay

Exeter Pi jam

The next Exeter Pi jam, takes place on Saturday, 3rd May, Rougemont room, Exeter Library.

Start 10am

Tags

#Event,#Exeter,#RaspberryPiJam,#RaspberryPi,#Coding. #Programming,#Hacking,#Making


Mastodon ShellLabs Join Mastodon
AI statement : Consent is NOT granted to use the content of this blog for the purposes of AI training or similar activity. Consent CANNOT be assumed, it has to be granted.

Donate using Liberapay

Bash scripting 22 – passwords

Having a secure password is really important, but thinking of one can be difficult, you can use three random words such as

CanBookBoard2024%

I have added some numbers and a special symbol here as some websites will insist on more than just upper and lower case letters.

Tbankfully bash has a tool for this

apg - generates several random passwords

You can find more information in the man page

man apg

The example below

apg -m 15 -M SNC

which produces

thuhuHersyoywik2
SindEjpiTrildaf3
Biabodke0skyudha
JeKneapFelu5gDom
neHydishGijcis02
NefNan2JeymDabas

So looking at this

-m 15 – tells apg min password length should be 15 characters -M SNC

Forces apg to use (you need SNC in capitals to force this) * special symbols e.g: %^&* * numerical characters, e.g 1234 * capital letters

Using -M SNC will give the same result.

To change the password on a GNU/Linux system use

passwd

Tags

#Bash,#Bashscripting,#BashScripting#apg,#Security,#Password,#Generation


Mastodon ShellLabs Join Mastodon
AI statement : Consent is NOT granted to use the content of this blog for the purposes of AI training or similar activity. Consent CANNOT be assumed, it has to be granted.

Donate using Liberapay

Tinkerers Meeting – April 2025 – Write up

Today was really productive

It was Myself, Helen and Ben. Ben gave an overview of his CNC project, and brought along the Arduino control unit and display unit. We had a discussion around this and other topics such as Containers, Kubernetes, git and how we can manage projects using git and gitlab, github.

The quality of these is not very good due to lighting, but the first photo shows the control board, the 2nd shows the display from the front.

CNC

CNC

Helen gave a review of what happened when she visited the UK Micromouse and Robotics Society contest in Stratford, and that she will be attending the next meeting in a few weeks, this led to a discussion on hosting a similar event in Torbay.

Helen has also taken the Obstacle avoiding robot, to change the motor as the one current installed in faulty, I had a space so that was taken too. Fingers crossed that next meeting it will behave a little better.

Next Meeting

The next meeting of the Tor bay Tinkerers group will Saturday,Saturday May 24th at 9:30

We meet at:-

STEAM Cafe & Discovery Centre 13 Torbay Road, Paignton.

Related Links

Tags

#Torbay,#Tinkerers


Mastodon ShellLabs Join Mastodon
AI statement : Consent is NOT granted to use the content of this blog for the purposes of AI training or similar activity. Consent CANNOT be assumed, it has to be granted.

Donate using Liberapay

Bash scripting 21 – cron

One of the important things you need to do as a system admin is run tasks that perform specific tasks on the system. This is fine when you are sitting in front of the computer, or logged in remotely, but what happens if you need to run a task when you are not there, or you just want to automate the running of these tasks.

You can schedule tasks using the cron command. There is an easy-to-use tutorial for this at [1]. I am not going to reproduce this here, but may include some of my own specific use cases in a later post.

According to [1] the following

*/5  * * * * /home/paul/crontest.sh

runs every 5 minutes, where as

*/10  * * * * /home/paul/crontest.sh

So putting ls -l in crontest.sh will run that command every 5 or 10 minutes.

However, I am logged into my pi (where I am running this) so the script will run, but I don't see any output.

If I add in ls -l > dirlist.txt

It will produce a directory listing text file when run, by running ls manually you can see it in the listing.

I have a post on Ansiweather scheduled fora few days, in the meantime

putting this in the crontest.sh file

echo $(date), $(ansiweather -l Paignton -a false) >> weather.csv

It will run at the specified time and append the information to weather.csv which could be a good way to monitor weather over time.

This can be examined with

cat weather.csv 

if you use the watch command

watch cat weather.csv

the contents of weather.csv will be displayed and updated in real time as the file is appended with new information.

References

  1. https://stackoverflow.com/questions/12786410/run-cron-job-every-n-minutes-plus-offset

Tags

#Bash,#Bashscripting,#Task,#Schedule,#cron,#crontab,#Admin, #Automation


Mastodon ShellLabs Join Mastodon
AI statement : Consent is NOT granted to use the content of this blog for the purposes of AI training or similar activity. Consent CANNOT be assumed, it has to be granted.

Donate using Liberapay

Bash scripting 20a – weather

Another useful program is ANSI weather. We can use this to get a weather forecast or current weather conditions for a specified location, for example

This can be installed with

sudo apt install ansiweather
ansiweather -l torquay
 Weather in Torquay: 11 °C - UVI: 4.19 - Wind: 5.81 

By default, the output uses ansicolour, and as per man page this can be turned off by using -l

ansiweather -a false

Which is useful., as we have been looking at loops and generating data we can do combine some of this and generate current date / time and then the output of the weather forecast

echo "date" "data" # for reference, not written to file 
for ((i = 0 ; i < 10 ; i++)); do
    echo $(date), $(ansiweather -l Paignton -a false) # echo to screen (stdout)
    echo $(date), $(ansiweather -l Paignton -a false) >> weather.csv
done

#sources of help

#https://stackoverflow.com/questions/40175868/how-to-create-csv-file-using-shell-script

#https://stackoverflow.com/questions/1194882/how-to-generate-random-number-in-bash

#https://linuxhandbook.com/bash-loops/

#https://stackoverflow.com/questions/25242626/bash-redirect-output-to-log-file-with-day-date-in-the-name

Which produces

date data
Sat 19 Apr 20:52:54 BST 2025, Weather in Paignton: 11 °C - UVI: 4.19 - Wind: 5.52 m/s NE - Humidity: 79% - Pressure: 1006 hPa
Sat 19 Apr 20:52:55 BST 2025, Weather in Paignton: 11 °C - UVI: 4.19 - Wind: 5.52 m/s NE - Humidity: 79% - Pressure: 1006 hPa
Sat 19 Apr 20:52:56 BST 2025, Weather in Paignton: 11 °C - UVI: 4.19 - Wind: 5.52 m/s NE - Humidity: 79% - Pressure: 1006 hPa
Sat 19 Apr 20:52:56 BST 2025, Weather in Paignton: 11 °C - UVI: 4.19 - Wind: 5.52 m/s NE - Humidity: 79% - Pressure: 1006 hPa
Sat 19 Apr 20:52:57 BST 2025, Weather in Paignton: 11 °C - UVI: 4.19 - Wind: 5.52 m/s NE - Humidity: 79% - Pressure: 1006 hPa
Sat 19 Apr 20:52:58 BST 2025, Weather in Paignton: 11 °C - UVI: 4.19 - Wind: 5.52 m/s NE - Humidity: 79% - Pressure: 1006 hPa
Sat 19 Apr 20:52:59 BST 2025, Weather in Paignton: 11 °C - UVI: 4.19 - Wind: 5.52 m/s NE - Humidity: 79% - Pressure: 1006 hPa
Sat 19 Apr 20:52:59 BST 2025, Weather in Paignton: 11 °C - UVI: 4.19 - Wind: 5.52 m/s NE - Humidity: 79% - Pressure: 1006 hPa
Sat 19 Apr 20:53:00 BST 2025, Weather in Paignton: 11 °C - UVI: 4.19 - Wind: 5.52 m/s NE - Humidity: 79% - Pressure: 1006 hPa
Sat 19 Apr 20:53:01 BST 2025, Weather in Paignton: 11 °C - UVI: 4.19 - Wind: 5.52 m/s NE - Humidity: 79% - Pressure: 1006 hPa

Which is probably not all that helpful. But it will produce a CSV file of that data.

However, if we put the command to produce output, in a script and then run this script as a cron job, we should be able to generate a weather report on a daily basis for a specific location.

I have made a post (scheduled for later today) on cron and used this as an example of using cron, a script to produce a CSV file of date, time, weather info over time.

This is clearly more useful than what it produces above.

Tags


Mastodon ShellLabs Join Mastodon
AI statement : Consent is NOT granted to use the content of this blog for the purposes of AI training or similar activity. Consent CANNOT be assumed, it has to be granted.

Donate using Liberapay

Matrix Beginner guide

Matrix is “An open network for secure, decentralised communication” We have some rooms on Matrix which you can access via the contact page.

If you are not sure what Matrix is, please check the video below, which gives a beginner introduction.

Other links

Tags

#StemGroup,#Matrix,#Communication.#Protocol,#Element


Mastodon ShellLabs Join Mastodon
AI statement : Consent is NOT granted to use the content of this blog for the purposes of AI training or similar activity. Consent CANNOT be assumed, it has to be granted.

Donate using Liberapay

Bash Scripting 20 – Write date and data a file

So following on from the previous scripts, it may be useful to record both the date and some data associated with that date to a file.

I have found out how to do this, but also tidied my script up, removed some unneeded code (quite a lot) and this should work.

echo "date" "data" # for reference, not written to file 
for ((i = 0 ; i < 10 ; i++)); do
    echo $(date), $((RANDOM % 20)) # echo to screen (stdout)
    echo $(date), $((RANDOM % 20)) > log3.csv
done

Produces

date data
Fri 18 Apr 12:48:16 BST 2025, 0
Fri 18 Apr 12:48:16 BST 2025, 15
Fri 18 Apr 12:48:16 BST 2025, 13
Fri 18 Apr 12:48:16 BST 2025, 6
Fri 18 Apr 12:48:16 BST 2025, 6
Fri 18 Apr 12:48:16 BST 2025, 13
Fri 18 Apr 12:48:16 BST 2025, 18
Fri 18 Apr 12:48:16 BST 2025, 5
Fri 18 Apr 12:48:16 BST 2025, 3
Fri 18 Apr 12:48:16 BST 2025, 14

Which is fine, but we can refine the date output so it is shorter with

date "+DATE: %D TIME: %T"
  date "+%D"   display date no label
  date "+%T"  display tine no label 

If we want this in a csv file it may help to add a comma

date "+DATE: %D, TIME: %T"

Remove the labels DATE: & TIME:

date "+ %D, %T"

Produces

date "+ %D, %T"
 04/20/25, 14:58:19

My final entry, in the script run by cron, at least as far as time and date is concerned

echo $(date "+ %D, %T"), $(ansiweather -l Paignton -a false) >> weather.csv

As this sends the output to weather.csv, If we remove the redirect it will go to stout (standard output) we can just run the following on the terminal 'as is'

echo $(date "+ %D, %T"), $(ansiweather -l Paignton -a false)

This you should end up with

04/29/25, 15:44:14, Weather in Paignton: 16 °C - UVI: 5.91 - Wind: 0.89 m/s NNE - Humidity: 75% - Pressure: 1024 hPa

I have just copied and pasted the line in to the terminal and removed the >> weather.csv as that appends the csv file with the data.

As we all have different use cases, this solution is fine for me, I can adjust to suit further. This is far more useful for a CSV file, especially if you want to load the data in to a spreadsheet.

See fourth link below for full details. * Sources of help

https://stackoverflow.com/questions/40175868/how-to-create-csv-file-using-shell-script
https://stackoverflow.com/questions/1194882/how-to-generate-random-number-in-bash
https://linuxhandbook.com/bash-loops/
https://stackoverflow.com/questions/25242626/bash-redirect-output-to-log-file-with-day-date-in-the-name
https://phoenixnap.com/kb/linux-date-command

Tags

#Bash,#Bashscripting,#BashScripting,#Files,#Write,#Data,#Time,#Data


Mastodon ShellLabs Join Mastodon
AI statement : Consent is NOT granted to use the content of this blog for the purposes of AI training or similar activity. Consent CANNOT be assumed, it has to be granted.

Donate using Liberapay