I am using Net::Ping to check the network availability. When I run the code, it returns response. But when I recheck using command prompt, it is time out.
The Code :
$p = Net::Ping->new('tcp');
$p->hires();
($ret, $duration, $ip) = $p->ping($host);
if ($ret){
printf("$host [ip: $ip] is alive (packet return time: %.2f ms)\n", 1000 * $duration);
}else{
printf("time out");
}
Result :
10.10.x.x [ip: 10.10.x.x] is alive (packet return time: 18.72 ms)
Using command prompt :
C:\ping 10.10.x.x -t
Pinging 10.10.x.x with 32 bytes of data:
Request timed out.
Any explanation and advice would be appreciated
Related
So I'm trying to do a mock Traceroute function where a UDP packet is sent to an IP address. I'm trying to design the program in such a way where a packet is sent each time the packet makes it to a router. I am trying to do this by making a very short TTL. However, the recvfrom function is stalling.
Here's the code:
host_addr = gethostbyname(host)
port = 33434
max_hops = 30
ttl = 1
while True:
recv_socket = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)
send_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
send_socket.setsockopt(0, 4, ttl)
recv_socket.bind(("", port))
send_socket.sendto("waddup".encode(), (host_addr, port))
cur_addr = None
cur_name = None
path_end = False
cur_bytes = None
attempts_left = 3
timeReceived = 0
pingStartTime = 0
while not path_end and attempts_left > 0:
try:
pingStartTime = time.time()
cur_bytes, cur_addr = recv_socket.recvfrom(1024)
timeReceived = time.time()
path_end = True
cur_addr = cur_addr[0]
try:
cur_name = gethostbyaddr(cur_addr)[0]
except error:
cur_name = cur_addr
except error:
attempts_left -= 1
send_socket.close()
recv_socket.close()
if not path_end:
pass
if cur_addr is not None:
cur_host = "%s (%s) " % (cur_name, cur_addr)
else:
cur_host = ""
print("%d: %.0f ms " %
(
ttl,
(timeReceived - pingStartTime) * 1000,
) + cur_host
)
ttl += 1
if cur_addr == host_addr or ttl > max_hops:
break
I have set up the receiving socket for an ICMP packet as far as I can tell but it just hangs on recvfrom. I've allowed all ICMP connections on Windows Defender and when I run Wireshark, an appropriate ICMP packet is sent to my router.
The packet with the TTL expired message is the one I want to receive
I'm new to networking but all the code I've seen online has this exact setup. Would anyone be able to tell me why it's stalling and what I can do to fix it? I want the program to read the ICMP packet with the TTL expiration message.
I'm trying to make client-server connection, I don't know much about sockets etc. so I used github example for luajitsocket and im getting error "A non-blocking socket operation could not be completed immediately."
So I dont even know how can I fix that, that's why Im asking here
My code:
local port = 8080
local address = socket.find_first_address("*", port)
do -- server
local server = assert(socket.create("inet", "dgram", "udp"))
assert(server:set_blocking(false))
assert(server:bind(address))
print("hosting at ", address:get_ip(), address:get_port())
function update_server()
local data, addr = server:receive_from()
if data then
print(data)
assert(server:send_to(addr, "hello from server " .. os.clock()))
elseif addr ~= "timeout" then
error(addr)-- here
end
end
end
do -- client
local client = assert(socket.create("inet", "dgram", "udp"))
assert(client:set_blocking(false))
local next_send = 0
function update_client()
if next_send < os.clock() then
assert(client:send_to(address, "hello from client " .. os.clock()))
next_send = os.clock() + math.random() + 0.5
end
local data, addr = client:receive_from(address)
if data then
print(data, addr:get_ip(), addr:get_port())
elseif addr ~= "timeout" then
error(addr)
end
end
end
while true do
update_server()
update_client()
end
taken from: https://github.com/CapsAdmin/luajitsocket/blob/master/examples/udp_client_server.lua
I was looking about this error on google but can't find any working solution. Thanks in advance!
After set_blocking(false) that return code should not be considered abnormal.
You're probably getting it from receive_from(), which you should not be calling constantly, you should do it when select() or poll() tells you the socket has data waiting.
I am trying get Sony TV model from computer over Serialport.
Here is a documentation (page 80 - how to build query, page 83 - how to get model name).
https://cdn.cnetcontent.com/d1/9d/d19d926b-bb36-4ed3-bd03-af1cad4069da.pdf
Here is my simply script, but, unfortunatelly, always return ERR.
What I did wrong?
$hex = 0x4d,0x4e,0x52,0x44,0x31,0x20,0x20,0x20,0x0d
$com = New-Object System.IO.Ports.Serialport COM4,9600,None,8,one
$com.Open()
$com.Write($hex, 0, $hex.Count)
Start-Sleep -s 2
$read = $com.ReadExisting()
Write-Host "Response: $read" #ERR
$com.Close()
Found response: it return ERR wneh power is off.
When I powered on device over RS-232, script starts respond LE740E insteadof ERR
First time attempting to work with UDP. I'm harvesting lat/long from a NMEA string in a UDP broadcast. The code below works fine in the ISE but hangs up in the console. I'm running both with Admin rights. Other posts on the topic recommend dot-sourcing, no difference. I suspect it's something to do with how it's handling variables but have hit a wall. Thanks in advance for help.
##########################################################################
#
# Read $GPGGA string from UDP broadcast on port 40181
#
# Convert Degree-Minutes-Seconds format to decimal lat/long
#
# UDP code harvested from http://winpowershell.blogspot.com/2010/01/powershell-udp-clientserver.html
#
#
##########################################################################
# Form a connection point
$endpoint = New-Object system.net.ipendpoint([system.net.ipaddress]::Any,0)
Write-Host "Endpoint: " $endpoint
# Open UDP client
$udpclient = new-Object system.Net.Sockets.Udpclient(40181)
Write-Host "UDP client: " $udpclient
# Grab the datagram
$content = $udpclient.Receive([ref]$endpoint)
Write-Host "Content: " $content
# Put the datagram in $udpstring as type string
$udpstring = [Text.Encoding]::ASCII.GetString($content)
Write-Host "UDP string: " $udpstring
# Close UDP client
$udpclient.Close()
# Get Degree Min Sec format latitude from UDP string
# Explicit typing required in order to do math later
[Double]$DMSlatdeg = $udpstring.Substring(17,2)
$DMSlatmin = $udpstring.Substring(19,2)
# Get North South directional from UDP string
$directNS = $udpstring.Substring(29,1)
# Get Degree Min Sec format longitude from UDP string
# Explicit typing required in order to do math later
[Double]$DMSlongdeg = $udpstring.Substring(31,3)
$DMSlongmin = $udpstring.Substring(34,2)
# Get East West directional from UDP string
$directEW = $udpstring.Substring(44,1)
# Add decimal latitude minutes value to degrees
$declat = ($DMSlatdeg + ($DMSlatmin/60))
# Add decimal longitude minutes value to degrees
$declong = ($DMSlongdeg + ($DMSlongmin/60))
# Output formatted to three decimal places - variable value not changed
Write-Host "Decimal lat: " $declat.ToString("#.###") $directNS
Write-Host "Decimal long: " $declong.ToString("#.###") $directEW
In ISE I get the correct output:
Endpoint: 0.0.0.0:0
UDP client: System.Net.Sockets.UdpClient
Content: 36 71 80 71 71 65 44 [etc]
UDP string: $GPGGA,160516.13,[etc]
Decimal lat: [correct value] N
Decimal long: [correct value] W
In the console, it gets out the first two statements and hangs:
Endpoint: 0.0.0.0:0
UDP client: System.Net.Sockets.UdpClient
Is there something wrong with the way I'm setting $content?
I have batch file in which i am restarting a machine and want to check if machine is back online then execute the code block else after particular time inform the user the machine is not online
I want to check something like:
pingresult = ping \\machinename
if (pingresult == true)
{
execute some task
}
else
{
keep pinging for say 5min. if after 5mins machine is not up show message to user
}
This will try pinging %machinename% for 100 times with a default timeout of 3 seconds: 3s * 100 = 300s, i.e. 5 minutes.
for /L %%N IN (1, 1, 100) DO (
ping -n 1 %machinename%
if not ERRORLEVEL 1 (
set pingresult=true
goto done
)
)
set pingresult=false
:done
if %pingresult% == true (
echo "ping ok, doing something..."
) else (
echo "no reply after 5 mins, error!"
)