2012年2月19日星期日

使用AutoIT编写的常熟理工学院-锐捷VPN登录器

首先要使用IE正常登录一次VPN,目的是安装SSL VPN的浏览器插件,因为这个登录器的目的是隐藏IE窗口进行登录,这样不要一直保持一个打开的浏览器窗口。

代码如下:

#include <ie.au3>
#include <array.au3>
#include <date.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiEdit.au3>
#include <GuiListBox.au3>
#include <base64.au3>
#include <winapi.au3>

Global $oIE,$oFlag
$oFlag=False

Opt("TrayMenuMode", 1)
;Opt("TrayOnEventMode", 1) ;响应托盘事件
Opt("WinTitleMatchMode", 2) ;匹配任意位置字符串

#Region ### START Koda GUI section ### Form=D:\Tools\KodaFormDesigner\Forms\RgVPN.kxf
$Form1 = GUICreate("RGVPN-锐捷VPN登录器", 323, 109, 192, 124)
$Label1 = GUICtrlCreateLabel("用户名", 16, 12, 40, 17)
$txtUsername = GUICtrlCreateInput("", 72, 10, 129, 21)
$Label2 = GUICtrlCreateLabel("密码", 16, 40, 28, 17)
$txtPassword = GUICtrlCreateInput("", 72, 38, 129, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_PASSWORD))
$btnLogin = GUICtrlCreateButton("登录", 224, 8, 73, 25)
$BtnLogout = GUICtrlCreateButton("注销", 224, 36, 73, 25)
$Label3 = GUICtrlCreateLabel("验证码", 16, 72, 40, 17)
$txtVCode = GUICtrlCreateInput("", 72, 70, 57, 21)
$Pic1 = GUICtrlCreatePic("", 136, 72, 60, 20)
$btnRefresh = GUICtrlCreateButton("看不清?", 208, 72, 89, 25)

TraySetIcon("", -1)
TraySetClick(64)
$MenuItem1 = TrayCreateItem("恢复窗口")
$MenuItem2 = TrayCreateItem("退出")
$MenuItem3 = TrayCreateItem("关于")
;TraySetOnEvent($MenuItem1,restore)
;TraySetOnEvent($MenuItem2,quit)
;TraySetOnEvent($MenuItem3,about)
TraySetState()
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

GUICtrlSetState($BtnLogout,$GUI_DISABLE)
GUICtrlSetState($BtnLogin,$GUI_ENABLE)
GUICtrlSetState($BtnRefresh,$GUI_ENABLE)

