-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi
More file actions
executable file
·35 lines (27 loc) · 851 Bytes
/
i
File metadata and controls
executable file
·35 lines (27 loc) · 851 Bytes
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
#!/usr/bin/env python3
import re
import os
import sys
import argparse
def matches(hostname, a):
return all([word in hostname for word in a])
home = os.environ['HOME']
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--search", help="search and print matches", action="store_true")
parser.add_argument("pattern", nargs="?", default="")
args = parser.parse_args()
with open(os.path.join(home, ".inventory")) as f:
inventory = f.read().splitlines()
if args.pattern == "":
for hostname in inventory:
print(hostname)
exit(0)
match = re.search('([a-zA-Z\-]+)(\d+)?', args.pattern)
parts = list(filter(None, match.groups()))
for hostname in inventory:
if matches(hostname, parts):
if args.search:
print(hostname)
else:
os.system(f'ssh {hostname}')
break