How to ping an IP address
You can not simply ping in Java as it relies on ICMP, which is sadly not supported in Java
http://mindprod.com/jgloss/ping.html
Use sockets instead
Hope it helps
How to ping an IP Address in golang
As @desaipath mentions, there is no way to do this in the standard library. However, you do not need to write the code for yourself - it has already been done:
https://github.com/tatsushid/go-fastping
Note, sending ICMP packets requires root privileges
Pinging servers in Python
This function works in any OS (Unix, Linux, macOS, and Windows)
Python 2 and Python 3
EDITS:
By @radato os.system
was replaced by subprocess.call
. This avoids shell injection vulnerability in cases where your hostname string might not be validated.
import platform # For getting the operating system name
import subprocess # For executing a shell command
def ping(host):
"""
Returns True if host (str) responds to a ping request.
Remember that a host may not respond to a ping (ICMP) request even if the host name is valid.
"""
# Option for the number of packets as a function of
param = '-n' if platform.system().lower()=='windows' else '-c'
# Building the command. Ex: "ping -c 1 google.com"
command = ['ping', param, '1', host]
return subprocess.call(command) == 0
Note that, according to @ikrase on Windows this function will still return True
if you get a Destination Host Unreachable
error.
Explanation
The command is ping
in both Windows and Unix-like systems.
The option -n
(Windows) or -c
(Unix) controls the number of packets which in this example was set to 1.
platform.system()
returns the platform name. Ex. 'Darwin'
on macOS.subprocess.call()
performs a system call. Ex. subprocess.call(['ls','-l'])
.
How to ping an IP address from a VueJS application?
Ping is a node.js module not supported in the browser. This module would need to run server-side.
This could be accomplished us axios where you issue a GET call to the url and if you get a 200 back that indicates a successful call. This could also be accomplished using $ajax.
axios example
const response = await axios.get('https://api.github.com/users/mapbox');
if (response.status === 200) {
console.log('success'
}
PYTHON PING IP ADDRESS WITH RESULTS
If you need to capture the output, or just don't like the way you are doing it currently then you could capture stdout and check the output for a failure string. You can capture stdout
like this:
def ping(host):
param = '-n' if platform.system().lower()=='windows' else '-c'
command = ['ping', param, '1', host]
result = subprocess.run(command, stdout=subprocess.PIPE)
output = result.stdout.decode('utf8')
if "Request timed out." in output or "100% packet loss" in output:
return "NOT CONNECTED"
return "CONNECTED"
print(ping(ip_addr))
Ping multiple computers through Excel and have IP addresses in a separate column
This worked for me
Sub temp()
Set WshShell = CreateObject("WScript.Shell")
RowCount = Worksheets("Sheet1").UsedRange.Rows.Count
For i = 1 To RowCount
Url = Worksheets("Sheet1").Cells(i, 1).Value
cmd = "ping " + Url
Set WshShellExec = WshShell.Exec(cmd)
result = WshShellExec.StdOut.ReadAll
Worksheets("Sheet1").Cells(i, 2).Value = getIP(result)
If (InStr(result, "Received = 4")) Then
Worksheets("Sheet1").Cells(i, 3).Value = "Online"
End If
Next
End Sub
Function getIP(result)
ip = Split(result, vbNewLine)(1)
startIndex = InStr(ip, "[") + 1
endIndex = InStr(ip, "]")
getIP = Mid(ip, startIndex, endIndex - startIndex)
End Function
Related Topics
How to Synchronize a Static Variable Among Threads Running Different Instances of a Class in Java
Test If Element Is Present Using Selenium Webdriver
Why Is String.Chars() a Stream of Ints in Java 8
Is It Expensive to Use Try-Catch Blocks Even If an Exception Is Never Thrown
How to Create a Zip File in Java
How to Convert a Java 8 Intstream to a List
Java: Run a Function After a Specific Number of Seconds
How to Write Console Output to a Txt File
Junit Testing with Simulated User Input
Regular Expression to Match Urls in Java
How to "Intercept" Ctrl+C in a Cli Application
What Is the Default Access Specifier in Java
How Are Integers Internally Represented at a Bit Level in Java