A-A+

管理艾泰路由器WEB认证帐号的脚本

2015年12月13日 站长资讯 暂无评论

放宿舍隔离pppoe路由器的艾泰路由不可建立单独管理WEB认证的帐号,所以写了个脚本管理WEB认证帐号。

添加WEB认证帐号的核心代码:

Func addHotSpotUser()

;从路由器上获取帐号数

  1. Dim $webHTML = InetRead('http://' & $RouterAdminName & ':' & $RouterAdminPSW & '@' & $RouterIP & '/WebAuthServer.asp', 9)  
  2.    If $webHTML = "" Then  
  3.       MsgBox(32, "敬告", "连接路由器超时或者登录路由器用户名密码错误。")  
  4.       Exit  
  5.    Else  
  6.       $webHTML = BinaryToString($webHTML)  
  7.             ;匹配出totalrecs=,即获得帐号数量  
  8.             $WebAuthNamesCounts = StringRegExp($webHTML, 'var\stotalrecs\=([^\"]+)\;var', 1)  
  9.                       If @error <> 0 Then  
  10.                         MsgBox(32, "敬告", "正则匹配WebAuthNamesCounts帐号出错,error=" & @error)   
  11.                         Exit  
  12.                      Else  
  13.                         $WebAuthNamesCount = $WebAuthNamesCounts[0]  
  14.                      EndIf  
  15.    EndIf  

;从日志获取账号数量

  1. Dim $hotspotUserName = 0, $remainingCount = 0, $logAccouts = 0  
  2.    
  3. For $i = 1 To $totalAccount  
  4. Local $fLine = FileReadLine($LogFile , $i)  
  5.    If @error <> 0 Then  
  6.       MsgBox(32, "敬告", "打开Log文件发生错误." & @error, 10)  
  7.   
  8.   
  9.       Exit  
  10.    EndIf  
  11. Local $fLineDay = StringLeft($fLine, 10)  
  12.    If $fLineDay = @YEAR & "-" & @MON & "-" & @MDAY Then  
  13.       $logAccouts += 1  
  14.    Else  
  15.       ExitLoop  
  16.    EndIf  
  17. Next  

;如果路由器上获取的帐号数和日志上获取到的记录数不同,很可能是有人直接登录路由器添加了帐号

  1. If $WebAuthNamesCount <> $logAccouts Then  
  2.    MsgBox(32, "敬告", "路由器上的记录数和日志的不同,请联系IT部")  
  3.    Exit  
  4. Else  
  5.    $remainingCount = 49 - $WebAuthNamesCount ;是加了这次帐号后剩余数  
  6. EndIf  

;找出最后一个帐号名

  1.  Local $aTemp = StringRegExp($webHTML, "(?<=\(\)\;var\stotalrecs\;\n).*(?=\;var\stotalrecs\=)", 1)   ;正则匹配  
  2.          If @error <> 0 And $WebAuthNamesCount = 0 Then  ;正则匹配不到表示还没有帐号,则要添加第一个帐号   
  3.             $lastWebAuthName = 0  
  4.          ElseIf IsArray($aTemp) Then  
  5.             Local $aListTemp = StringSplit($aTemp[0], ";")  
  6.             ;For $i = 2 to UBound($aListTemp) -1 Step 6     ;全部帐号  
  7.             ;匹配出最后一个WebAuthName  
  8.             $WebAuthNames = StringRegExp($aListTemp[UBound($aListTemp) - 5], 'WebAuthNames\[\d*\]\s\=\s\"([^\"]+)\"', 1)  
  9.                       If @error <> 0 Then  
  10.                         MsgBox(32, "敬告", "正则匹配WebAuthNames帐号出错,error=" & @error)   
  11.                         Exit  
  12.                      Else  
  13.                         $lastWebAuthName = $WebAuthNames[0]  
  14.                      EndIf  
  15.          Else  
  16.             MsgBox(32, "敬告", "匹配WebAuthNames数组出错")  
  17.             Exit  
  18.          EndIf  
  19.    
  20. If StringIsDigit($lastWebAuthName) Then  
  21.    $hotspotUserName = $lastWebAuthName + 1   
  22. EndIf  
  23. If $hotspotUserName = 13 Then  
  24.    $hotspotUserName = 15  
  25. EndIf  
  26.    
  27. $hotspotUserPSW = Random(100000, 999999, 1) ;用6位随机数做密码  

