You are not logged in.

#1 2017-05-15 17:08:58

n29xm
Member
Registered: 2017-02-11
Posts: 18

script: connect to a random VPN using nmcli (NetworkManager)

A simple script for connecting to a random vpn, given that they're setup with nm. Useful for mapping a key to run the script. It uses notify-send to inform about it, so that's a requirement if you need the messages. Can be improved i know, but anyway here it is.

#!/bin/bash

findactive="$(nmcli c show --active | sed -n '/VPN/I s/\s.*$//p')"
vpnrandom="$(nmcli c show | sed -n '/VPN/I s/\s.*$//p' | sort -R | head -n 1)"
check="$(nmcli c show --active)"

vpnup="nmcli c up $vpnrandom"
vpndwn="nmcli c down $findactive"

# notify-send
upmsg="notify-send 'VPN connection' 'Establishing connection to $vpnrandom...'"
dwnmsg="notify-send 'VPN connection' 'Disconnecting from $findactive...'"

if echo "${check}" | grep -q "vpn"; then
	$(eval $dwnmsg)
	$(eval $vpndwn)
else
	$(eval $upmsg)
	$(eval $vpnup)
fi

Last edited by n29xm (2018-06-13 23:30:38)

Offline

#2 2017-05-15 17:23:51

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,456
Website

Re: script: connect to a random VPN using nmcli (NetworkManager)

Note that grep plus sed is sed.  The following are identical:

findactive="$(nmcli c show --active | grep -i vpn | sed 's/\s.*$//')"
findactive="$(nmcli c show --active | sed '/VPN/I s/\s.*$//')"

But, while I'm not familiar with nmcli, it seems it has a flags field, so rather than trimming the output with sed, just specify which field you want to nmcli directly:

findactive=$(nmcli -f COLNAME con show --active | grep -i vpn)

Also, your "check" variable which greps for vpn is only used when it is also passed through another (case-sensitive ?) grep for vpn.  That serves no purpose.  Just use grep -q initially.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3 2017-05-15 17:40:19

n29xm
Member
Registered: 2017-02-11
Posts: 18

Re: script: connect to a random VPN using nmcli (NetworkManager)

Trilby wrote:

Note that grep plus sed is sed.  The following are identical:

findactive="$(nmcli c show --active | grep -i vpn | sed 's/\s.*$//')"
findactive="$(nmcli c show --active | sed '/VPN/I s/\s.*$//')"

But, while I'm not familiar with nmcli, it seems it has a flags field, so rather than trimming the output with sed, just specify which field you want to nmcli directly:

findactive=$(nmcli -f COLNAME con show --active | grep -i vpn)

Also, your "check" variable which greps for vpn is only used when it is also passed through another (case-sensitive ?) grep for vpn.  That serves no purpose.  Just use grep -q initially.

thanks for the input!.  But you see i need the findactive var to give the exact name of the VPN and nothing else as the output will be used to disable the vpn.
As in:

nmcli c down $findactive

Example:

nmcli c down es.vpnprovider.com.tcp443

The findactive i used outputs

es.vpnprovider.com.tcp443

Your code outputs:

NAME UUID TYPE DEVICE home aed46748-902b-2e76-9c26-ee59e4a9115e 802-11-wireless wifi es.vpnprovider.com.tcp443 tun0 aed46748-902b-2e76-9c26-ee59e4a9115e  tun tun0

And thus is incompatibe with vpndwn command.

Last edited by n29xm (2017-05-15 17:41:23)

Offline

#4 2017-05-15 17:43:47

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,456
Website

Re: script: connect to a random VPN using nmcli (NetworkManager)

Oops - I missed the '-n' flag to sed and a 'p'.  With these, it does the exact same thing:

findactive=$(nmcli c show --active | sed -n '/VPN/I s/\s.*$//p')

"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#5 2017-05-15 17:50:03

n29xm
Member
Registered: 2017-02-11
Posts: 18

Re: script: connect to a random VPN using nmcli (NetworkManager)

Trilby wrote:

Oops - I missed the '-n' flag to sed and a 'p'.  With these, it does the exact same thing:

findactive=$(nmcli c show --active | sed -n '/VPN/I s/\s.*$//p')

Mind breaking that sed command down? i don't get it really. but it works tho, thanks i'll update the code.

Offline

#6 2017-05-15 18:00:54

n29xm
Member
Registered: 2017-02-11
Posts: 18

Re: script: connect to a random VPN using nmcli (NetworkManager)

Trilby wrote:

through another (case-sensitive ?) grep for vpn.  That serves no purpose.  Just use grep -q initially.

you're right. Honestly - i didn't even know what -i was for till i read --help.  Thanks again

edit:

Trilby wrote:

findactive=$(nmcli -f COLNAME con show --active | grep -i vpn)

Outputs nothing, no matter which field is selected. no clue why.

Last edited by n29xm (2017-05-15 18:25:41)

Offline

#7 2017-05-15 18:56:45

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,456
Website

Re: script: connect to a random VPN using nmcli (NetworkManager)

On the second point, I feel a little foolish.  Of course if you don't include the column that has "vpn" in it, grepping for vpn will not be productive - so ignore that suggested revision.  As for the sed command the -n flag means to not print anything by default (normally sed prints every line *and* runs the commands specified on them).  The start of the sed command is then a regex to match /VPN/ so the remainder of the commands only execute if that is matched.  The 'I' makes it a case insenstive match - so this replicates the behavior of `grep -i vpn`.  Then there's the "s" substitution command followed by the "p" print command to print the matched line after the substitution.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#8 2017-05-17 11:12:12

n29xm
Member
Registered: 2017-02-11
Posts: 18

Re: script: connect to a random VPN using nmcli (NetworkManager)

Trilby wrote:

On the second point, I feel a little foolish.  Of course if you don't include the column that has "vpn" in it, grepping for vpn will not be productive - so ignore that suggested revision.  As for the sed command the -n flag means to not print anything by default (normally sed prints every line *and* runs the commands specified on them).  The start of the sed command is then a regex to match /VPN/ so the remainder of the commands only execute if that is matched.  The 'I' makes it a case insenstive match - so this replicates the behavior of `grep -i vpn`.  Then there's the "s" substitution command followed by the "p" print command to print the matched line after the substitution.

nice thanks. makes a bit more sense now.

Offline

Board footer

Powered by FluxBB