Saturday, April 27, 2024
 Popular · Latest · Hot · Upcoming
1
rated 0 times [  1] [ 0]  / answers: 1 / hits: 2846  / 3 Years ago, wed, july 7, 2021, 1:43:04

I started using Ubuntu alongside Windows about 2 months ago. I've slowly been adding all of the programs that I use regularly to Ubuntu, so that I will eventually be able to do everything in Ubuntu that I could on my Windows machine.



One of the last things I need to migrate over is Popcorn Time. I have finally installed the application on Ubuntu, and I have finally gotten VPNAutoConnect installed as well. However, on Windows, I have one extra precaution set up: a messagebox that pops up when the Popcorn Time icon is clicked, which asks whether the VPN is connected or not. If Yes is clicked, then the application will execute. If No is clicked, it asks whether the user knows what a VPN is and then asks them to either connect it or get off my computer (this was useful because my roommate and girlfriend would sometimes watch movies when i was not home, so this was an extra safeguard to make sure they checked).



I want to make the same messagebox in Ubuntu. Here is the script I used in Windows:



returnvalue=msgbox("Is the VPN connected?",4,"VPN Authentification")

If returnvalue=6 Then

Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run("""C:Users*********DownloadsPopcorn-TimePopcorn-Time.exe""")
Set objShell = Nothing

Else If returnvalue=7 Then

returnvalue=msgbox("Do you know what the VPN is?",4,"VPN Authentification")

If returnvalue=6 Then
x=msgbox("Please connect to the VPN",0,"VPN Authentification")

Else If returnvalue=7 Then
x=msgbox("Please get off my computer",0,"VPN Authentification")

End if

End if

End if

End if


How would I go about creating this same type of script in Ubuntu?


More From » bash

 Answers
5

I answered you on Reddit as well, but this is easier.



The way I'd do it is to check the output of the ifconfig command for the existance of a tun or tap interface. Most likely, one of those will be created when you connect to the VPN.



#!/usr/bin/env bash
if ifconfig -s | grep -qE "(tun|tap)[0-9]+"; then
# Run popcorn time
else
zenity --error --text "VPN is not running."
fi


ifconfig is used to configure network interfaces. If you don't supply any arguments, it will return all the available network interfaces along with a bunch of information about them. The -s flag makes it only output a small list of the interfaces. My ifconfig -s when connected to a VPN looks like this:



➜  ~  ifconfig -s
Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0 1500 0 5302413 0 0 0 1971869 0 0 0 BMRU
lo 65536 0 323789 0 0 0 323789 0 0 0 LRU
tun0 1500 0 0 0 0 0 15 0 0 0 MOPRU


As you can see, on the last line there's an interface called "tun0", which is the interface that my VPN connection uses. Since it's there, I know that I'm connected to my VPN. If you want to learn more about ifconfig, see man ifconfig.



The | (pipe) character "pipes" (sends) the output of the first command into the second command - grep.



The grep command searches through input text for anything that matches a pattern. The -E flag tells it that the pattern is an "extended regex". What that is is a bit too much to explain, but if you're curious, you can read man grep. The pattern I've supplied, (tun|tap)[0-9]+ will search for a string that starts with "tun" or "tap", followed by at least one digit. The -q flag tells grep that I don't want it to output the actual text that it found, I'm just interested to know whether it found something or not.



By putting that in the if statement, the condition will be true if grep finds a matching string, so the code within the then clause will be run. If it fails to find a matching string, the code in the else clause will run.



Lines starting with # are comments - code that is ignored and not run. Since I don't know where Popcorn Time is installed, I don't know how to run it, so you'll have to fill that in yourself.



The first line, starting with #! is called the "shebang". That line simply tells your shell (the thing that you use to run the script with) what program it should use to execute the script. In this case, we probably want it to use bash.






Someone in the comments suggested that you look at the output of the route command, which could be good, but when I tried it it was pretty slow. If you had multiple VPN connections and you wanted to check that a specific one was up, then the route command would be much better (because you could look up a specific IP address), but for the simple use-case, I don't think it's necessary.


[#24756] Wednesday, July 7, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
armis

Total Points: 38
Total Questions: 106
Total Answers: 118

Location: Mali
Member since Sat, Feb 12, 2022
2 Years ago
;