0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[VBScript × AnyConnect]VBScriptを使ってAnyConnectを自動接続する

Last updated at Posted at 2021-05-20

#備忘メモ:VBScriptを使ってAnyConnect自動接続

説明

AnyConnectで会社NWに接続する際、ワンタイムパスワードの取得と入力が煩わしいので、手動操作の一部(①~②)を自動化する。
①ワンタイムパスワード取得
②AnyConnectに接続先/ユーザアカウント/ワンタイムパスワードを入力してVPN接続
③VMware Horizon Clientに接続先/ユーザアカウント/パスワードを入力して会社クライアント接続

##動作確認環境
Internet Explorer 11.00.19041.1
Cisco AnyConnect Secure Mobility Client Version 4.6.03049
VMware Horizon Client バージョン 5.5.1 build-17575367

##コード

autoConnect.vbs
'動作確認環境
'Cisco AnyConnect Secure Mobility Client
'バージョン : 4.6.03049
'VMware Horizon Client
'バージョン : 5.5.1 build-17575367

'このファイルの置き場所
'%userprofile%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

Option Explicit

Dim objShell : Set objShell = WScript.CreateObject("WScript.Shell")
Dim strCmdLine

Dim blConn : blConn = False
Dim strFile : strFile = objShell.ExpandEnvironmentStrings("%temp%") & "\nwstat.txt"
Dim strLine

Dim objSWbemLocator : Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator") 
Dim objSWbemService : Set objSWbemService = objSWbemLocator.ConnectServer

Dim strUrl : strUrl = "https://www.example.com/passlogic&id="
Dim myloginname : myloginname = "myname@hoge.local"
Dim myvpnhost : myvpnhost = "vpn.example.com"
Dim myfile : myfile = objShell.ExpandEnvironmentStrings("%temp%") & "\myfile.txt"

Dim objIE : Set objIE = CreateObject("InternetExplorer.Application")
Dim i
Dim mypassword

Dim objFso : Set objFso = CreateObject("Scripting.FileSystemObject")
Dim objFile

logging("autoConnect開始")
strCmdLine = "powershell get-content " & objShell.ExpandEnvironmentStrings("%temp%") & "\autoConnect.log -wait -tail 1"
objShell.Run strCmdLine, 1, False

'VMware Horizon ClientをKILL
strCmdLine = "cmd /c taskkill -f -im vmware-view.exe"
objShell.Run strCmdLine, 0, True
Do While objSWbemService.ExecQuery("Select * From Win32_Process Where Caption='vmware-view.exe'").Count > 0
	WScript.Sleep 100
Loop 

'CLIコマンドプロンプトをKILL
strCmdLine = "cmd /c taskkill -f -im vpncli.exe"
objShell.Run strCmdLine, 0, True
Do While objSWbemService.ExecQuery("Select * From Win32_Process Where Caption='vpncli.exe'").Count > 0
	WScript.Sleep 100
Loop 

'DISCONNECT
strCmdLine = "cmd /c """ & objShell.ExpandEnvironmentStrings("%programfiles(x86)%") & "\Cisco\Cisco AnyConnect Secure Mobility Client\vpncli.exe"" disconnect"
objShell.Run strCmdLine, 0, True

'AnyConnectをKILL
strCmdLine = "cmd /c taskkill -f -im vpnui.exe"
objShell.Run strCmdLine, 0, True
Do While objSWbemService.ExecQuery("Select * From Win32_Process Where Caption='vpnui.exe'").Count > 0
	WScript.Sleep 100
Loop 

Do
	'NW接続状況確認
	strCmdLine = "cmd /c netsh mbn sho interfaces > " & strFile			'携帯電話回線の場合
'	strCmdLine = "cmd /c ping -n 1 8.8.8.8 > " & strFile				'インターネット回線の場合
	objShell.Run strCmdLine, 0, True

	logging("NW接続確認")
	logging("+-------------------")
	Set objFile = objFso.OpenTextFile(strFile)
	Do While objFile.AtEndOfStream <> True
		strLine = objFile.ReadLine

		logging("| " & strLine)

		'NW接続完了していたら
		If InStr(strLine, "状態              : 接続済み") <> 0 Then		'携帯電話回線の場合
'		If InStr(strLine, "8.8.8.8 からの応答: バイト数 =") <> 0 Then	'インターネット回線の場合
			blConn = True
			logging("+-------------------")
			logging("NW接続済み")
			Exit Do
		End If
	Loop

	If blConn Then Exit Do
	logging("+-------------------")
	logging("NW未接続")

	Wscript.Sleep 1000

Loop While blConn = False

'PassLogicサイトに接続
'非表示のままでも動くからコメントアウト
'objIE.Visible = True
'objShell.AppActivate "Internet Explorer"
objIE.Navigate2 strUrl & myloginname
Do While objIE.Busy = True Or objIE.readyState <> 4
	WScript.Sleep 100
Loop

logging("PassLogicサイト接続完了")

'認証パターンの1~8文字目をパスワードとして取得
For i = 0 To 7
	mypassword = mypassword & objIE.document.getElementsByClassName("cell").Item(i).innerText
Next
objIE.Quit

logging("パスコード取得完了:" & mypassword)

'接続情報をmyfileに書出し
objFso.CreateTextFile(myfile)
Set objFile = objFso.OpenTextFile(myfile, 8, True)
objFile.WriteLine("connect " & myvpnhost)
objFile.WriteLine(myloginname)
objFile.WriteLine(mypassword)
objFile.Close

logging("接続開始")

'CLIコマンドプロンプトにmyfileをリダイレクトして実行
strCmdLine = "cmd /c """"" & objShell.ExpandEnvironmentStrings("%programfiles(x86)%") & "\Cisco\Cisco AnyConnect Secure Mobility Client\vpncli.exe"" -s < """ & myfile & """"""
objShell.Run strCmdLine, 1, True

logging("接続完了")

'DISCONNECT操作用にAnyConnectを実行して常駐させておく
strCmdLine = "cmd /c """ & objShell.ExpandEnvironmentStrings("%programfiles(x86)%") & "\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe"""
objShell.Run strCmdLine, 0, False
logging("AnyConnect常駐実行")

'VMware Horizon Clientを実行
strCmdLine = "cmd /c """ & objShell.ExpandEnvironmentStrings("%programfiles(x86)%") & "\VMware\VMware Horizon View Client\vmware-view.exe"""
objShell.Run strCmdLine, 0, False
logging("VMware Horizon Client起動実行")

logging("autoConnect終了")



'---------------------------------------
'参考に書き残しておく
'AnyConnectで接続する場合、Sleep関数で入力タイミングを制御しているため動作精度が低い
'strCmdLine = "cmd /c """ & objShell.ExpandEnvironmentStrings("%programfiles(x86)%") & "\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe"""
'objShell.Run strCmdLine, 0
'
'WScript.Sleep 1000
'
'objShell.SendKeys "{TAB}"
'objShell.SendKeys "{TAB}"
'objShell.SendKeys "{ENTER}"
'
'WScript.Sleep 10000
'
'objShell.SendKeys mypassword
'objShell.SendKeys "{TAB}"
'objShell.SendKeys "{ENTER}"
'
'WScript.Sleep 5000
'
'strCmdLine = "cmd /c C:\comware_work\vmware-run.bat"
'objShell.Run strCmdLine, 0, Flase
'---------------------------------------

Sub logging(str)
	Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
	Dim f
	Dim objShell : Set objShell = WScript.CreateObject("WScript.Shell")

	'ログファイルを開き、存在しない場合には作成する
	Set f = fso.OpenTextFile(objShell.ExpandEnvironmentStrings("%temp%") & "\autoConnect.log", 8, True)

	'ログを書き込む
	f.WriteLine(Date() & " " & Time() & " " & str)
	Set f = Nothing
End Sub

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?