Friday, 3 November 2023
New Raspbian Pi OS 'Bookworm' pip3 mod
pip3 install gives error for new Python 3.11 - can be fixed with command -
sudo mv /usr/lib/python3.11/EXTERNALLY-MANAGED /usr/lib/python3.11/EXTERNALLY-MANAGED.old
Monday, 10 April 2023
Using find with multiple exec flags.
# Find all python files and see if any contain a particular string
find . -name '*.py' -exec echo {} \; -exec grep -e 'usr/bin' {} \;
Thursday, 3 November 2022
SSH public/private keys HOWTO
User Case
---------
On our local Mac machine under the user 'johnny' we want to remote log into a remote machine under the user name 'root'
Without the need for typing in the remote user ('root') password.
On local machine (host:localmachine)- (with username 'johnny') we want to remote ssh to a remote server (host:remoteserver.com) as 'root'
On 'localmachine'
-----------------
ssh-keygen -t rsa -b 4096
# Press enter for defaults and do not bother with a passphrase (just hit enter)
# This generates two files 'id_rsa' and 'id_rsa.pub'
# Rename the private key 'id_rsa' (usually found in ~./.ssh) to something like 'remoteserver_rsa.priv'
Upload id_rsa.pub to the remoteserver (eg. scp .ssh/id_rsa.pub root@remoteserver.com:/root/.ssh)
On remoteserver.com (log in using ssh and the usual password authentication - eg. ssh root@remoteserver.com (use root password))
---------------
# append newly uploaded public key to 'authorized_keys' file
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
#If you desire, you can now remove the 'id_rsa.pub' file on the remote server
rm ~/.ssh/id_rsa.pub
Note: once you cat the 'authorized_keys' file - you will see our new public key (for 'johnny') appended.
# make sure file and directory attributes are correctly set
chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys
# You are now ready to log in from your localmachine without the remote users password credentials.
On 'localmachine' (under the username 'johnny')
-----------------
# We can log into the remoteserver with the command 'ssh REMOTESERVER -l REMOTEUSER -i LOCAL_PRIVATE_KEY_FILE'
# Make sure you use the private key file that is paired with the public key file given and set up on the remote server
# (i.e the public key file appended to the .ssh/authorized_keys file on the remote server)
ssh remoteserver.com -l root -i .ssh/remoteserver_rsa.priv
# Here we are now logging in as the remote user 'root' (The remote linux user - where we placed our public key generated for and by the local user 'johnny')
# we specify the matching private key file - conveniently renamed.
Alternatively we can have an entry in the 'config' file in the local .ssh directory
# Part contents of $HOME/.ssh/config
Host remote mainremote tvserver
HostName remoteserver.com
User root
Port 22
IdentityFile ~/.ssh/remoteserver_rsa.priv
#now we are able to ssh with using just alias names - such as
ssh remote
or
ssh mainremote
or
ssh tvserver
Sunday, 11 September 2022
Recorder.py
import logging
import re
import datetime
import os
from pixels import pixels
import time
class RecordException(Exception):
def __init__(self, msg):
self.msg = msg
def __str__(self):
return f"RecordException {self.msg}"
def dbg(lmsg):
logging.info(lmsg)
def dbgDeprecated(*args, quiet = True):
if not quiet:
for arg in args:
print(arg, end = '')
print("")
def record(basename='', hours = 1, minute_chunks = 15, dry_run = False):
duration_secs = minute_chunks * 60
num_recordings = int((hours * 60)/minute_chunks)
dbg(f"number of recordings : {num_recordings} chunk size: {minute_chunks} mins total: {hours} hrs")
for chunk in range(num_recordings):
format = 'S32_LE'
rate = 16000
file = re.sub('[- :.]','_',str(datetime.datetime.now())) + '.wav'
fullpath = f"/mnt/usb1/recordings/{basename}_{file}"
cmd = f"arecord -q -d {duration_secs} -f {format} -r {rate} {fullpath}"
dbg(f"Recording {chunk} {cmd}")
if not dry_run:
v = os.system(cmd)
dbg(f"Chunk completed {v}")
if (v != 0):
raise RecordException("Record Failure")
dbg("Completed")
logname = '/mnt/usb1/recordings/recordlog.log'
logging.basicConfig(filename=logname,
filemode='a',
format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
datefmt='%H:%M:%S',
level=logging.DEBUG)
dbg("Starting Recording...")
lights = False
if lights:
pixels.wakeup()
try:
record(basename='frontroom', hours = 24*30, minute_chunks = 15, dry_run = False)
except RecordException as e:
dbg(f"Oh No! {e}")
pixels.off()
time.sleep(1)
Thursday, 1 September 2022
Byron Warning System
import machine
import time
from ubitstring import Bits
def wavebit(pin, v, dly):
pin.value(1 if v else 0)
time.sleep_us(dly)
def xmit(data,gpio_pin = 16,delay_us = 160):
pin = machine.Pin(gpio_pin, machine.Pin.OUT)
for b in data:
for bit in range(8):
# wavebit(pin, b & 128 != 0, delay_us)
pin.value(1 if (b & 128 != 0) else 0)
time.sleep_us(delay_us)
b<<=1
byron = "bec3e43e41e79041043079e43e41f0001041e4107df61f61f61f7d86186083efb0fb0f8000c30fb0c3efb0fb079079f61861861f7d87d87c0006187d861f7d87d87d87df60860860f3c83c83c0006187d861f7d87d87c83ef20c30c30fbec3ec3e00030c3ec30fbec3e41e41e7d86186187df61f20f0000820f20c"
bitstring = Bits(hex=(byron*4))
#print(bitstring)
formatted = bitstring.tobytes()
while True:
#print("Transmitting..")
xmit(formatted)
time.sleep(5)
Monday, 29 August 2022
Killing a process (extracting PID with grep)
I can never remember the finer details of grep.
Here I kill a particular python application by using a sequence of piped greps.
The last grep to the right as the '-o' option to extract the PID of our particular Python application
we wish to terminate.
kill -9 $(ps -ax | grep python | grep replay.py | grep -o '^[0-9]*')
Saturday, 13 August 2022
Subscribe to:
Posts (Atom)