Multiple IP pinging - vb.net

If My.Computer.Network.Ping("192.168.20.251") Then
Console.WriteLine("IP FOUND")
Else
Console.WriteLine("IP NOT FOUND")
End If
Is there a way to ping an array of IP address and display if how many is online and offline?

Let's consider ListOfIPs is a List(Of String) or a string array that is already populated.
I can't clearly understand if you just want to the number of online IPs and offline.
Here's a solution to do both: show which one is ON/OFF, and count them.
Dim IpIsOn as Integer = 0
Dim IpIsOff as Integer = 0
For Each ip as String in ListOfIPs
If My.Computer.Network.Ping(ip) Then
Console.WriteLine(ip & " is online")
IpIsOn = IpIsOn + 1
Else
Console.WriteLine(ip & " is offline")
IpIsOff = IpIsOff + 1
End If
Next
Console.WriteLine("A total of " & IpIsOn & " IP are online, while " & IpIsOff & " are offline.")

Yes, this can be done by putting the IP addresses that you want into an array and loop through that array and then test every IP.
Code (not tested, but should give you the idea):
Dim list As New List(Of String)
list.Add("192.168.20.251")
list.Add("192.168.233.1")
list.Add("192.168.0.199")
list.Add("192.168.2.55")
For Each item As String In list
If My.Computer.Network.Ping(item) Then Console.WriteLine("IP FOUND " + item) Else Console.WriteLine("IP NOT FOUND " + item) End If
Next

Related

Q. Accept two numbers using an InputBox in two different variables and print the sum of two in a message box. (Visual Basic)

I have to answer this in Visual Basic.I actually don't have any idea how to solve this, our teacher barely teaches us practical stuff. I have to submit this assignment by today too.
I have tried to do solve it and searched the internet for it, but I could barely understand it.
Here is some code you can build upon:
Public Sub AddTwoNumbers()
Dim FirstNumber As String = Convert.toInt32(InputBox("Enter the first number.")) 'Get the first number
Dim SecondNumber As String = Convert.toInt32InputBox("Enter the second number.")) 'Get the second number
Dim Result As Integer = 0 'Used to store the result in
'Now perform the calculation.
Result = FirstNumber + SecondNumber
'Then show the result in a MessageBox
MessageBox.Show("The result is: " & Result.ToString())
End Sub
Here's an example using Integer.TryParse():
Dim value1, value2 As Integer
Dim response As String = InputBox("Enter first Integer:")
If Integer.TryParse(response, value1) Then
response = InputBox("Enter second Integer:")
If Integer.TryParse(response, value2) Then
Dim sum As Integer = value1 + value2
MessageBox.Show("The sum of " & value1 & " and " & value2 & " is " & sum)
Else
MessageBox.Show(response & " is not a valid Integer!")
End If
Else
MessageBox.Show(response & " is not a valid Integer!")
End If

Excel+VBA: Userfrom string Deal(Order) Number parsing

I am trying to make a full function report file which connects to old version access data base. I need to build a Userform where user can enter deal (order) number just like we can add pages in print dialog box.
So far I have found below question which has given me partial answer:
How to Parsing Full String and split into several String in Excel VBA?
I can use Split(string) method to get various deal numbers when separated by ",".
However, I also need From to TO deal number option as well.
so, if user enters on String1 = "10001 - 10050, 20111 , 20115"
then I need output as
Deal_Line.Deal_Number >= 10001 and Deal_Line.Deal_Number <= 10050
and Deal_Line.Deal_Number = 20111 and Deal_Line.Deal_Number = 20115.
I can refine SQL String for my requirement, I would like to know if there is a way to use two deliminator.
Expanding on #user3598756 answer, (which does exactly what you asked for) and interpreting your requirements I come up with the following:
Function ProcessString(strng1 As String, fieldname As String) As String
Dim sqlStrng As String
Dim strng As Variant, limits As Variant
For Each strng In Split(strng1, ",")
limits = Split(strng, "-")
If UBound(limits) = 0 Then
sqlStrng = sqlStrng & "[FIELD] = " & limits(0) & "|"
Else
sqlStrng = sqlStrng & "([FIELD] >= " & Trim(limits(0)) & " And [FIELD] <= " & Trim(limits(1)) & ")|"
End If
Next
ProcessString = Replace(WorksheetFunction.Trim(Join(Split(Left(sqlStrng, Len(sqlStrng) - 1), "|"), " Or ")), "[FIELD]", fieldname)
End Function
The test line would be:
MsgBox ProcessString("10001 - 10050, 20111 , 20115","Deal_Line.Deal_Number")
This will produce an output that I think is more likely to be what you actually want - using OR instead of AND for starters, adding in parentheses needed for the OR to work properly and allowing for multiple table/field names.
Then again, maybe I've misinterpreted your requirement - I just can't see what use having all AND would be one one field.
you could use this helper function:
Function ProcessString(strng1 As String) As String
Dim sqlStrng As String
Dim strng As Variant, limits As Variant
For Each strng In Split(strng1, ",")
limits = Split(strng, "-")
If UBound(limits) = 0 Then
sqlStrng = sqlStrng & "Deal_Line.Deal_Number = " & limits(0) & "|"
Else
sqlStrng = sqlStrng & "Deal_Line.Deal_Number >= " & limits(0) & " And Deal_Line.Deal_Number <= " & limits(1) & "|"
End If
Next
ProcessString = WorksheetFunction.Trim(Join(Split(Left(sqlStrng, Len(sqlStrng) - 1), "|"), " And "))
End Function
to be tested/expolited in your "main" code as:
Sub main()
MsgBox ProcessString("10001 - 10050, 20111 , 20115")
End Sub

