473,473 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

OpenDNS download automation - VBScript to VB.Net

BHo15
143 New Member
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)...

Expand|Select|Wrap|Line Numbers
  1. cscript /NoLogo fetchstats.vbs <username> <network-id> <YYYY-MM-DD> [<YYYY-MM-DD>]

This is the VBScript that does all of the work...

Expand|Select|Wrap|Line Numbers
  1. Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
  2.  
  3. Function GetUrlData(strUrl, strMethod, strData)
  4.         objHTTP.Open strMethod, strUrl
  5.         If strMethod = "POST" Then
  6.                 objHTTP.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
  7.         End If
  8.         objHTTP.Option(6) = False
  9.         objHTTP.Send(strData)
  10.  
  11.         If Err.Number <> 0 Then
  12.                 GetUrlData = "ERROR: " & Err.Description & vbCrLf & Err.Source & " (" & Err.Nmber & ")"
  13.         Else
  14.                 GetUrlData = objHTTP.ResponseText
  15.         End If
  16. End Function
  17.  
  18. URL="https://dashboard.opendns.com"
  19.  
  20.  
  21. If Wscript.Arguments.Count < 3 Or Wscript.Arguments.Count > 4 Then
  22.         Usage
  23. End If
  24.  
  25. Username = Wscript.Arguments.Item(0)
  26. If Len(Username) = 0 Then: Usage
  27. Network = Wscript.Arguments.Item(1)
  28. If Len(Network) = 0 Then: Usage
  29.  
  30. Set regDate = new RegExp
  31. regDate.Pattern = "^\d{4}-\d{2}-\d{2}$"
  32.  
  33. DateRange = Wscript.Arguments.Item(2)
  34. If Not regDate.Test(DateRange) Then: Usage
  35.  
  36. If Wscript.Arguments.Count = 4 Then
  37.         ToDate = Wscript.Arguments.Item(3)
  38.         If Not regDate.Test(toDate) Then: Usage
  39.         DateRange = DateRange & "to" & ToDate
  40. End If
  41.  
  42.  
  43. ' Are they running Vista or 7?
  44. On Error Resume Next
  45. Set objPassword = CreateObject("ScriptPW.Password")
  46.     If strP="" Then strP=BASE64SHA1(InputBox("Please type your password."))
  47.      Password = strP
  48.  
  49. Wscript.StdErr.Write vbCrLf
  50. On Error GoTo 0
  51.  
  52. Set regEx = New RegExp
  53. regEx.IgnoreCase = true
  54. regEx.Pattern = ".*name=""formtoken"" value=""([0-9a-f]*)"".*"
  55.  
  56. data = GetUrlData(URL & "/signin", "GET", "")
  57.  
  58. Set Matches = regEx.Execute(data)
  59. token = Matches(0).SubMatches(0)
  60.  
  61. data = GetUrlData(URL & "/signin", "POST", "formtoken=" & token & "&username=" & Escape(Username) & "&password=" & Escape(Password) & "&sign_in_submit=foo")
  62. If Len(data) <> 0 Then
  63.         Wscript.StdErr.Write "Login Failed. Check username and password" & vbCrLf
  64.         WScript.Quit 1
  65. End If
  66.  
  67. page=1
  68. Do While True
  69.         data = GetUrlData(URL & "/stats/" & Network & "/topdomains/" & DateRange & "/page" & page & ".csv", "GET", "")
  70.         If page = 1 Then
  71.                 If LenB(data) = 0 Then
  72.                         WScript.StdErr.Write "You can not access " & Network & vbCrLf
  73.                         WScript.Quit 2
  74.                 ElseIf InStr(data, "<!DOCTYPE") Then
  75.                  Wscript.StdErr.Write "Error retrieving data. Date range may be outside of available data."
  76.                  Wscript.Quit 2
  77.                 End If
  78.         Else
  79.                 ' First line is always header
  80.                 data=Mid(data,InStr(data,vbLf)+1)
  81.         End If
  82.         If LenB(Trim(data)) = 0 Then
  83.                 Exit Do
  84.         End If
  85.         Wscript.StdOut.Write data
  86.         page = page + 1
  87. 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.

