I have a VBScript that downloads all of my router traffic from the OpenDNS website. I tweaked it just a bit so that I could store account information and my category tracking preferences in an INI file.
This was working just fine, but I really wanted to make it adaptable for different users, and so that account information and user preferences could be changed easily without opening the INI file.
I came up with a VB.Net GUI that would allow the storage of the information in the My.Settings of VB.Net. I could just call the VBScript from VB.Net, but would really like to convert the entire thing to .Net.
I attempted to do this, and got 90% of it converted without errors. But it does not work, and I am not sure if it is because of the few errors, or because my conversion was (how shall I say... RUBBISH!). :)
The VBScript is called with this command from the CMD prompt (or another VBScript)... - cscript /NoLogo fetchstats.vbs <username> <network-id> <YYYY-MM-DD> [<YYYY-MM-DD>]
This is the VBScript that does all of the work... - Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
-
-
Function GetUrlData(strUrl, strMethod, strData)
-
objHTTP.Open strMethod, strUrl
-
If strMethod = "POST" Then
-
objHTTP.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
-
End If
-
objHTTP.Option(6) = False
-
objHTTP.Send(strData)
-
-
If Err.Number <> 0 Then
-
GetUrlData = "ERROR: " & Err.Description & vbCrLf & Err.Source & " (" & Err.Nmber & ")"
-
Else
-
GetUrlData = objHTTP.ResponseText
-
End If
-
End Function
-
-
URL="https://dashboard.opendns.com"
-
-
-
If Wscript.Arguments.Count < 3 Or Wscript.Arguments.Count > 4 Then
-
Usage
-
End If
-
-
Username = Wscript.Arguments.Item(0)
-
If Len(Username) = 0 Then: Usage
-
Network = Wscript.Arguments.Item(1)
-
If Len(Network) = 0 Then: Usage
-
-
Set regDate = new RegExp
-
regDate.Pattern = "^\d{4}-\d{2}-\d{2}$"
-
-
DateRange = Wscript.Arguments.Item(2)
-
If Not regDate.Test(DateRange) Then: Usage
-
-
If Wscript.Arguments.Count = 4 Then
-
ToDate = Wscript.Arguments.Item(3)
-
If Not regDate.Test(toDate) Then: Usage
-
DateRange = DateRange & "to" & ToDate
-
End If
-
-
-
' Are they running Vista or 7?
-
On Error Resume Next
-
Set objPassword = CreateObject("ScriptPW.Password")
-
If strP="" Then strP=BASE64SHA1(InputBox("Please type your password."))
-
Password = strP
-
-
Wscript.StdErr.Write vbCrLf
-
On Error GoTo 0
-
-
Set regEx = New RegExp
-
regEx.IgnoreCase = true
-
regEx.Pattern = ".*name=""formtoken"" value=""([0-9a-f]*)"".*"
-
-
data = GetUrlData(URL & "/signin", "GET", "")
-
-
Set Matches = regEx.Execute(data)
-
token = Matches(0).SubMatches(0)
-
-
data = GetUrlData(URL & "/signin", "POST", "formtoken=" & token & "&username=" & Escape(Username) & "&password=" & Escape(Password) & "&sign_in_submit=foo")
-
If Len(data) <> 0 Then
-
Wscript.StdErr.Write "Login Failed. Check username and password" & vbCrLf
-
WScript.Quit 1
-
End If
-
-
page=1
-
Do While True
-
data = GetUrlData(URL & "/stats/" & Network & "/topdomains/" & DateRange & "/page" & page & ".csv", "GET", "")
-
If page = 1 Then
-
If LenB(data) = 0 Then
-
WScript.StdErr.Write "You can not access " & Network & vbCrLf
-
WScript.Quit 2
-
ElseIf InStr(data, "<!DOCTYPE") Then
-
Wscript.StdErr.Write "Error retrieving data. Date range may be outside of available data."
-
Wscript.Quit 2
-
End If
-
Else
-
' First line is always header
-
data=Mid(data,InStr(data,vbLf)+1)
-
End If
-
If LenB(Trim(data)) = 0 Then
-
Exit Do
-
End If
-
Wscript.StdOut.Write data
-
page = page + 1
-
Loop
And... Here is my attempt at converting the VBScript code into VB.Net code. I was planning on calling this from a button on the application main form. On click, it would send the Username, Network, Begin Date, End Date, and Password. - Imports System.Text.RegularExpressions
-
-
Module GetStats
-
-
Dim CurPath = Application.StartupPath & "\"
-
Dim strK = "#$UnEqu1v0cal!?"
-
Dim objHTTP As Object = CreateObject("WinHttp.WinHttpRequest.5.1")
-
Dim URL = "https://dashboard.opendns.com"
-
Dim data as string
-
-
Function GetUrlData(ByVal strUrl, ByVal strMethod, ByVal strData)
-
objHTTP.Open(strMethod, strUrl)
-
If strMethod = "POST" Then
-
objHTTP.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
-
End If
-
objHTTP.Option(6) = False
-
objHTTP.Send(strData)
-
-
If Err.Number <> 0 Then
-
GetUrlData = "ERROR: " & Err.Description & vbCrLf & Err.Source & " (" & Err.Number & ")"
-
Else
-
GetUrlData = objHTTP.ResponseText
-
End If
-
End Function
-
-
Public Sub Process(ByVal strUser As String, ByVal strNetwork As String, ByVal strBDate As String, ByVal strEDate As String, ByVal strPW As String)
-
Dim DateRange As String
-
Dim page As Integer
-
Dim regEx As String
-
-
DateRange = strBDate & "to" & strEDate
-
-
regEx = New Regex
-
regEx.IgnoreCase = True
-
regEx.Pattern = ".*name=""formtoken"" value=""([0-9a-f]*)"".*"
-
-
data = GetUrlData(URL & "/signin", "GET", "")
-
-
Matches = regEx.Execute(data)
-
token = Matches(0).SubMatches(0)
-
-
data = GetUrlData(URL & "/signin", "POST", "formtoken=" & token & "&username=" & Escape(strUser) & "&password=" & Escape(strPW) & "&sign_in_submit=foo")
-
If Len(data) <> 0 Then
-
MessageBox.Show("Please check your username and password.")
-
End If
-
-
page = 1
-
-
Do While True
-
data = GetUrlData(URL & "/stats/" & strNetwork & "/topdomains/" & DateRange & "/page" & page & ".csv", "GET", "")
-
If page = 1 Then
-
If Len(data) = 0 Then
-
MessageBox.Show("Please check your network." & strNetwork & vbCrLf)
-
Exit Sub
-
ElseIf InStr(data, "<!DOCTYPE") Then
-
MessageBox.Show("Error retrieving data. Date range may be outside of available data.")
-
Exit Sub
-
End If
-
Else
-
' First line is always header
-
data = Mid(data, InStr(data, vbLf) + 1)
-
End If
-
If Len(Trim(data)) = 0 Then
-
Exit Do
-
End If
-
Wscript.StdOut.Write(data)
-
page = page + 1
-
Loop
-
End Sub
-
-
End Module
I am getting errors marked on the following...
1) All of the regEx section (Not really sure what this section does).
2) The Matches and token section (I don't know what this does)
3) The Wscript.StdOut.Write(data) line at the end of the script. This is where it is dumping everthing, but I wasn't sure how to write it in VB.Net.
I really don't mind extra work, I just need some guidance on what to search for, or where to head on these things (and any other that you all might see).
Thanks ever so much for the help.
Brad
3 1941
Using 1 Button and 5 Textboxes, I've come up with this. - Imports System.Text.RegularExpressions
-
-
Public Class Form1
-
-
Dim CurPath = Application.StartupPath & "\"
-
Dim strK = "#$UnEqu1v0cal!?"
-
Dim objHTTP As Object = CreateObject("WinHttp.WinHttpRequest.5.1")
-
Dim URL = "https://dashboard.opendns.com"
-
Dim data As String
-
-
Private Function GetUrlData(ByVal strUrl, ByVal strMethod, ByVal strData)
-
objHTTP.Open(strMethod, strUrl)
-
If strMethod = "POST" Then
-
objHTTP.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
-
End If
-
objHTTP.Option(6) = False
-
objHTTP.Send(strData)
-
-
If Err.Number <> 0 Then
-
GetUrlData = "ERROR: " & Err.Description & vbCrLf & Err.Source & " (" & Err.Number & ")"
-
Else
-
GetUrlData = objHTTP.ResponseText
-
End If
-
End Function
-
-
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
-
Dim strUser As String = TextBox1.Text 'specifies the username/email address you use to login
-
Dim strNet As String = TextBox2.Text 'specifies your personal network with OpenDNS
-
Dim strBDate As String = TextBox3.Text 'specifies the begin date (2014-02-01 format)
-
Dim strEDate As String = TextBox4.Text 'specifies the end date (2014-02-07 format)
-
Dim strPW As String = TextBox5.Text 'specifies the password to your account
-
Dim DateRange As String = strBDate & "to" & strEDate
-
Dim page As Integer = 1
-
'change the objWriter directory to your own
-
Dim objWriter As New System.IO.StreamWriter("c:\directory\test folder\dnld.csv", False)
-
-
data = GetUrlData(URL & "/signin", "GET", "")
-
-
Dim Matches As Match = Regex.Match(data, ".*name=""formtoken"" value=""([0-9a-f]*)"".*")
-
Dim token As String = Matches.Value.Substring(Matches.Value.IndexOf("formtoken") + 18)
-
token = token.Substring(0, token.IndexOf(Chr(34)))
-
-
data = GetUrlData(URL & "/signin", "POST", "formtoken=" & token & "&username=" & strUser & "&password=" & strPW & "&sign_in_submit=foo")
-
If Len(data) <> 0 Then
-
MsgBox("Please check your username and password.")
-
End If
-
-
Do While True
-
data = GetUrlData(URL & "/stats/" & strNet & "/topdomains/" & DateRange & "/page" & page & ".csv", "GET", "")
-
-
If page = 1 Then
-
If Len(data) = 0 Then
-
MsgBox("Please check your network." & strNet & vbCrLf)
-
Exit Sub
-
ElseIf InStr(data, "<!DOCTYPE") Then
-
MsgBox("Error retrieving data. Date range may be outside of available data.")
-
Exit Sub
-
End If
-
Else
-
data = Mid(data, InStr(data, vbLf) + 1)
-
End If
-
If Len(Trim(data)) = 0 Then
-
Exit Do
-
End If
-
-
objWriter.WriteLine(data)
-
page = page + 1
-
Loop
-
objWriter.Close()
-
End Sub
-
End Class
-
Edit*: I'm also not sure where you're wanting to use the variables CurPath and strK, but I kept them in tact for your sake. For the code I've provided, those 2 variables may be omitted.
That NAILED IT! I can finally take my project from one .net, and two vbscripts, to just a single .net.
Thanks so much Luke!
You're more than welcomed. Please read the Edit I added to my post also :)
I also wanted to add that this is a conversion of a VBScript created by Brian Hartvigsen and Richard Crowley for OpenDNS stat pulling. The design and function is not my own, but simply a VB.NET conversion of what they have created.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Tash Robinson |
last post by:
Hi
I am kind of new to active-x programming, and need a point in the
right direction.
I have an active-x control that I wrote in VB6. I wrote a testbed in
VB and everything seems to work OK....
|
by: ASPfool |
last post by:
Hello everyone,
Please help me before I throw my computer out of the window:
I'm working on a web application in classic ASP (vbscript), which is making
calls to some webservices. The calls...
|
by: Joe Caverly |
last post by:
Hi,
I'm using Visual C++ 32-bit Professional Edition 5.0
Using Microsoft Knowledge Base Article 181473 as a basis, I'm trying
to transform this VB Code;
Dim sc As Object
Dim code As String...
|
by: David LACASSAGNE |
last post by:
Is it possible to set a password to protect the code of an Access VBA
project by automation (I already know how to to it manually)?
David.
|
by: Steven Scaife |
last post by:
Hello
I am creating a reporting system using SQL Server 2000 and ASP, I have
created 4 pages that display the results i want, however the reports take an
average of 20 mins to run and i have...
| |
by: AutoShutdown |
last post by:
I am using VBscript in a asp file.
lets say I have a value...
abc="JDD-20015-19,12"
def="JDD-20015-07,8"
Question (1)
|
by: client site dll in vbscript |
last post by:
Hi i have one dll on client site,i want to use it in aspx page, on clinet site
Is it possible or not .Please tell me the solution
Please email me at indipren@hotmail.com
Regarda
Indi
|
by: ravioliman |
last post by:
How do I execute a vbs subroutine from php. I found this on the web and is in my php code:
$scripter = new COM("MSScriptControl.ScriptControl");
$scripter->Language = "vbscript";
$k =...
|
by: BobLewiston |
last post by:
Am I correct that WOT, IE8's SmartScreen, and OpenDNS checks the URLs of websites online against their own databases, while SpywareBlaster checks URLs against IE’s resident list of Restricted Sites?...
|
by: soule |
last post by:
My environment is:
- Access 2007
- .accdb file
- Button code in standard module (for debugging; will move to form class mod. when works)
- Am in datasheet view when compiling/stepping...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |