Skip to content

Instantly share code, notes, and snippets.

@unclechu
Last active April 24, 2019 19:30
Show Gist options
  • Save unclechu/a4aade1d704b658518bf9c992ef98317 to your computer and use it in GitHub Desktop.
Save unclechu/a4aade1d704b658518bf9c992ef98317 to your computer and use it in GitHub Desktop.
connect-to-vpns.sh
#!/usr/bin/env bash
# Script for connecting to VPN connections via Network Manager.
# all of these will be connected (`up' command).
# those which you want to be always connected when turn VPNs on.
vpns=(
foo
bar
)
# all of these will be shown in `status' command.
# all of these will be disconnected by `down' command.
# all VPNs which may be connected and which you want be disconnected
# when you turning VPNs off.
all_possible_vpns=(
"${vpns[@]}"
baz
bzz
)
# connect
if (( $# == 0 )) || (
(( $# == 1 )) && [[ $1 == connect || $1 == c || $1 == up || $1 == u ]]
); then
for x in "${vpns[@]}"; do nmcli c u "$x"; done
"$0" s
# disconnect
elif (( $# == 1 )) && [[ $1 == disconnect || $1 == down || $1 == d ]]; then
printf '%s\n' "${all_possible_vpns[@]}" | parallel nmcli c d {}
"$0" s
# reconnect
elif (( $# == 1 )) && [[ $1 == reconnect || $1 == r ]]; then
"$0" d
"$0" c
# show
elif (( $# == 1 )) && [[ $1 == show || $1 == status || $1 == s ]]; then
active_vpns=`nmcli c s --active | awk '{print $1}' | sed 1d`
perl -E '
use Term::ANSIColor qw<:constants>;
BEGIN {
@all_vpns = split "\n", $ARGV[0];
@active_vpns = split "\n", $ARGV[1];
@ARGV=()
}
for (@all_vpns) {
$x = $_;
($c, $s) =
scalar(grep {$_ eq $x} @active_vpns)
? (GREEN, "•")
: (RED, " ");
say BOLD.$c."[$s]".RESET." $x"
}
' -- "$(printf '%s\n' "${all_possible_vpns[@]}")" "$active_vpns"
else
echo $"unknown arguments: \"$@\"" 1>&2
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment