-1

So I have a test server with a vpn which i've created a script to connect to a particular vpn using the cisco vpncli and it works great when I run it manually or call my deployment as its part of it.

How can I programmatically tell if a VPN is connected on my remote server? I assume I can ping a particular address which is only reachable when the vpn is connected via the remote server, How would I easily go about doing this?

As part of my test automation i need to confirm the VPN is connected before I continue with the run, if not I then want to turn it on.

I'm not looking for a copy and paste job here, Im looking for guidance on how I would achieve my task.

Is this the kind of approach I should take or is there something simpler?

symon
  • 670
  • 1
  • 7
  • 20

1 Answers1

2

You should ping the vpn server that is unavailable without vpn connection.

You can ping your VPN server as a common remote server.

Just ping 80 port:

boolean reachable = InetAddress.getByName(hostname).isReachable();

or use:

public static boolean pingHost(String host, int port, int timeout) {
    try (Socket socket = new Socket()) {
        socket.connect(new InetSocketAddress(host, port), timeout);
        return true;
    } catch (IOException e) {
        return false; // Either timeout or unreachable or failed DNS lookup.
    }
}

See here Preferred Java way to ping an HTTP URL for availability

Mike Adamenko
  • 2,944
  • 1
  • 15
  • 28
  • Does it allow OP to ensure he is using VPN? I can ping google for example even if I have VPN connection issues. When I check for example site like whatismyip, it shows my local IP not the VPN server's I thought I was using. – gonczor May 23 '17 at 15:15
  • He needs to ping the vpn server that is unavailable without vpn connection – Mike Adamenko May 23 '17 at 15:35
  • 1
    Hi, sorry I didn't explain too well. The java code is executing on a machine outside of the machine which requires the VPN, so I need to remote into that machine and then ping the vpn server. – symon May 23 '17 at 16:19