By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,805 Members | 1,273 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,805 IT Pros & Developers. It's quick & easy.

OpenDNS download automation - VBScript to VB.Net

BHo15
100+
P: 142
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

✓ answered by Luk3r

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.

Share this Question
Share on Google+
3 Replies


100+
P: 299
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
100+
P: 142
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

100+
P: 299
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

Post your reply

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