LoginSignup
11
12

More than 5 years have passed since last update.

WindowsクライアントPCからのVPN(L2TP/IPsec)接続を自動化しました

Last updated at Posted at 2016-01-22

1. はじめに

 SoftLayer上に設定したVPN(L2TP/IPsec)サーバーへ接続するのには、クライアントPC起動後にVPN(L2TP/IPsec)接続操作を行います。
毎日クラウドサーバーへアクセスする場合、この操作も面倒ですのでPC起動後のスタートアップでVPN(L2TP/IPsec)接続できるようにしました。


本投稿で使用している "TIMEPUT"コマンドは、クライアントPCのOSが「VISTA」以降でないと動作しません。
「XP」の場合は、"SLEEP"コマンドをお使いください。
"SLEEP"コマンドは、「Windows Server 2003 Resource Kit Tools」に含まれます。
https://www.microsoft.com/en-us/download/confirmation.aspx?id=17657

2. VPN(L2TP/IPsec)自動接続バッチプログラム

 自動接続には「rasdial」コマンドを使用します。

「VPNConnect.bat」という名でバッチプログラムを作成しました。

VPNConnect.bat
TIMEOUT /T 20
rasdial “VPN-L2TP接続” /disconnect
TIMEOUT /T 10
rasdial "VPN-L2TP接続" UserID Password

(1) TIMEOUT /T 20
Windows起動後、ネットワーク環境が安定するまで20秒待ちます。
(各クライアントPC環境により時間調整してください。)

(2) rasdial “VPN-L2TP接続” /disconnect
VPN(L2TP/UPsec)は未接続ですが二重接続操作を防ぐ目的で、切断操作を実行します。

(3) TIMEOUT /T 10
10秒待ちます。
(各クライアントPC環境により時間調整してください。)

(4) rasdial "VP-L2TP接続" UserID Password
VPN(L2TP/IPsec)を接続します。
・"VPN-L2TP接続"----- クライアントPC側で登録した、VPN接続名
・UserID --------------- VPN(L2TP/IPsec)サーバーで登録したユーザーID
・Password ------------ VPN(L2TP/IPsec)サーバーで登録したパスワード

3. バッチプログラム保存先

クライアントPCの下記スタートアップフォルダーに保存します。

  Cドライブ
   └ユーザー
    └(ユーザー名)
     └AppData
      └Roaming
       └Microsoft
        └Windows
         └スタートメニュー
          └プログラム
           └スタートアップ

4. クラウドファイルサーバーの例

SoftLayer上のWindowsサーバーに、
 ・ファイアーウォール機能
 ・VPN(L2TP/IPsec)機能
 ・ファイルサーバー機能

を1台に集約し、簡易ファイルサーバーを構築。

クライアントPCからWindowsサーバーのパブリックIPにVPN接続して、プライベートネットワークからのみファイルサーバーへアクセスする。

「VPNConnectFS.bat」という名でバッチプログラムを作成し、PC起動後にバッチプログラムにより、VPN接続及びネットワークドライブアサインを行う。

VPNConnectFS.bat
TIMEOUT /T 20
rasdial “VPN-L2TP接続” /disconnect
TIMEOUT /T 10
rasdial "VPN-L2TP接続" UserID Password
net use x: \\xxx.xxx.xxx.xxx\共有フォルダー /user:FSUserID FSUser /persistent:no

net use p: \xxx.xxx.xxx.xxx\共有フォルダー /user:FSUserID FSPassword /persistent:no
・p: ---------------------- ネットワークドライブ
・xxx.xxx.xxx.xxx.xxx ----- Windowsサーバーのプライベートアドレス
・FSUserID -------------- WindowsユーザーID
・FSPassword ----------- Windowsパスワード
・/persistent:no --------- 次回ログオン時に再接続しない。
(/persistent:yesだと、Windows起動後はVPN未接続のため、「ドライブアサインできない」ダイアログが表示される。)

6. おわりに

 rasdialコマンドは使用する機会がなかなかありませんが、毎日接続する必要がある場合、起動運用が楽になります。

11
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
12