Expand|Select|Wrap|Line Numbers
  1. Imports System.Text.RegularExpressions
  2.  
  3. Module GetStats
  4.  
  5.     Dim CurPath = Application.StartupPath & "\"
  6.     Dim strK = "#$UnEqu1v0cal!?"
  7.     Dim objHTTP As Object = CreateObject("WinHttp.WinHttpRequest.5.1")
  8.     Dim URL = "https://dashboard.opendns.com"
  9.     Dim data as string
  10.  
  11.     Function GetUrlData(ByVal strUrl, ByVal strMethod, ByVal strData)
  12.         objHTTP.Open(strMethod, strUrl)
  13.         If strMethod = "POST" Then
  14.             objHTTP.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
  15.         End If
  16.         objHTTP.Option(6) = False
  17.         objHTTP.Send(strData)
  18.  
  19.         If Err.Number <> 0 Then
  20.             GetUrlData = "ERROR: " & Err.Description & vbCrLf & Err.Source & " (" & Err.Number & ")"
  21.         Else
  22.             GetUrlData = objHTTP.ResponseText
  23.         End If
  24.     End Function
  25.  
  26.     Public Sub Process(ByVal strUser As String, ByVal strNetwork As String, ByVal strBDate As String, ByVal strEDate As String, ByVal strPW As String)
  27.         Dim DateRange As String
  28.         Dim page As Integer
  29.         Dim regEx As String
  30.  
  31.         DateRange = strBDate & "to" & strEDate
  32.  
  33.         regEx = New Regex
  34.         regEx.IgnoreCase = True
  35.         regEx.Pattern = ".*name=""formtoken"" value=""([0-9a-f]*)"".*"
  36.  
  37.         data = GetUrlData(URL & "/signin", "GET", "")
  38.  
  39.         Matches = regEx.Execute(data)
  40.         token = Matches(0).SubMatches(0)
  41.  
  42.         data = GetUrlData(URL & "/signin", "POST", "formtoken=" & token & "&username=" & Escape(strUser) & "&password=" & Escape(strPW) & "&sign_in_submit=foo")
  43.         If Len(data) <> 0 Then
  44.             MessageBox.Show("Please check your username and password.")
  45.         End If
  46.  
  47.         page = 1
  48.  
  49.         Do While True
  50.             data = GetUrlData(URL & "/stats/" & strNetwork & "/topdomains/" & DateRange & "/page" & page & ".csv", "GET", "")
  51.             If page = 1 Then
  52.                 If Len(data) = 0 Then
  53.                     MessageBox.Show("Please check your network." & strNetwork & vbCrLf)
  54.                     Exit Sub
  55.                 ElseIf InStr(data, "<!DOCTYPE") Then
  56.                     MessageBox.Show("Error retrieving data. Date range may be outside of available data.")
  57.                     Exit Sub
  58.                 End If
  59.             Else
  60.                 ' First line is always header
  61.                 data = Mid(data, InStr(data, vbLf) + 1)
  62.             End If
  63.             If Len(Trim(data)) = 0 Then
  64.                 Exit Do
  65.             End If
  66.             Wscript.StdOut.Write(data)
  67.             page = page + 1
  68.         Loop
  69.     End Sub
  70.  
  71. 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
Feb 5 '14 #1
3 1941
Luk3r
300 Contributor
Using 1 Button and 5 Textboxes, I've come up with this.


