Bottom Page

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
Quote
#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.
Quote
#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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Issues in Chat App for Local Network iMuny 1 165 Jul-19-2019, 04:11 AM
Last Post: micseydel
  Server and Network (socket) [WinError 10053] Timxxx 0 114 Jul-17-2019, 11:50 PM
Last Post: Timxxx
  creation of an application for lora sever network moez 3 309 Apr-18-2019, 11:05 AM
Last Post: buran
  Server and Network (socket) [WinError 10053] SheeppOSU 2 541 Apr-13-2019, 09:23 PM
Last Post: SheeppOSU
  Assistance with Python Network script cscecela 2 822 Feb-24-2019, 01:23 AM
Last Post: searching1
  A virtual network to use with python to learn about networking with python? marienbad 0 310 Feb-11-2019, 09:20 PM
Last Post: marienbad
  Run interpreter from Network? JP_ROMANO 3 619 Nov-14-2018, 02:50 AM
Last Post: wavic
  Multiple network socket servers? MuntyScruntfundle 1 498 Nov-13-2018, 03:46 PM
Last Post: wavic
  safe output in a file for all switches in a network henry1077 0 644 Jul-26-2018, 01:05 PM
Last Post: henry1077
  Batch network configuration/ verification task using Python anonyfate 1 788 Jul-03-2018, 04:30 AM
Last Post: buran

Forum Jump:


Users browsing this thread: 1 Guest(s)