Scientific Computing

View tree of USB devices on Windows/Linux

Microsoft USBView allows viewing connected USB devices on Windows in a tree view. This allows viewing which ports devices are plugged in to. USBView shows the name of chipsets and devices, even their serial numbers. An example use is computers in remote locations: verify that equipment is plugged in via USBView.

USBview

Linux USB View is available on GitHub. Follow the “INSTALL” file directions.

Send email via Gmail from Python

Note: This test should only be used with a new throwaway Gmail account as it risks security of the Gmail account in use. Instead consider Oauth with Gmail.


This is a complete example of SMTP sending email via Gmail from Python. To use with two-factor authentication account requires a Gmail App Password.

You need to use Oauth instead of this method for real-world systems, this is just a simple didactic example.

"""
send text string (e.g. status) via Gmail SMTP
"""
import smtplib
from email.mime.text import MIMEText
from getpass import getpass

def sender(user:str, passw:str, to:list, textmsg:str, server:str):
    """
    this is not a good way to do things.
    Should use Oauth.
    """
    with smtplib.SMTP_SSL('smtp.gmail.com') as s:
        s.login(user, passw)

        msg = MIMEText(textmsg)

        msg['Subject']= 'System status update'
        msg['From']= user
        msg['To'] = ', '.join(to)

        s.sendmail(user,to, msg.as_string())
        s.quit()

if __name__ == '__main__':
    from argparse import ArgumentParser
    p = ArgumentParser()
    p.add_argument('user',help='Gmail username')
    p.add_argument('to',help='email address(es) to send to', nargs='+')
    p.add_argument('-s','--server',help='SMTP server',default='smtp.gmail.com')
    p = p.parse_args()

    testmsg="just testing email from Python setup"

    sender(p.user+'@gmail.com',
           getpass('gmail password: '),
           p.to,
           testmsg,
           p.server)

Get Public IP address from Shell or Python

Both the shell and Python methods get public IPv4 and IPv6 addresses. This is good for verifying the computer IP is in the organization’s IP address range. These scripts use the “reflector” method, which can be more reliable on complex networks.

Shell: specify the network interface with curl --interface eth0 option.

Bash shell script “getIP.sh”

url=('https://ident.me' 'https://api.ipify.org')

for u in ${url[@]}; do
  curl -6 -s -m 2 $u && break
done

for u in ${url[@]}; do
  curl -4 -s -m 2 $u && break
done

Python script getIP.py

#!/usr/bin/env python
"""
gets interface IPv4 and IPv6 public addresses using libCURL
This uses the "reflector" method, which seems more reliable for finding public-facing IP addresses,
WITH THE CAVEAT that man-in-the-middle, etc. attacks can defeat the reflector method.
"""
from ipaddress import ip_address
import pycurl
from io import BytesIO

urls = ['https://ident.me', # ipv6 and ipv4
        'https://api.ipify.org'] # ipv4 only
length=45
# https://stackoverflow.com/questions/166132/maximum-length-of-the-textual-representation-of-an-ipv6-address

def getip(interface=None):
    for url in urls:
        addr = []
        for ipv in (pycurl.IPRESOLVE_V4,pycurl.IPRESOLVE_V6):
            buffer = BytesIO() # must clear like this
            C = pycurl.Curl()
            if interface:
                C.setopt(pycurl.INTERFACE,interface)
            C.setopt(C.URL, url)
            C.setopt(pycurl.IPRESOLVE, ipv)
            C.setopt(C.WRITEDATA, buffer)
            try:
                C.perform()
                result = buffer.getvalue()
                try: #validate response
                    addr.append(ip_address(result.decode('utf8')))
                except ValueError:
                    pass
            except pycurl.error:
                pass
            finally:
                C.close()

        if len(addr)>1: #IPv4 and IPv6 found
            break

    return addr

if __name__ == '__main__':
    import signal
    signal.signal(signal.SIGINT, signal.SIG_DFL)

    from argparse import ArgumentParser
    p = ArgumentParser()
    p.add_argument('-i','--iface',help='network interface to use')
    p = p.parse_args()

    addr = getip(p.iface)
    print(addr)

Low priority Python execution

A data acquisition system typically runs multiple programs simultaneously:

  1. High-priority data collection - a program driving high data bandwidth acquisition
  2. Low-priority monitoring tools - check system status or process data
  3. Low-priority services - web servers displaying processed results

Properly prioritizing these processes ensures the critical data collection isn’t interrupted by secondary tasks.

Suppose a Python script “preview.py” is monitoring a camera’s status and needs to run at low priority.

On Unix-like systems (Linux, macOS, BSD, …) use nice to control process priority.

nice -n 19 python preview.py
nice -n
run task(s) with priority, where bigger positive numbers are lower priority, and more negative numbers are higher priority.

On Windows the start command can control program run priority.

start /low python preview.py
start /low
runs a program and its child processes at low priority.

Repairing broken symbolic link

A symbolic link may become broken due to updating/compiling/installing software, showing up as red text in ls Terminal.

Suppose the broken link is named “foo.so”: Find where link pointed to:

readlink -v foo.so

Point link to new file (perhaps foo2.so)

ln -sfn foo2.so foo.so

Verify new link is blue color

ls foo.so

Linux loopback audio record

Easily record the sounds heard through system speakers on Linux with Pulseaudio and Audacity.

apt install audacity pavucontrol

To record “what you hear” in Audacity: “click to monitor” (right of microphone in main Audacity screen). Start Pulseaudio advanced configuration tool

pavucontrol

Under pavucontrol Recording tab input, select “Monitor of Built-in XXX” where XXX is the playback device you wish to monitor. This loopback audio from pavucontrol allows recording what you hear in Linux using Audacity and PulseAudio.

PulseAudio loopback

List comments/annotations in PDF

On Windows, Adobe Reader can export and view a list of comments through the Comment tab on the right side. On Linux, in Evince PDF viewer, the side pane has a drop-down annotations menu that lists comments. Press F9 key if you don’t see the left-hand panel.

One can extract all comments and annotations from a PDF file by

apt install texlive-latex-extra

pdfannotextractor --install

pdfannotextractor in.pdf

This yields a .pax file without plain text. However, it was not clear how to read this file.

Open same PDF multiple times in Evince

It’s often useful to open the same PDF file multiple times, particularly when reviewing journal article drafts to view figures simultaneously with text describing the figure.

From Evince (popular Linux PDF viewer), Menu select “Open a Copy”. Open as many copies of the same Postscript or PDF document as desired.

Restarting Unity desktop in Ubuntu

This is for Ubuntu < 18.04. Ubuntu ≥ 18.04 no longer use Unity.


If the desktop seems to freeze for more than a few seconds, try

  1. press together the keys: Ctrl Alt F1
  2. login and type
unity & disown

This leaves desktop/apps all as they were while fixing the broken state.

Full desktop reset

Restart the whole Unity desktop (which is like unto logging out and logging in, discarding the current desktop):

service lightdm restart

Jekyll Markdown syntax highlighting

Jekyll uses Markdown or HTML files to statically render complex websites from simple text files. While Jekyll syntax highlighting is possible, more often we might use more generic Markdown commands.

Enable syntax highlighting: Before every code block, simply include the language name, for example

```fortran

this even works for gdb.

The color code syntax highlighting on your webpage looks quite striking, and the number of languages covered is very extensive. Despite the different syntax highlighter modules used by different Jekyll web hosting services, almost every language works with the method above.

Github uses Linguist syntax highlighting for its own service (Issues, README.md, etc.) Github/Gitlab Pages can use Rouge syntax highlighting via Jekyll _config.yml – specify an allowed syntax highlighter.