-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakeinv.py
executable file
·115 lines (88 loc) · 3.21 KB
/
makeinv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python
"""Local network discovery inventory script
Generates an Ansible inventory of all hosts on a local network that
appear to be capable of incoming SSh connections (i.e. have port 22
open).
"""
import argparse
import json
import nmap
import socket
import time
class LocalNetworkInventory(object):
def __init__(self):
"""Main execution path"""
self.parse_cli_args()
data_to_print = ""
if self.args.host:
data_to_print = self.get_host_info()
else:
# Default action is to list instances, so we don't bother
# checking `self.args.list`.
#data_to_print = self.json_format_dict(self.get_inventory())
data_to_print = self.format_inventory(self.get_inventory())
#write the inventory file
with open('./inventory','w+') as finventory:
for line in data_to_print:
finventory.write(line+'\n')
def parse_cli_args(self):
"""Parse command-line arguments"""
parser = argparse.ArgumentParser(
description="Produce an Ansible Inventory file comprised of hosts on the local network"
)
parser.add_argument(
"--list",
action="store_true",
default=True,
help="List instances (default: True)"
)
parser.add_argument(
"--host",
action="store",
help="Get all the variables about a specific instance"
)
parser.add_argument(
"--connect-address",
metavar="ADDR",
action="store",
default="whiskerlabs.com",
help="A hostname or IP address to use in determining localhost's public IP"
)
self.args = parser.parse_args()
def get_inventory(self):
"""Populate `self.inventory` with hosts on the local network that
are accessible via SSH.
"""
return { "all": { "hosts": self.lookup_local_ips() }}
def json_format_dict(self, data):
return json.dumps(data, sort_keys=True, indent=2)
def format_inventory(self, data):
hosts = data['all']['hosts']
#print "hosts={}".format(hosts)
lines=[]
lines.append('[kube_host]')
firsthost = hosts[0]
for host in hosts[1:]:
lines.append("localmachine ansible_ssh_host='{}' ansible_connection=ssh ansible_ssh_user='pirate' ansible_ssh_pass='hypriot' ansible_ssh_pass='hypriot'".format(host))
lines.append('')
lines.append('[kube_master]')
lines.append("localmachine ansible_ssh_host='{}' ansible_connection=ssh ansible_ssh_user='pirate' ansible_ssh_pass='hypriot' ansible_ssh_pass='hypriot'".format(firsthost))
return lines
def get_local_routing_prefix(self):
"""Computes the local network routing prefix in CIDR notation."""
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.connect((self.args.connect_address, 80))
localhost_ip = sock.getsockname()[0]
sock.close()
octet_strs = localhost_ip.split('.')[:3]
octet_strs.append('0')
return '.'.join(octet_strs) + "/24"
def lookup_local_ips(self):
"""Lookup IPs of hosts connected to the local network"""
nm = nmap.PortScanner()
nm.scan(hosts=self.get_local_routing_prefix(), arguments="-p 22 --open")
return nm.all_hosts()
def get_host_info(self):
"""Get variables about a specific host"""
return self.json_format_dict({})
LocalNetworkInventory()