放宿舍隔离pppoe路由器的艾泰路由不可建立单独管理WEB认证的帐号,所以写了个脚本管理WEB认证帐号。
添加WEB认证帐号的核心代码
Func addHotSpotUser() ;从路由器上获取帐号数 Dim $webHTML = InetRead(‘http://‘ & $RouterAdminName & ‘:‘ & $RouterAdminPSW & ‘@‘ & $RouterIP & ‘/WebAuthServer.asp‘, 9) If $webHTML = "" Then MsgBox(32, "敬告", "连接路由器超时或者登录路由器用户名密码错误。") Exit Else $webHTML = BinaryToString($webHTML) ;匹配出totalrecs=,即获得帐号数量 $WebAuthNamesCounts = StringRegExp($webHTML, ‘var\stotalrecs\=([^\"]+)\;var‘, 1) If @error <> 0 Then MsgBox(32, "敬告", "正则匹配WebAuthNamesCounts帐号出错,error=" & @error) Exit Else $WebAuthNamesCount = $WebAuthNamesCounts[0] EndIf EndIf ;从日志获取账号数量 Dim $hotspotUserName = 0, $remainingCount = 0, $logAccouts = 0 For $i = 1 To $totalAccount Local $fLine = FileReadLine($LogFile , $i) If @error <> 0 Then MsgBox(32, "敬告", "打开Log文件发生错误." & @error, 10) Exit EndIf Local $fLineDay = StringLeft($fLine, 10) If $fLineDay = @YEAR & "-" & @MON & "-" & @MDAY Then $logAccouts += 1 Else ExitLoop EndIf Next ;如果路由器上获取的帐号数和日志上获取到的记录数不同,很可能是有人直接登录路由器添加了帐号 If $WebAuthNamesCount <> $logAccouts Then MsgBox(32, "敬告", "路由器上的记录数和日志的不同,请联系IT部") Exit Else $remainingCount = 49 - $WebAuthNamesCount ;是加了这次帐号后剩余数 EndIf ;找出最后一个帐号名 Local $aTemp = StringRegExp($webHTML, "(?<=\(\)\;var\stotalrecs\;\n).*(?=\;var\stotalrecs\=)", 1) ;正则匹配 If @error <> 0 And $WebAuthNamesCount = 0 Then ;正则匹配不到表示还没有帐号,则要添加第一个帐号 $lastWebAuthName = 0 ElseIf IsArray($aTemp) Then Local $aListTemp = StringSplit($aTemp[0], ";") ;For $i = 2 to UBound($aListTemp) -1 Step 6 ;全部帐号 ;匹配出最后一个WebAuthName $WebAuthNames = StringRegExp($aListTemp[UBound($aListTemp) - 5], ‘WebAuthNames\[\d*\]\s\=\s\"([^\"]+)\"‘, 1) If @error <> 0 Then MsgBox(32, "敬告", "正则匹配WebAuthNames帐号出错,error=" & @error) Exit Else $lastWebAuthName = $WebAuthNames[0] EndIf Else MsgBox(32, "敬告", "匹配WebAuthNames数组出错") Exit EndIf If StringIsDigit($lastWebAuthName) Then $hotspotUserName = $lastWebAuthName + 1 EndIf If $hotspotUserName = 13 Then $hotspotUserName = 15 EndIf $hotspotUserPSW = Random(100000, 999999, 1) ;用6位随机数做密码 ;先写入日志,否则已经添加账号进路由但写入日志出错麻烦,顺便先检查是否允许写入日志,如果不允许写入日志则退出 _FileWriteLog($LogFile, $UserName & " Add HotSpot Account " & $hotspotUserName & " Password is " & $hotspotUserPSW, 0) If @error <> 0 Then MsgBox(32, "敬告", "打开Log文件发生错误或不可写入。" & @error, 10) Exit EndIf ;连接路由器添加账号 $oHTTP = ObjCreate("Microsoft.xmlhttp") $oHTTP.Open("POST", "http://" & $RouterAdminName & ":" & $RouterAdminPSW & "@" & $RouterIP & "/goform/formWebAuthConfig", False) $oHTTP.setRequestHeader("Cache-Control", "no-cache") $oHTTP.setRequestHeader("Content-Type","application/x-www-form-urlencoded") $oHTTP.setRequestHeader("Referer","http://" & $RouterIP &"/WebAuth_edit.asp") $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关键数据 $oText = BinaryToString($oHTTP.responseBody) ;处理responsetext会乱码,用responseBody才可以 ;Action=add&instIndex=&authname=test10&authnameold=&authpasswd=abc&authip= ;&authtimestart=yyyy-mm-dd&BeginDate=&authtimestop=yyyy-mm-dd&EndDate=&authtimetotal=9.5&remark=9.5%D0%A1%CA%B1 ;ConsoleWrite($oText) If StringInStr($oText, ‘用户名已存在‘) <> 0 Then;已经注册过 _FileWriteToLine($LogFile, 1, "", 1) ;删除上面的添加Log记录 MsgBox(0, "", "发生用户名已存在错误,请重新添加。") ElseIf $oText = "" Then ;返回为空,可以处理为超时了。也有可能是提交的数据有误导致返回为空 MsgBox(0, "", "超时错误。可能已经添加账号到路由器,请联系IT部查看。") Else $hDiff = _DateDiff( ‘h‘,_NowCalc(), _DateAdd(‘d‘, 1, _NowCalcDate()) & " 00:00:00") ;计算今天还剩余多少小时 ;返回账号密码等信息 MsgBox(0, "Fiori WIFI", @CRLF & "登录账号名:" & $hotspotUserName & @CRLF & "登录密码是:" & $hotspotUserPSW & @CRLF & @CRLF & "剩余可上网时间:" & $hDiff & "小时" & @CRLF & @CRLF & "今天剩余可用账号数:" & $remainingCount & @CRLF & "账号当天有效,请遵守相关连网规定。") EndIf EndFunc ;==>addHotSpotUser
每天定时删除全部WEB认证帐号代码
;测试路由器是否连通 ;此处可考虑添加通知给管理员 Dim $testConnect = testConnect($routerIP, $routerPort) If $testConnect = 0 Then MsgBox(32, "出错", "访问不到路由器!", 10) Exit EndIf #-------------------------------------------------------- $ff = InetRead(‘http://‘ & $routerAdminName & ‘:‘ & $routerAdminPSW & ‘@‘ & $routerIP & ‘/goform/formWebAuthListDelAll‘, 9) ;$ff = BinaryToString($ff) ;ConsoleWrite($ff) Exit #----------------------------------------------------------- Func testConnect($ip, $port) TCPStartUp() $Socket = TCPConnect($ip, $port) If $socket <> 1 and $socket <> -1 and $socket <> 2 Then Return 1 Else Return 0 endif TCPCloseSocket($Socket) EndFunc ;==>testConnect
本文出自 “暂时博客” 博客,请务必保留此出处http://gzzhang.blog.51cto.com/5312382/1546932
原文地址:http://gzzhang.blog.51cto.com/5312382/1546932