Paul Sutton

Bash

Weather logging

So been experimenting with some of the options for ansiweather, partly as schools need to teach data logging as part of the national curriculum computing, however not all schools have the kit required.

As I have this running on a Raspberry Pi, I thought it may be possible to set one up to log just the location and temperature.

ansiweather -l paignton -p false -h false  -w false -i false >temponly.txt

so the above will display the location and temperature, but exclude pressure, humidity, wind and UV index.

now=$(date +"%m_%d_%Y")
echo "Filename : /nas/backup_$weather.csv"
echo $(date "+ %D, %T"), $(ansiweather -l Paignton -a false) >> weather_$now.csv

Control what is sent to stdout.

now=$(date +"%m_%d_%Y")
echo "Filename : /nas/backup_$weather.csv"
echo $(date "+ %D, %T"), $(ansiweather -l torquay -p false -h false  -w false -i false) >> torquay_$now.csv

Final script

now=$(date +"%m_%d_%Y")
echo "Filename : /nas/backup_$weather.csv"
echo $(date "+ %D, %T"), $(ansiweather -l torquay -p false -h false  -w false -i false) >> torquay_$now.csv

Tags

#Bash,#BashScripting,#Logging,#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

Logging the weather

I have modified the script that runs every 10 minutes to record the output from the ansiweather utility, This still saves the output to a file but adds the current date to the file name.

now=$(date +"%m_%d_%Y")
echo "Filename : /nas/backup_$weather.csv"
echo $(date "+ %D, %T"), $(ansiweather -l Paignton -a false) >> weather_$now.csv

So the output looks like

 Humidity: 71% - Pressure: 1023 hPa
06/09/25, 14:20:01, Weather in Paignton: 18 °C - UVI: 7.03 - Wind: 1.79 m/s WSW - Humidity: 69% - Pressure: 1024 hPa
06/09/25, 14:30:01, Weather in Paignton: 18 °C - UVI: 7.03 - Wind: 2.24 m/s SW - Humidity: 70% - Pressure: 1023 hPa
06/09/25, 14:40:01, Weather in Paignton: 18 °C - UVI: 7.03 - Wind: 3.13 m/s W - Humidity: 69% - Pressure: 1023 hPa
06/09/25, 14:50:01, Weather in Paignton: 18 °C - UVI: 7.03 - Wind: 1.79 m/s SW - Humidity: 69% - Pressure: 1023 hPa
06/09/25, 15:00:01, Weather in Paignton: 18 °C - UVI: 7.03 - Wind: 1.79 m/s WSW - Humidity: 71% - Pressure: 1023 hPa
06/09/25, 15:10:01, Weather in Paignton: 18 °C - UVI: 7.03 - Wind: 1.34 m/s SSW - Humidity: 71% - Pressure: 1024 hPa

As this runs on a headless pi4, I can login and copy the data files locally before uploading to my gopherspace (Look it up)

gopher://vern.cc/1/~zleap

Tags

#Bash,#BashScripting,#Data,#Logging,#Gopher,#RaspberryPi, #Weather


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

ssh – secure shell

Secure shell is a remote login client. The following is from the man page

DESCRIPTION
       ssh (SSH client) is a program for logging into a remote machine and for executing  commands on a remote machine.  It is intended to provide secure encrypted communications between two untrusted hosts over an insecure network.  X11 connections, arbitrary  TCP  ports  and  Unix-domain sockets can also be forwarded over the secure channel.

So as mentioned before, vfsync can't do everything, so if you do need a full Linux system to log in to and install what you want. By this, I am saying you also need to at least have sudo access to that remote system, even if this is a Raspberry Pi.

While being able to log in with

ssh user@host 

Is useful, you still need a password, which is fine. If you want to use some services provided by [vern.cc] then you need to be able to log in with an authentication key. This can be generated with

ssh-keygen
DESCRIPTION
       ssh-keygen generates, manages  and  converts  authentication  keys  for  ssh(1).  ssh-keygen can create keys for use by SSH protocol version 2.

Which will generate a public / private key pair

man ssh-copy-id 

NAME
       ssh-copy-id  —  use locally available keys to authorise logins on a re‐mote machine

