You are not logged in.
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)
fiLast edited by n29xm (2018-06-13 23:30:38)
Offline
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
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 $findactiveExample:
nmcli c down es.vpnprovider.com.tcp443The findactive i used outputs
es.vpnprovider.com.tcp443Your 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 tun0And thus is incompatibe with vpndwn command.
Last edited by n29xm (2017-05-15 17:41:23)
Offline
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
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
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:
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
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
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