From 57190d1a28b07668459c7da244f2e5d40bde1654 Mon Sep 17 00:00:00 2001 From: coderick14 Date: Wed, 14 Jun 2017 15:54:40 +0530 Subject: Add script to scan RPi's --- new_cron_job.sh | 5 +++-- sbhs_server/create_ip_map.py | 23 +++++++++++++++++++++++ sbhs_server/scan_rpis.sh | 11 +++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 sbhs_server/create_ip_map.py create mode 100644 sbhs_server/scan_rpis.sh diff --git a/new_cron_job.sh b/new_cron_job.sh index f757e20..aa55092 100644 --- a/new_cron_job.sh +++ b/new_cron_job.sh @@ -10,11 +10,12 @@ cd $DIR killall streamer source ../bin/activate #source ./bin/activate -python sbhs_server/scan_machines.py +#python sbhs_server/scan_machines.py +bash sbhs_server/scan_rpis.sh #python offline_reconnect.py python manage.py makemigrations python manage.py migrate -python manage.py initialize_machines +#python manage.py initialize_machines python manage.py generate_checksum touch index.wsgi python manage.py log_generator diff --git a/sbhs_server/create_ip_map.py b/sbhs_server/create_ip_map.py new file mode 100644 index 0000000..2964be0 --- /dev/null +++ b/sbhs_server/create_ip_map.py @@ -0,0 +1,23 @@ +import json + +pi_ip_map = {} +ipaddrs = [] + +with open("temp/ipaddrs.txt", "r") as filehandler: + ipaddrs = filehandler.readlines() + +ipaddrs = [ip.strip() for ip in ipaddrs] + +for ip in ipaddrs: + filename = "temp/" + ip + ".txt" + with open(filename, "r") as filehandler: + for line in filehandler: + try: + data = line.split('=') + pi_ip_map[data[0].strip()] = ip + except: + pass + +json_data = json.dumps(pi_ip_map, indent=2, separators=(',',':')) +with open("pi-ip.json", "w") as filehandler: + filehandler.write(json_data) \ No newline at end of file diff --git a/sbhs_server/scan_rpis.sh b/sbhs_server/scan_rpis.sh new file mode 100644 index 0000000..3a1fd3c --- /dev/null +++ b/sbhs_server/scan_rpis.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +mkdir -p temp +nmap -n -sP 10.42.0.255/24 | grep -E -o "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])" | grep -v "10.42.0.1" > temp/ipaddrs.txt + +while read -r ip +do + curl -o temp/$ip.txt $ip/map_machine_ids.txt +done < "temp/ipaddrs.txt" + +python create_ip_map.py \ No newline at end of file -- cgit