Skip to content

Commit d12c464

Browse files
authored
Create rpi-dectector.py
1 parent 64114d8 commit d12c464

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

rpi-dectector.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import subprocess
2+
import os
3+
import platform
4+
import re
5+
6+
7+
HOST_VARS = {}
8+
ANSIBLE_INV = {}
9+
rpi_ip_list = []
10+
rpi_name_list = []
11+
12+
def nmap():
13+
t = subprocess.Popen("nmap -sP 10.0.0.0/24",shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
14+
t.wait()
15+
16+
def pi_search():
17+
#print ('Searching for RPi')
18+
#print ("---------------------------")
19+
p = subprocess.Popen("arp -a | cut -f 2,4 -d ' ' ", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
20+
for line in p.stdout.readlines():
21+
if line[-18:].startswith('b8:27:eb'):#eth0 mac : b8:27:eb
22+
ip_is = str(re.findall( r'[0-9]+(?:\.[0-9]+){3}',line))[2:-2]
23+
rpi_ip_list.append(ip_is)
24+
#print ("This is RPi IP: " + ip_is)
25+
#print ("---------------------------")
26+
def var_gen_host():
27+
for i in range(len(rpi_ip_list)):
28+
rpi_name_list.append("rpi"+str(i))
29+
HOST_VARS[rpi_name_list[i]] = {}
30+
for k in range(len(rpi_name_list)):
31+
HOST_VARS[rpi_name_list[i]]['ansible_ssh_host']=rpi_ip_list[k]
32+
33+
return (HOST_VARS)
34+
35+
def var_gen_inv():
36+
ANSIBLE_INV ["rpi"]={}
37+
ANSIBLE_INV ["rpi"]["hosts"]=rpi_name_list
38+
ANSIBLE_INV ["rpi"]["vars"] = {
39+
"ansible_ssh_user": "root",
40+
"ansible_ssh_private_key_file":"~/.ssh/dd_wrt"
41+
}
42+
return (ANSIBLE_INV)
43+
44+
45+
def var_gen():
46+
var_gen_host()
47+
var_gen_inv()
48+
49+
50+
def run():
51+
pi_search()
52+
53+
if rpi_ip_list == []:
54+
nmap()
55+
pi_search()
56+
57+
def main():
58+
pi_search()
59+
60+
if rpi_ip_list == []:
61+
print ('Runing nmap')
62+
nmap()
63+
pi_search()
64+
65+
var_gen()
66+
print ("HOST_VARS")
67+
print (HOST_VARS)
68+
print ("ANSIBLE_INV")
69+
print (ANSIBLE_INV)
70+
#return (HOST_VARS,ANSIBLE_INV)
71+
72+
73+
if __name__ == "__main__":
74+
main()

0 commit comments

Comments
 (0)