Need to build a ping test for work network - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Networking (https://python-forum.io/forum-12.html) +--- Thread: Need to build a ping test for work network (/thread-16603.html) |
Need to build a ping test for work network - austina2 - Mar-06-2019 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 RE: Need to build a ping test for work network - searching1 - Mar-08-2019 @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. RE: Need to build a ping test for work network - austina2 - Mar-11-2019 (Mar-08-2019, 09:03 PM)searching1 Wrote: @austina2 Hi, 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. |