-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.py
50 lines (38 loc) · 1.2 KB
/
misc.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
import socket
import subprocess
import keyring
import urllib3
from pynautobot import api
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
def nb_login():
nautobot = api(
url=keyring.get_password("nautobot_stage", "url"),
token=keyring.get_password(
"nautobot-stage", keyring.get_password("cas", "user") + "mfa"
),
)
nautobot.http_session.verify = False
return nautobot
def get_devicetype(devices: list):
nautobot = nb_login()
for i in devices:
print(f"{i} = ", nautobot.dcim.devices.get(name=i).device_type)
def get_v4(hosts: list):
# print IPv4 for host
for i in hosts:
v4 = socket.gethostbyname(i)
print(f"neighbor {v4};")
def get_v6(hosts: list):
# print IPv6 for host
for i in hosts:
bashCmd = ["host", i]
process = subprocess.Popen(bashCmd, stdout=subprocess.PIPE)
output, error = process.communicate()
d = output.decode("utf-8")
v6 = d.split("\n")[1].split()[4]
print(f"neighbor {v6};")
def get_host(hosts: list):
# print hostname from IPs
for i in hosts:
name = socket.gethostbyaddr(i)
print(name[0].split(".")[0])