DESCRIPTION
       ssh-copy-id  is  a script that uses ssh(1) to log into a remote machine  (presumably using a login password, so password  authentication  should be enabled, unless you've done some clever use of multiple identities).  It  assembles  a  list of one or more fingerprints (as described below) and tries to log in with each key, to see if any of  them  are  already installed (of course, if you are not using ssh-agent(1) this may result  in  you being repeatedly prompted for pass-phrases).  It then assembles a list of those that failed to log in and, using ssh(1), enables logins with those keys on the remote server.  By default it adds the  keys  by appending  them  to  the remote user's ~/.ssh/authorized_keys (creating  the file, and directory, if necessary).  It is also capable of  detecting if the remote system is a NetScreen, and using its ‘set ssh pka-dsa
       key ...’ command instead.

There is some good information here too

The following allowed me to remote login to my Raspberry Pi from my desktop

cd .ssh (I did this so I was in the right place to generate the key)
ssh-keygen
ssh-copy-id -i ~/.ssh/Pi4.pub paul@xxx.xxx.xxx.xxx
ssh paul@xxx.xxx.xxx.xxx

Login with password, and you will be asked for the passphrase the first time you do this. After which, you will be able to just ssh in without the password.

Replace the x's with your IPv4 address.

While using vern, I had to use

ssh-keygen -lf ./key.pub

To address an error, it came up with about keyboard authentication. This worked for me, but should not 'just' be used in every situation.

Graphical ssh

With Linux mint, it is also possible to connect the file manager nemo to a remote server.

File –> Connect to server, then fill in the credentials.

Remote with nemo

With folder, it is probably a good idea to enter the path to your home directory on the remote server, so for a Raspberry Pi with a default username of pi, this is:-

/home/pi

Chat

I am on the Devon and Cornwall Linux user group mailing list and also their matrix channel as zleap, it is better to ask there, that way others can answer too.

Tags

#Bash,#Linux,#ssh,#RemoteAccess,#Security,#SSHKeys,#SecureShell


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 Shortcuts

This question was asked on the Linux Mint Matrix Support recently, I helped by looking up what was needed, and posting this as a reply, As this is also really useful and worth sharing more widely too.

Chat

I am on the Devon and Cornwall Linux user group mailing list and also their matrix channel as zleap, it is better to ask there, that way others can answer too.

Tags

#Bash,#BashScripting,#Bashscripting,#Keyboard,#Shortcuts


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

File integrity

If providing downloadable files, it is important to ensure that the downloaded file is the same as that provided on the host server, this ensures the file has not been tampered with in some way. The end user needs a way to check what they have downloaded is safe.

One way to do this is to create a file checksum with md5sum

let's say we have two files

-rw-rw-r-- 1 psutton psutton  67200 May  3 19:42 jcr.pdf
-rw-rw-r-- 1 psutton psutton 433231 Apr 28 17:50 southwest1.png

We want to create a checksum for both files. To do this we can run

md5sum southwest1.png 
502d4f9dacab9240f2622c032e6ea11f  southwest1.png
md5sum jcr.pdf 
b98edf4dd0afd0ede8ac6dabce66f522  jcr.pdf

This sends the checksum to stdout

So need a file to store this

md5sum jcr.pdf southwest1.png > md5sum2

We can then view the contents if this with

as previously discussed the > directs output to a specified file

cat md5sums
502d4f9dacab9240f2622c032e6ea11f  southwest1.png
b98edf4dd0afd0ede8ac6dabce66f522  jcr.pdf

In this example, we listed the files we want to create a checksum for.

If we were to add a 3rd file say tux.png then we can do this and append the new checksum with

md5sum tux.png >> md5sums

Then example our new md5sums file

cat md5sums
502d4f9dacab9240f2622c032e6ea11f  southwest1.png
b98edf4dd0afd0ede8ac6dabce66f522  jcr.pdf
d52cd5e7164eccd6da50e10d5a9c3dbf  tux.png

So now that we have 3 files and a md5sums file, we can check all the files listed

md5sum -c md5sums
southwest1.png: OK
jcr.pdf: OK
tux.png: OK

Chat

I am on the Devon and Cornwall Linux user group mailing list and also their matrix channel as zleap, it is better to ask there, that way others can answer too.

Tags

#Bash,#BashScripting,#Bashscripting,#md5sums,#md5sum,#file, #integrity,#check


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

Fun bash stuff

While these are not bash commands, there are a few fun tools.

One is fortune, this displays a random quote on the console or terminal.

fortune
Many pages make a thick book, except for pocket Bibles which are on very
very thin paper.

Another is cowsay which takes an argument and prints a picture of a cow (there are options to change how it looks or to other characters)

cowsay hello
 _______
< hello >
 -------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

As with other commands these can be combined

fortune | cowsay
 ________________________________________
/ I don't know half of you half as well  \
| as I should like; and I like less than |
| half of you half as well as you        |
| deserve.                               |
|                                        |
\ -- J. R. R. Tolkien                    /
 ----------------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

Options

cowsay -f tux hello
 _______
< hello >
 -------
   \
    \
        .--.
       |o_o |
       |:_/ |
      //   \ \
     (|     | )
    /'\_   _/`\
    \___)=(___/

cowsay -f daemon hello
 _______
< hello >
 -------
   \         ,        ,
    \       /(        )`
     \      \ \___   / |
            /- _  `-/  '
           (/\/ \ \   /\
           / /   | `    \
           O O   ) /    |
           `-^--'`<     '
          (_.)  _  )   /
           `.___/`    /
             `-----' /
<----.     __ / __   \
<----|====O)))==) \) /====
<----'    `--' `.__,' \
             |        |
              \       /
        ______( (_  / \______
      ,'  ,-----'   |        \
      `--{__________)        \/

Chat

I am on the Devon and Cornwall Linux user group mailing list and also their matrix channel as zleap, it is better to ask there, that way others can answer too.

Tags

#Bash.#BashScripting,#Bashscripting,#Cowsay,#Fortune


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

More security : fail2ban

Fail2ban is a program that will help you with the security of your system.

There is a tutorial for this at Linux Handbook, so I have quoted the description, or part of, below.

This is where a tool like Fail2Ban comes into picture. Fail2Ban is a free and open source software that helps in securing your Linux server against malicious logins. Fail2Ban will ban the IP (for a certain time) if there is a certain number of failed login attempts. [1]

Links

[1] Linux handbook : fail2ban

Chat

I am on the Devon and Cornwall Linux user group mailing list and also their matrix channel as zleap, it is better to ask there, that way others can answer too.

Tags

#Linux,#Bash,#ssh,#Security,#fail2ban


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

Removing users

To remove a user, you can issue the following, as we are no longer setting up a gopher server on the pi, I will just remove the user.

sudo deluser gopher

Which will remove the user, but not that users /home directory, which for the most part is fine if you want to keep hold of the user account data.

For more information and options, consult the man page

man deluser

Chat

I am on the Devon and Cornwall Linux user group mailing list and also their matrix channel as zleap, it is better to ask there, that way others can answer too.

Tags

#Bash,#Bashscripting,#BashScripting#users,#removing


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

Viewing processes

You can view a table that shows currently running processes and information about them.

There are several tools for this:– Both of these update in real time so you can see what is going on

One is top

man top
top

top

The other is htop, which is the same as top, but has a colour output.

man htop
htop

htop

These are really useful tools, there is also ps

man ps
$ ps
    PID TTY          TIME CMD
  51221 pts/0    00:00:00 bash
  51239 pts/0    00:00:00 ps
ps -a
    PID TTY          TIME CMD
  51251 pts/0    00:00:00 ps
ps -u
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
psutton    51221  0.0  0.0  11284  5504 pts/0    Ss   22:51   0:00 bash
psutton    51256  0.0  0.0  13760  4608 pts/0    R+   22:53   0:00 ps -u
ps -x

PID TTY      STAT   TIME COMMAND
   2016 ?        Ss     0:00 /usr/lib/systemd/systemd --user
   2017 ?        S      0:00 (sd-pam)
   2029 ?        S<sl   0:13 /usr/bin/pipewire
   2030 ?        Ssl    0:00 /usr/bin/pipewire -c filter-chain.conf
   2031 ?        S<sl   0:00 /usr/bin/wireplumber
   2033 ?        S<sl   0:21 /usr/bin/pipewire-pulse

Note : Above is NOT full output,

If you are ever unsure as to what a process is, you can try seeing if there is a man page.

NAME
       pipewire - The PipeWire media server

If not, you can look it up using a search tool such as duckduckgo, which from this I found info on wireplumber on the Arch Linux wiki.

Links

Tags

#Bash,#Tools,#Processes,#htop,#top


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