Connect to VPN if server is not available on lan

1

I want to make a connection from my laptop to my server where I have my music library. I used

net use "\\SERVER\Folder\etc\" /SAVECRED /PERSISTENT:yes

to connect to my server. But I also want to be able to connect to the server when I'm not at home, so I've set up a VPN server where I can manually connect to.

Now I'd like to be able to connect to the server without having to check if the VPN is enabled and whether or not I need to use the VPN.

Can I make it so that when I try to connect to my server, it connects to the VPN if I'm not on LAN, otherwise it just connects over LAN without the VPN?

EDIT

For future reference, here's my final solution:

@echo off

ping -n 1 SERVER
if errorlevel 1 (
    rasdial "Server VPN" "username" "supersecretpassword"

    ping -n 1 SERVER
    if errorlevel 1 (
        echo msgbox "Could not establish connection to \\SERVER\" + vbCrLf + vbCrLf + "*sadface*" > %tmp%\tmpmsgbox.vbs
        wscript %tmp%/tmpmsgbox.vbs
        del %tmp%/tmpmsgbox.vbs
    )
)

Jochem Kuijpers

Posted 2014-02-06T21:03:38.260

Reputation: 383

Answers

2

Would this work for you?

ping -n 1 SERVER
if errorlevel 1 rasdial vpn.server.com username password
net use "\\SERVER\Folder\etc" /SAVECRED /PERSISTENT:yes

Ross Presser

Posted 2014-02-06T21:03:38.260

Reputation: 1 139

The net use ... line is a one-time action (/PERSISTENT:yes), but thanks, I'll check if that works as soon as I'm away from my home network! – Jochem Kuijpers – 2014-02-06T23:29:35.743

Thanks, it works (I tested it with another server and another VPN). The only difference is that instead of using the VPN address, you have to use the name of the VPN you've added to the network adapters via the network center in the Control Panel (e.g. resdail "My VPN" "username" "password"). Thanks! – Jochem Kuijpers – 2014-02-06T23:43:08.750