;先写入日志,否则已经添加账号进路由但写入日志出错麻烦,顺便先检查是否允许写入日志,如果不允许写入日志则退出

  1. _FileWriteLog($LogFile, $UserName & " Add HotSpot Account " & $hotspotUserName & " Password is " & $hotspotUserPSW, 0)  
  2.    If @error <> 0 Then  
  3.       MsgBox(32, "敬告", "打开Log文件发生错误或不可写入。" & @error, 10)  
  4.       Exit  
  5.    EndIf  

;连接路由器添加账号

  1. $oHTTP = ObjCreate("Microsoft.xmlhttp")  
  2. $oHTTP.Open("POST", "http://" & $RouterAdminName & ":" & $RouterAdminPSW & "@" & $RouterIP & "/goform/formWebAuthConfig", False)  
  3. $oHTTP.setRequestHeader("Cache-Control", "no-cache")  
  4. $oHTTP.setRequestHeader("Content-Type","application/x-www-form-urlencoded")  
  5. $oHTTP.setRequestHeader("Referer","http://" & $RouterIP &"/WebAuth_edit.asp")  
  6. $oHTTP.Send("Action=add&instIndex=&authname=" & $hotspotUserName & "&authnameold=&authpasswd=" & $hotspotUserPSW & "&authip=&authtimestart=yyyy-mm-dd&BeginDate=&authtimestop=yyyy-mm-dd&EndDate=&authtimetotal=0.0&remark=");post关键数据  
  7. $oText = BinaryToString($oHTTP.responseBody) ;处理responsetext会乱码,用responseBody才可以  
  8.   
  9. ;Action=add&instIndex=&authname=test10&authnameold=&authpasswd=abc&authip=  
  10. ;&authtimestart=yyyy-mm-dd&BeginDate=&authtimestop=yyyy-mm-dd&EndDate=&authtimetotal=9.5&remark=9.5%D0%A1%CA%B1  
  11. ;ConsoleWrite($oText)  
  12.    
  13. If StringInStr($oText, '用户名已存在') <> 0 Then;已经注册过  
  14.    _FileWriteToLine($LogFile, 1, "", 1)   ;删除上面的添加Log记录  
  15.    MsgBox(0, "", "发生用户名已存在错误,请重新添加。")  
  16. ElseIf $oText = "" Then ;返回为空,可以处理为超时了。也有可能是提交的数据有误导致返回为空  
  17.    MsgBox(0, "", "超时错误。可能已经添加账号到路由器,请联系IT部查看。")  
  18. Else  
  19. $hDiff = _DateDiff( 'h',_NowCalc(),  _DateAdd('d', 1, _NowCalcDate()) & " 00:00:00")  ;计算今天还剩余多少小时  
  20. ;返回账号密码等信息  
  21. MsgBox(0, "Fiori WIFI",  @CRLF & "登录账号名:" & $hotspotUserName & @CRLF & "登录密码是:" & $hotspotUserPSW & @CRLF & @CRLF & "剩余可上网时间:" & $hDiff & "小时" & @CRLF & @CRLF & "今天剩余可用账号数:" & $remainingCount & @CRLF & "账号当天有效,请遵守相关连网规定。")   
  22. EndIf  
  23. EndFunc ;==>addHotSpotUser  

每天定时删除全部WEB认证帐号代码:

;测试路由器是否连通

;此处可考虑添加通知给管理员

  1. Dim $testConnecttestConnect = testConnect($routerIP, $routerPort)  
  2. If $testConnect = 0 Then  
  3.    MsgBox(32, "出错", "访问不到路由器!", 10)  
  4.    Exit  
  5. EndIf  
  6. #--------------------------------------------------------  
  7.    
  8. $ff = InetRead('http://' & $routerAdminName & ':' & $routerAdminPSW & '@' & $routerIP & '/goform/formWebAuthListDelAll', 9)  
  9.    
  10. ;$ff = BinaryToString($ff)  
  11. ;ConsoleWrite($ff)  
  12. Exit  
  13.    
  14. #-----------------------------------------------------------  
  15. Func testConnect($ip, $port)  
  16.    
  17. TCPStartUp()  
  18.    
  19. $Socket = TCPConnect($ip, $port)  
  20.    
  21.    If $socket <> 1 and $socket <> -1 and $socket <> 2 Then  
  22.       Return 1  
  23.    Else  
  24.       Return 0   
  25.    endif  
  26.    
  27. TCPCloseSocket($Socket)  
  28.    
  29. EndFunc ;==>testConnect  
标签:

给我留言