1

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\*********\Downloads\Popcorn-Time\Popcorn-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?

1
  • If you can guide in the direction of how to do that, it would definitely be preferable. – Nicomachus Jun 7 '14 at 19:53
1

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.

2

One approach would be to use Zenity. For example:

#!/usr/bin/env bash

if zenity --question --text="What do you choose?"; then
    zenity --info --text="You pressed \"Yes\"!"
else
    zenity --info --text="You pressed \"No\"!"
fi
1

You will probably need to use xmessage

For example: xmessage -center -buttons yes,no Are you connected to the VPN?

Example script:

#!/bin/bash

xmessage -center -buttons yes,no Are you connected to the VPN?

ans="$?"

if [[ "$ans" == 101 ]]; then
        #command to connect to VPN
        notify-send "Connecting"
else
    notify-send "Not Connecting"
fi
3
  • Thanks. I found the xmessage manpages after googling a bit, but I'm not sure how to write the If/Else with that. – Nicomachus Jun 7 '14 at 19:54
  • See the script in the answer above - does that help? – Tim Jun 8 '14 at 14:28
  • No, not exactly. It gives me the syntax though, which is a huge help. I got into a discussion on reddit that's helping me actually query the VPN connection instead of just using a dialog box, so I think I'm going to go that route. But thank you for the help. – Nicomachus Jun 8 '14 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.