Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.