Expand|Select|Wrap|Line Numbers
  1. Imports System.Text.RegularExpressions
  2.  
  3. Public Class Form1
  4.  
  5.     Dim CurPath = Application.StartupPath & "\"
  6.     Dim strK = "#$UnEqu1v0cal!?"
  7.     Dim objHTTP As Object = CreateObject("WinHttp.WinHttpRequest.5.1")
  8.     Dim URL = "https://dashboard.opendns.com"
  9.     Dim data As String
  10.  
  11.     Private Function GetUrlData(ByVal strUrl, ByVal strMethod, ByVal strData)
  12.         objHTTP.Open(strMethod, strUrl)
  13.         If strMethod = "POST" Then
  14.             objHTTP.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
  15.         End If
  16.         objHTTP.Option(6) = False
  17.         objHTTP.Send(strData)
  18.  
  19.         If Err.Number <> 0 Then
  20.             GetUrlData = "ERROR: " & Err.Description & vbCrLf & Err.Source & " (" & Err.Number & ")"
  21.         Else
  22.             GetUrlData = objHTTP.ResponseText
  23.         End If
  24.     End Function
  25.  
  26.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  27.         Dim strUser As String = TextBox1.Text       'specifies the username/email address you use to login
  28.         Dim strNet As String = TextBox2.Text        'specifies your personal network with OpenDNS
  29.         Dim strBDate As String = TextBox3.Text      'specifies the begin date (2014-02-01 format)
  30.         Dim strEDate As String = TextBox4.Text      'specifies the end date (2014-02-07 format)
  31.         Dim strPW As String = TextBox5.Text         'specifies the password to your account
  32.         Dim DateRange As String = strBDate & "to" & strEDate
  33.         Dim page As Integer = 1
  34.         'change the objWriter directory to your own
  35.         Dim objWriter As New System.IO.StreamWriter("c:\directory\test folder\dnld.csv", False)
  36.  
  37.         data = GetUrlData(URL & "/signin", "GET", "")
  38.  
  39.         Dim Matches As Match = Regex.Match(data, ".*name=""formtoken"" value=""([0-9a-f]*)"".*")
  40.         Dim token As String = Matches.Value.Substring(Matches.Value.IndexOf("formtoken") + 18)
  41.         token = token.Substring(0, token.IndexOf(Chr(34)))
  42.  
  43.         data = GetUrlData(URL & "/signin", "POST", "formtoken=" & token & "&username=" & strUser & "&password=" & strPW & "&sign_in_submit=foo")
  44.         If Len(data) <> 0 Then
  45.             MsgBox("Please check your username and password.")
  46.         End If
  47.  
  48.         Do While True
  49.             data = GetUrlData(URL & "/stats/" & strNet & "/topdomains/" & DateRange & "/page" & page & ".csv", "GET", "")
  50.  
  51.             If page = 1 Then
  52.                 If Len(data) = 0 Then
  53.                     MsgBox("Please check your network." & strNet & vbCrLf)
  54.                     Exit Sub
  55.                 ElseIf InStr(data, "<!DOCTYPE") Then
  56.                     MsgBox("Error retrieving data. Date range may be outside of available data.")
  57.                     Exit Sub
  58.                 End If
  59.             Else
  60.                 data = Mid(data, InStr(data, vbLf) + 1)
  61.             End If
  62.             If Len(Trim(data)) = 0 Then
  63.                 Exit Do
  64.             End If
  65.  
  66.             objWriter.WriteLine(data)
  67.             page = page + 1
  68.         Loop
  69.         objWriter.Close()
  70.     End Sub
  71. End Class
  72.  
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.
Feb 7 '14 #2
BHo15
143 New Member
That NAILED IT! I can finally take my project from one .net, and two vbscripts, to just a single .net.

Thanks so much Luke!
Feb 7 '14 #3
Luk3r
300 Contributor
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.
Feb 7 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
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....
6
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...
3
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...
4
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.
8
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...
3
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)
4
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
5
ravioliman
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 =...
0
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?...
0
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...
0
marktang
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,...
0
Oralloy
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,...
0
jinu1996
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...
1
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...
0
tracyyun
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...
1
isladogs
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.