Click here to Skip to main content
14,937,163 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to ping an address using a Raspberry PI 3B, tried ping 192.168.0.1 from the command line, that appeared to respond. Now I need to get to work faster so I tried the below Python (I am far from expert) from SO it did not appear to work (surprise!) I have tried to get it to work, adding a 'w' to open error count file if it does not exist, creating ErrorCount.log on the desktop causes the script to limp rather than run... So am not creating the text file properly or am I pining (or ponging) wrong?

What I have tried:

Python
import socket

max_error_count = 10
error_count = 1


def increase_error_count():
    # Quick hack to handle false Port not open errors
    with open('ErrorCount.log') as f:
        for line in f:
            error_count = line
    error_count = int(error_count)
    print ("Error counter: " + str(error_count))
    file = open('ErrorCount.log', 'w')
    file.write(str(error_count + 1))
    file.close()
    if error_count == max_error_count:
        # Send email, pushover, slack or do any other fancy stuff
        print ("Sending out notification")
        # Reset error counter so it won't flood you with notifications
        file = open('ErrorCount.log', 'w')
        file.write('0')
        file.close()

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(2) 
result = sock.connect_ex(('192.168.0.1',80))
if result == 0:
        print ("Port is open")
else:
        print ("Port is not open")
        increase_error_count()
Posted
Updated 17-Aug-18 0:45am
Comments
Richard MacCutchan 17-Aug-18 6:21am
   
That is not a ping. You are trying to connect to the HTTP server on some local machine. If there is no server listening on that port then the connect will fail.
glennPattonContracting2 17-Aug-18 6:35am
   
Ahh, good point... So I am using the wrong command
Jochen Arndt 17-Aug-18 6:38am
   
I'm not a Python expert too.

You are not pinging at all. You are trying to connect to port 80 which is usually a HTTP server which fails if there is no such service running. You might print the result value then to know why. If it succeeds, you should close the socket.

If you need a real (ICMP) ping, see for example GitHub - samuel/python-ping: Pure Python version of ICMP ping.
glennPattonContracting2 17-Aug-18 6:47am
   
I'm really an Embedded Guy, but occasionally I have to play with higher level stuff
which where the trouble starts...
Jochen Arndt 17-Aug-18 7:02am
   
For an application developer, sockets belong to the lowest level APIs.

But I understand what you mean using embedded system and microcontrollers too.

1 solution

Having further google I have found this
Python
def pingComputer():

  import os
  hostname = input("Enter the ip address: ")
  response = os.system("ping -c 1 " + str(hostname))
  if response == 0:
    print hostname, 'is up!'
  else:
    print hostname, 'is down!

Will this perform the Ping?
Oh Yes it Will!!
   
v2
Comments
Richard MacCutchan 17-Aug-18 9:20am
   
Yes of course it will. But why not just type the ping command from the terminal screen?
Richard MacCutchan 17-Aug-18 10:53am
   
I wonder if you realise that ...
... the above code is a bit like writing a telephone number on a piece of paper. Then folding the paper four or five times and putting it in a sealed envelope. Then passing the envelope to a co-worker, and asking him to open the envelope and read the number. He should then ask another co-worker to call the number to see if anyone answers.

Whereas typing the ping directly into a command shell is like dialling the number yourself.
glennPattonContracting2 17-Aug-18 11:17am
   
Yes, but the whole point is to see if the thing is up or down don't really care any more than that the whole point of Pinging them is to prevent them powering down, if they see a 'ping' they won't power off for two minutes. As this controls the Power line to another circuit which is being tested, the unit going to low power was an extra pain.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900