ASP.NET & Microsoft's VB.NET Ping Application Trouble...

liunx

Guest
Hopefully someone can help me here, I'm fairly new to VB.NET as well as ASP.NET, but I'm doing my best.

I am trying to create a simple ASP.NET application that pings about 30 hard-coded IP addresses in my company network, and simply relate wether they are up or down. I am using the Microsoft setup for a simple Ping utility, found here:

<!-- m --><a class="postlink" href="http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B828993">http://support.microsoft.com/default.as ... s%3B828993</a><!-- m -->

I can successfully get this code to compile and operate, however, when I attempt to build a small ASP page to use this code, I get a error stating that access to the socket has been denied based upon its set permissions.

Basically what I have done is created a VB.NET, ASP.NET Web Application in Visual Studio.NET. I have a simple asp Datagrid with 3 columns. The first is the IP Address, the second is the status (up or down) and the thrid is response time (which can be pulled from the Microsoft utility)

I have simply copied and pasted the code from the link above into my WebForm1.aspx.vb file...with a few obvious modifications. (Removed the Main Sub,etc) I can successfully call the Ping function, but I get an error as stated above.

Here's my code for the page, minus the Microsoft code from above which appears just after this code. Perhaps someone can offer guidance on permission rights that I might be missing.

Public Class WebForm1
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents Ping As System.Web.UI.WebControls.DataGrid

'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Public Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim PingTable As New DataTable
PingTable.Columns.Add("IP_Address")
PingTable.Columns.Add("Status")

Dim i As Integer
Dim j As Integer = 64

Dim row_total As Integer = 14

For i = 1 To row_total
Dim PingRow As DataRow = PingTable.NewRow()
PingRow(0) = "67.50.115." & i + j
Scan(PingRow(0), PingRow(1))
PingTable.Rows.InsertAt(PingRow, PingTable.Rows.Count)
Next

PingTB.DataSource = PingTable
PingTB.DataBind()


End Sub

Sub Scan(ByVal IP, ByRef Status)

Dim strComputer As String = IP
Dim objShell = CreateObject("WScript.Shell")
Dim objScriptExec = objShell.Exec("ping -n 2 -w 100 " & strComputer)
Dim strPingResults = LCase(objScriptExec.StdOut.ReadAll)

If InStr(strPingResults, "reply from") Then
Status = "<refresh><img src='http://www.htmlforums.com/PingApp/uparrow.gif' align='middle'></refresh>"
Else
Status = "<refresh><img src='http://www.htmlforums.com/PingApp/downarrow.gif' align='middle'></refresh>"
End If

End Sub
End Class

Thank you again for any and all advice, it is greatly appreciated.That is correct. As ASP.net does not have permissions to create RAW sockets. You must use one of the Clients that is already built for you.

UpdClient or TcpClient are the classes that you needed, or you can explicitly trust your application to create Sockets. Doing so might have security repercussions though as it would allow it to open a raw socket to grab anything it wants.Thanks alot afterburn, that's exactly what I was wondering. Awesome help as always! I really appreciate it.

I actually found a much more simplified solution...

Public Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim PingTable As New DataTable
PingTable.Columns.Add("IP_Address")
PingTable.Columns.Add("Status")

Dim i As Integer
Dim j As Integer = 0

Dim row_total As Integer = 30

For i = 1 To row_total
Dim PingRow As DataRow = PingTable.NewRow()
PingRow(0) = "1.1.1." & i + j

Scan(PingRow(0), PingRow(1))

PingTable.Rows.InsertAt(PingRow, PingTable.Rows.Count)
Next

PingTB.DataSource = PingTable
PingTB.DataBind()

End Sub

Sub Scan(ByVal IP, ByRef Status)

Dim strComputer As String = IP
Dim objShell = CreateObject("WScript.Shell")
Dim objScriptExec = objShell.Exec("ping -n 2 -w 1000 " & strComputer)
Dim strPingResults = LCase(objScriptExec.StdOut.ReadAll)

If InStr(strPingResults, "reply from") Then
Status = "<img src='http://www.htmlforums.com/PingApp/uparrow.gif' align='middle'>"
Else
Status = "<img src='http://www.htmlforums.com/PingApp/downarrow.gif' align='middle'>"
End If

End Sub

Not quite as fancy, but does exactly what I needed.

I will definitely look into your answer afterburn, for my own knowledge. Thanks again :)Not sure if this will help but here is a class in the framework for granting or denying permissions on sockets.

SocketPermission
its located in the System.Net namespace.<!-- m --><a class="postlink" href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/THCMCh09.asp">http://msdn.microsoft.com/library/defau ... CMCh09.asp</a><!-- m -->


Tell you how to create an external assembly then grant access to it using an XML document and the web.config file. You must scroll half way down to see how to do it ...Thanks agian afterburn, that's definitely some good info. I'm reading through it now . Make more sense now that I see ASP.NET doesn't have any of it's own permissions for raw sockets.

Here's another quick question relating to this little project I've been working on.

As it stands now, the page takes about 10 seconds to load, since it's pinging around 15 IP Addresses. If the IP is 'up' a green arrow is displayed, if the IP is 'down' a red arrow is displayed. What I would like to happen is the page to load up immediately (so I know it's working) and all the arrows are grey...as the IP's are pinged, the icon changes based upon the IP's status.

Currently, I just have the 'ping' function in the page_load(), I'm just not sure how to update the datagrid 'on-the-fly'. Any thoughts would be greatly appreciated.

Thanks again so much. :Dyou can wrap the ping process in a web service returning XML, parse the XML update each element in the grid based on the status of the Ping process.I guess I'll have to brush up on my XML, I'm still new to most of this. So basically, would I still DataBind my DataGrind...or would I build the grid seperately based upon the returned XML from the WebService? (sorry, never used a webservice before either...complete beginner I know sorry)

Thank you again for your continued help...I'm playing with it right now.First things first ... Read about WebMethod attributes on classes that use the @WebService directive instead of Page. The file extension is .asmx on Web Service files.

Pass in what ever you might need and package an XML response in your function then response. Place a javascript mouseover like function that will change the image dynamically.

var stateUp = new Image();
stateUp.src = <!-- m --><a class="postlink" href="http://www.htmlforums.com/archive/index.php/">http://www.htmlforums.com/archive/index.php/</a><!-- m -->"UpArrow.gif";

var stateDown = new Image();
stateDown.src = <!-- m --><a class="postlink" href="http://www.htmlforums.com/archive/index.php/">http://www.htmlforums.com/archive/index.php/</a><!-- m -->"DownArrow.gif";

var stateUnknown = new Image();
stateUnknown.src = <!-- m --><a class="postlink" href="http://www.htmlforums.com/archive/index.php/">http://www.htmlforums.com/archive/index.php/</a><!-- m -->"Unknown.gif";

function MachineState(imgName,state)
{
document[imgName].src = <!-- m --><a class="postlink" href="http://www.htmlforums.com/archive/index.php/eval(state">http://www.htmlforums.com/archive/index.php/eval(state</a><!-- m --> + ".Src");
}

to change the image use the above.

that will allow you to get state when complete. you might want to have a valid function that continues to ping the address every 5 - 10 minutes and reset the state of the images based on poll of webservice.Excellent example afterburn, thanks so much. I will continue to study the WebService methods and functionality. Again I appreciate your help and will let you know if I have any questions. :) Thanks again!
 
Back
Top