LoadHomePage()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            if $oFlag=True Then
                Run("cmd /c start close.exe")
                Sleep(1000)
                _IELinkClickByText($oIE,"注    销")
            EndIf
            _IEErrorHandlerDeRegister()
            _IEQuit($oIE)
            Exit

        Case $GUI_EVENT_MINIMIZE
            GUISetState(@SW_HIDE)

        Case $btnRefresh
            _IENavigate($oIE,"
https://61.155.18.8/",1)
            _IELoadWait($oIE)
            Sleep(300)
            $oIE.document.getElementById("overridelink").Click
            _IELoadWait($oIE)
            Sleep(300)
            $oImg = _IEImgGetCollection ($oIE, 8)
            $oPic = $oIE.Document.body.createControlRange()
            $oPic.Add($oImg)
            $oPic.execCommand("Copy")
            $bmp = ClipGet()
            GUICtrlSetImage($pic1,$bmp)

        Case $btnLogin
            GUICtrlSetState($BtnLogout,$GUI_DISABLE)
            GUICtrlSetState($BtnLogin,$GUI_DISABLE)
            GUICtrlSetState($BtnRefresh,$GUI_DISABLE)
            $formUsername=$oIE.document.getElementById("username")
            $formPasswd=$oIE.document.getElementById("passwd")
            $chkCode=$oIE.document.getElementById("chkCode")
            $username=_GUICtrlEdit_GetText($txtUsername)
            $password=_GUICtrlEdit_GetText($txtPassword)
            $vcode=_GUICtrlEdit_GetText($txtVCode)
            if $username<>"" AND $password<>"" AND $vcode<>"" Then
                _IEFormElementSetValue($formUsername,$username)
                _IEFormElementSetValue($formPasswd,$password)
                _IEFormElementSetValue($chkCode,$vcode)
                $oForm=_IEFormGetObjByName($oIE,"Login")
                $oSubmit=_IEFormElementGetObjByName($oForm,"submit")
                _IEAction($oSubmit,"click")
                _IELoadWait($oIE)
                Sleep(300)
                Local $str=$oIE.Document.body.innerHtml
                Local $b=StringRegExp($str,"重新登录",1)
                if @error=0 Then
                    GUICtrlSetState($BtnLogout,$GUI_ENABLE)
                    GUICtrlSetState($BtnRefresh,$GUI_DISABLE)
                    GUICtrlSetState($BtnLogin,$GUI_DISABLE)
                    Sleep(20000)
                    MsgBox(0,"VPN","登录成功")
                    $oFlag=True
                Else
                    MsgBox(0,"VPN","登录失败")
                    GUICtrlSetState($BtnLogout,$GUI_DISABLE)
                    GUICtrlSetState($BtnLogin,$GUI_ENABLE)
                    GUICtrlSetState($BtnRefresh,$GUI_ENABLE)
                EndIf
            Else
                GUICtrlSetState($BtnLogout,$GUI_DISABLE)
                GUICtrlSetState($BtnLogin,$GUI_ENABLE)
                GUICtrlSetState($BtnRefresh,$GUI_ENABLE)
            EndIf

        Case $BtnLogout
            Run("cmd /c start close.exe")
            Sleep(2000)
            _IELinkClickByText($oIE,"注    销")
            _IEErrorHandlerDeRegister()
            ;_IEQuit($oIE)
            Exit

    EndSwitch
    $msg = TrayGetMsg()
    Select
        Case $msg = $MenuItem1
            GUISetState(@SW_SHOWNORMAL)

        Case $msg = $MenuItem2
            quit()

        Case $msg = $MenuItem3
            aboutme()
    EndSelect
WEnd

Func restore()
    GUISetState(@SW_RESTORE)
EndFunc

Func quit()
    if $oFlag=True Then
        Run("cmd /c start close.exe")
        Sleep(2000)
        _IELinkClickByText($oIE,"注    销")
    EndIf
    _IEErrorHandlerDeRegister()
    ;_IEQuit($oIE)
    Exit
EndFunc

Func aboutme()
    MsgBox(0,"锐捷VPN拨号器","常熟理工学院计算机学院 沈健(jimshen@gmail.com,QQ:3262743)版权所有")
EndFunc


Func LoadHomePage()
    _IEErrorHandlerRegister("MyErrFunc")
    $oIE=_IECreate("
https://61.155.18.8/",0,0,1,0)
    _IELoadWait($oIE)
    Sleep(300)
    $oIE.document.getElementById("overridelink").Click
    _IELoadWait($oIE)
    Sleep(300)
    $oImg = _IEImgGetCollection ($oIE, 8)
    $oPic = $oIE.Document.body.createControlRange()
    $oPic.Add($oImg)
    $oPic.execCommand("Copy",False)
    $bmp = ClipGet()
    GUICtrlSetImage($pic1,$bmp)
EndFunc

Func MyErrFunc()
    ; Important: the error object variable MUST be named $oIEErrorHandler
    Local $ErrorScriptline = $oIEErrorHandler.scriptline
    Local $ErrorNumber = $oIEErrorHandler.number
    Local $ErrorNumberHex = Hex($oIEErrorHandler.number, 8)
    Local $ErrorDescription = StringStripWS($oIEErrorHandler.description, 2)
    Local $ErrorWinDescription = StringStripWS($oIEErrorHandler.WinDescription, 2)
    Local $ErrorSource = $oIEErrorHandler.Source
    Local $ErrorHelpFile = $oIEErrorHandler.HelpFile
    Local $ErrorHelpContext = $oIEErrorHandler.HelpContext
    Local $ErrorLastDllError = $oIEErrorHandler.LastDllError
    Local $ErrorOutput = ""
    $ErrorOutput &= "--> COM Error Encountered in " & @ScriptName & @CR
    $ErrorOutput &= "----> $ErrorScriptline = " & $ErrorScriptline & @CR
    $ErrorOutput &= "----> $ErrorNumberHex = " & $ErrorNumberHex & @CR
    $ErrorOutput &= "----> $ErrorNumber = " & $ErrorNumber & @CR
    $ErrorOutput &= "----> $ErrorWinDescription = " & $ErrorWinDescription & @CR
    $ErrorOutput &= "----> $ErrorDescription = " & $ErrorDescription & @CR
    $ErrorOutput &= "----> $ErrorSource = " & $ErrorSource & @CR
    $ErrorOutput &= "----> $ErrorHelpFile = " & $ErrorHelpFile & @CR
    $ErrorOutput &= "----> $ErrorHelpContext = " & $ErrorHelpContext & @CR
    $ErrorOutput &= "----> $ErrorLastDllError = " & $ErrorLastDllError
    ;MsgBox(0, "COM Error", $ErrorOutput)
    SetError(1)
    Return
EndFunc

在运行的过程中IE窗口弹出的消息框,通过另一个进程来自动关闭

#include <IE.au3>

while true
    WinWaitActive("来自网页的消息","")
    If WinActive("来自网页的消息","") Then
        Send("{ENTER}")
        ExitLoop
    endif
wend

while true
    WinWaitActive("Windows Internet Explorer","")
    If WinActive("Windows Internet Explorer","") Then
        Send("{ENTER}")
        ExitLoop
    endif
wend

Exit

没有评论:

发表评论