# Scripted Network Management with network.sh



## vermaden (Aug 13, 2017)

Hi,

As some of You know there is net-mgmt/networkmgr which allows convenient/graphical Wireless and LAN connections switch.

What I miss in it is the WWAN connection management, DNS management, optional MAC generation and network shares unmount at disconnect. With my solution you still need to edit /etc/wpa_supplicant.conf or /etc/ppp/ppp.conf by hand so it's also not a perfect solution, it's just different.

As I use WWAN, WLAN and LAN connections on my laptop depends on the location I wrote a script to automate this connection management.

It can also set DNS to some safe/nologging providers or even a random safe DNS and generate legitimate MAC address for both LAN and WLAN if needed, even with real OUI first three octets if You provide additional network.sh.oui.txt file with them inside.


```
% network.sh help
USAGE:
  network.sh TYPE [OPTIONS]

TYPES:
  lan
  wlan
  wwan
  dns

OPTIONS:
  start
  start SSID|PROFILE
  stop
  example

EXAMPLES:
  network.sh lan start
  network.sh lan restart
  network.sh wlan start
  network.sh wlan start HOME-NETWORK-SSID
  network.sh wwan example
  network.sh dns onic
  network.sh dns udns
  network.sh dns random
  network.sh doas
  network.sh sudo
```

It display on the screen what command it would run and it makes use of sudo(8) or doas(1) assuming that You are in the network group.

The command `network.sh doas` prints what rights it needs to work without root, same for `network.sh sudo` command, like that:


```
% network.sh doas
  # pw groupmod network -m YOURUSERNAME
  # cat /usr/local/etc/doas.conf
  permit nopass :network as root cmd /bin/cat args /etc/ppp/ppp.conf
  permit nopass :network as root cmd /etc/rc.d/netif args onerestart
  permit nopass :network as root cmd dhclient
  permit nopass :network as root cmd ifconfig
  permit nopass :network as root cmd killall args -9 dhclient
  permit nopass :network as root cmd killall args -9 ppp
  permit nopass :network as root cmd killall args -9 wpa_supplicant
  permit nopass :network as root cmd ppp
  permit nopass :network as root cmd tee args -a /etc/resolv.conf
  permit nopass :network as root cmd tee args /etc/resolv.conf
  permit nopass :network as root cmd umount
  permit nopass :network as root cmd wpa_supplicant
```

The script would upon disconnect would also unmount all network shares.

You will have to change the name from network.sh.txt to network.sh as forums engine does not allow me to upload .sh file directly.

If you intend to use network.sh.oui.txt file, to not rename it 

The settings are on the begin of the file, as follows:

```
# SETTINGS
LAN_IF=em0
LAN_RANDOM_MAC=0
WLAN_IF=wlan0
WLAN_PH=iwn0
WLAN_RANDOM_MAC=1
WWAN_IF=tun0
WWAN_PROFILE=WWAN
NAME=$( basename ${0} )
NETFS="nfs,smbfs,fusefs.sshfs"
TIMEOUT=16
DELAY=0.5
SUDO=0
DOAS=1
```


Comments welcome.

Regards,
vermaden


----------



## jb_fvwm2 (Aug 14, 2017)

Seems useful. [the english one, at least].  Though not so easy here because in my case files in  /boot/loader.conf  also have to be modified to switch from wpa0 to-from eth0...   unless I'm just beginning to get the gist
of rc.d scripts.


----------



## vermaden (Aug 19, 2017)

Added *powersave* option to WLAN config and also MAC generation with first three octets from device (manufacturer OUI) when the file with OUI's is not available which generates real random MAC.


----------



## vermaden (Oct 23, 2017)

An OpenBSD 'variant' by Vincent Delft here:
http://vincentdelft.be/post/post_20171023


----------



## vermaden (Mar 23, 2018)

Added additional check if DNS server is present in /etc/resolv.conf file, if not (rarely happens), set it to the same address as default gateway address.


----------