If a listbox contains an item LIKE

Is there anywhere to check whether a listbox contains an item which is similar to a string?
I have tried to use a Like statement but this doesn't work.
I have tried:
For i As Integer = 0 To TopicListBox.Items.Count - 1
If (TopicListBox.Items(i).ToString.Contains(Record.Item("Keyphrase2"))) Then
Dim Item As String = TopicListBox.Items(i).ToString
If Item Like "Questions related to:" & Record.Item("Keyphrase2") & ": |*| Qs" Then
Dim ItemSplit() As String = Item.Split(New Char() {"|"c})
Dim AmountOfQuestionsAfter As Integer = AmountOfQuestions + CInt(ItemSplit(1))
Item = ItemSplit(0) & AmountOfQuestionsAfter & ItemSplit(1)
Else
TopicListBox.Items.Add("Questions related to:" & Record.Item("Keyphrase2") & ": |" & AmountOfQuestions & "| Qs")
Exit For
End If
End If
Next
I don't really understand what you are trying to accomplish, but here is a LINQ function to return all the items that contain a certain string.
Dim lb As New ListBox
lb.Items.Add("asdf")
lb.Items.Add("asdfasdf")
lb.Items.Add("aifisasdf")
lb.Items.Add("adf")
'' find all items in the ListBox that contain the string "asdf"
Dim found = lb.Items.Cast(Of String).Where(Function(f) f.Contains("asdf"))
For Each s In found
'' do something with those items
Next
Can't you just do
If Item.Contains("Questions related to:" & Record.Item("Keyphrase2")) Then
...
End If
or
If Item.StartsWith("Questions related to:" & Record.Item("Keyphrase2")) Then
...
End If
?
Dim Found = (From Result In lb.Items Where Result.ToString = "Value" Select Result)
If (Found.Count > 0) Then
' your code
End If

get ip address from url

I am trying to get country location from the ip address which I am also looking up from actual url. However for certain urls I am getting the following error:
The requested name is valid and was found in the database, but it does not have the correct associated data being resolved for
I wanted to use the following code to identify the proxy perhaps but since this is a regular console app I am not sure how to get around it. Here is my code;
For Each prod In querylist
If myfetcher.getHtml(prod, userAgent, page) Then
' The lines below I use to find proxy ip
' but error name 'Request' not declared
' Dim nowip As String
' nowip = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
' If nowip = "" Then
'nowip = Request.ServerVariables("REMOTE_ADDR")
'End If
'
If prod.Contains("http://") Then
prod = Regex.Replace(prod, "http://", "")
End If
badHost = prod
Dim ipEntry As IPHostEntry = Dns.GetHostByName(prod)
Dim IPAdd As IPAddress() = ipEntry.AddressList
Dim i As Integer = 0
For i = 0 To IPAdd.GetUpperBound(0)
number = number & "IP Address {0}:{1}" & IPAdd(i).ToString
Next
IPList.Add(prod & " " & number)
number = ""
Else
badList.Add(prod)
number = ""
End If
count = count + 1
Next
Here's a language agnostic way to do it:
do a HTTP GET for
domain2ip.net/:url
like
http://domain2ip.net/www.edresearch.co.jp
is
122.200.237.66
you can even to it from JavaScript
$.getJSON("http://domain2ip.net/google.com", callback);
Disclosure: it's my site, and open source.

A better way of writing this : growing array

Was looking at some code earlier, and am thinking that there has to be a more elegant way of writing this....
(returnVar.Warnings is a string array, it could be returned as any size depending on the number of warnings that are logged)
For Each item In items
If o.ImageContent.ImageId = 0 Then
ReDim Preserve returnVar.Warnings(returnVar.Warnings.GetUpperBound(0) + 1)
returnVar.Warnings(returnVar.Warnings.GetUpperBound(0)) = "Section: " & section.<header>.<title>.ToString & " , Item: " & item.<title>.ToString
End If
Next
use the generic List(of string) then get an array containing the list data if you need it
dim list = new List(of string)
list.Add("foo")
list.Add("bar")
list.ToArray()
Can't you use ArrayList which does this for you?
http://msdn.microsoft.com/en-us/library/system.collections.arraylist.aspx
Start by moving the If statement out of the loop.
If you are using framework 3.5, you can use LINQ to loop the items.
If o.ImageContent.ImageId = 0 Then
returnVar.Warnings = items.Select(Function(item) "Section: " & section.<header>.<title>.ToString & " , Item: " & item.<title>.ToString).ToArray()
Else
returnVar.Warnings = New String() {}
End If