Snippets

Bits of code and other things that I find useful, mostly for myself.

Autoreload changes to python files when working in VSCode's interactive Window

%reload_ext autoreload
%autoreload 2

Can also add this to settings.json:

"jupyter.runStartupCommands": [
    "%load_ext autoreload", "%autoreload 2"
],

Start an ad-hoc network in Windows 10

In an administrative command prompt:

netsh wlan set hostednetwork mode=allow ssid=SSID key=KEY
netsh wlan start hostednetwork

May also need to configure TCP/IPv4 for the interface in Network Connections.

Adding your public ssh-key to a remote server

ssh-copy-id -i ~/.ssh/mykey user@host

For more information see here.

Calculate CIDR ranges with an IP address or range excluded

Make this Python 3 script and name it exclude.py (or whatever you fancy):

from ipaddress import ip_network
import sys

all_ips = ip_network('0.0.0.0/0')
arg = ip_network(sys.argv[1])
print(",".join([str(x) for x in list(all_ips.address_exclude(arg))]))

And use like this: python exclude.py 172.29.0.0/16 or python exclude.py 192.168.1.1.

Comparing contents of two directories

python -c 'import filecmp; filecmp.dircmp("DIR_ONE", "DIR_TWO").report()'

For more uses of the filecmp module see PyMOTW.

Mounting LVM volume in Ubuntu Server 18.04

sudo mount /dev/mapper/vg0-lv--0 /mnt

Substitute your volume and mount point as needed.

Start ssh-agent and add key

eval $(ssh-agent -s) && ssh-add ~/.ssh/id_rsa

Short though this may be, I often forget it.