Skip to content

Instantly share code, notes, and snippets.

@mafredri
Created March 1, 2019 11:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mafredri/d601e5fe26a898fbe6784f48efe4da69 to your computer and use it in GitHub Desktop.
Save mafredri/d601e5fe26a898fbe6784f48efe4da69 to your computer and use it in GitHub Desktop.
Expect script to automate WireGuard upgrade on UniFi Security Gateway
#!/usr/bin/env expect -f
# Description: Expect script to automate WireGuard upgrade on UniFi Security Gateway
# Author: Mathias Fredriksson <mafredri@gmail.com>
# This script assumes non-interactive ssh login.
# Usage upgrade.expect <[user@]usg> <https://github.com/Lochnair/vyatta-wireguard/releases/download/${RELEASE}/wireguard-${BOARD}-${RELEASE}.deb>
set timeout 60
spawn ssh [lindex $argv 0]
expect -exact ":~$ "
send -- "sudo su -\r"
expect -exact ":~# "
send -- "curl -sSL '"
send -- [lindex $argv 1]
send -- "' -o /tmp/wireguard-latest.deb\r"
expect -exact ":~# "
send -- "configure\r"
expect -exact "\[edit\]"
expect -exact "# "
send -- "set interfaces wireguard wg0 route-allowed-ips false\r"
expect -exact "\[edit\]"
expect -exact "# "
send -- "commit\r"
expect -exact "\[edit\]"
expect -exact "# "
# Precautionary measure, in case wg0 is missing so that next step can succeed.
send -- "ip link add dev wg0 type wireguard\r"
expect -exact "\[edit\]"
expect -exact "# "
send -- "delete interfaces wireguard\r"
expect -exact "\[edit\]"
expect -exact "# "
send -- "commit\r"
expect -exact "\[edit\]"
expect -exact "# "
send -- "dpkg -i /tmp/wireguard-latest.deb\r"
expect -exact "# "
send -- "rm /tmp/wireguard-latest.deb\r"
expect -exact "# "
send -- "modprobe -r wireguard\r"
expect -exact "# "
send -- "modprobe wireguard\r"
expect -exact "# "
send -- "load\r"
expect -exact "# "
send -- "commit\r"
expect -exact "# "
# Exit configure.
send -- "exit\r"
expect -exact ":~# "
# Exit sudo.
send -- "exit\r"
expect -exact ":~$ "
# Exit ssh session.
send -- "exit\r"
expect -exact ":~$ "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment