Python Forum
Need to build a ping test for work network
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need to build a ping test for work network
#1
This is my firs time posting and i am going to be as thorough as possible

my boss has requested that i make a python program on a raspberry pi (running raspbian), the parameters he requested are these:

the program should ping various network devices every 5 minutes
if the ping succeeds, send a signal to one of the GPIO pins
if the ping fails, send a signal to one of the GPIO pins

that signal will be sent to another device which will initiate an alarm.

I have come up with something based on internet searches, but i keep running into road blocks, and have a lot of questions...

here is the code i have:

=========================================
import ipaddress
import socket
import os


with open('test_list.txt') as fname:
           sw = fname.read().splitlines()
for s in sw:
    response = os.system('ping -n 2' + s)
    if response == 0:
               print(s, 'is up!')
    else:
               print(s, 'is Down!')
========================================


right now, i am getting this error, "if response= == 0 name "response" is not defined."

is it smart to run this based on a .txt file? i thought it would be good in case we want to add or remove IPs.

i am also wondering how to then take that output of the ping and make it trigger a high or low signal on the pin.

Sorry if this post broke all the rules... thanks in advance
Reply
#2
@austina2 Hi,

You can use similar with this method, instead of using os.system. you can try doing a print if there's an output from os.system + command.... though used that long time ago and I forgot.

You can try using os.popen. since i run this on linux distri. i used different ping command, but might be the same output from you ping -n(this is in windows it think.
import os

ipadd = "8.8.8.8"
up = "0%"

poll = os.popen("ping -c 2 " + ipadd).readlines()
print ("\n Output: \n",poll)

#Strip        
strpoll = [test.strip() for test in poll]
#Convert as String
spoll = ''.join(strpoll)


if up in spoll:
   print("\n Result: It's UP")
else:
   print("\n Result: Down")
Result:
python3 ping.py 

 Output: 
 ['PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.\n', '64 bytes from 8.8.8.8: icmp_seq=1 ttl=123 time=1.24 ms\n', '64 bytes from 8.8.8.8: icmp_seq=2 ttl=123 time=0.926 ms\n', '\n', '--- 8.8.8.8 ping statistics ---\n', '2 packets transmitted, 2 received, 0% packet loss, time 1001ms\n', 'rtt min/avg/max/mdev = 0.926/1.083/1.240/0.157 ms\n']

 Result: It's UP

you can enhance this.
Reply
#3
(Mar-08-2019, 09:03 PM)searching1 Wrote: @austina2 Hi,

You can use similar with this method, instead of using os.system. you can try doing a print if there's an output from os.system + command.... though used that long time ago and I forgot.

You can try using os.popen. since i run this on linux distri. i used different ping command, but might be the same output from you ping -n(this is in windows it think.


you can enhance this.




I appreciate the response, but i am having trouble following exactly what you're suggesting. Looks like a similar program only tweaked for a different OS.

I would appreciate some clarification as i am still trying to figure this out.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ping program in python Skaperen 2 3,245 Jun-23-2018, 03:32 PM
Last Post: Skaperen
  TCP ping time mesure diffrence from other tools tbaror 2 3,322 Dec-25-2017, 08:57 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020