-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcreate_eapi_conf.py
More file actions
executable file
·98 lines (79 loc) · 2.98 KB
/
create_eapi_conf.py
File metadata and controls
executable file
·98 lines (79 loc) · 2.98 KB
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
#!/usr/bin/python3
"""
DESCRIPTION
Script to create an eapi.conf file for use with pyeapi given an existing network configured within a block of management IP addresses (script will skip over any IP's it can't connect to). Script will get the hostname from each switch and build the eapi.conf file. Needs the switch username and password input. Assumes https as the transport for each switch.
Run the script using the following:
.\create_eapi_conf.py --addr 192.168.1.1 --num 20 --user cvpadmin --passwd arista123
This example will try to connect to all IP's from 192.168.1.1 to 192.168.1.20
"""
__author__ = 'marayson'
import argparse
import ssl
import socket, struct
from jsonrpclib import Server
try:
_create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
# Legacy Python that doesn't verify HTTPS certificates by default
pass
else:
# Handle target environment that doesn't support HTTPS verification
ssl._create_default_https_context = _create_unverified_https_context
parser = argparse.ArgumentParser()
parser.add_argument('--addr', required=True,
default='', help='First management IP address in range')
parser.add_argument('--num', required=True,
default='', help='Number of addresses to try from first IP address')
parser.add_argument('--user', required=True,
default='', help='Username to access the switches')
parser.add_argument('--passwd', required=True,
default='', help='Password to access the switches')
args = parser.parse_args()
addr = args.addr
num = args.num
user = args.user
passwd = args.passwd
def ip2long(ip):
"""
Convert an IP string to long
"""
packedIP = socket.inet_aton(ip)
return struct.unpack("!L", packedIP)[0]
def long2ip(long):
"""
Convert a long to an IP string
"""
stringIP = socket.inet_ntoa(struct.pack('!L', long))
return stringIP
def getNextIPAddress(ip):
"""
Returns the next IP address
"""
ip_address_long = ip2long(ip) + 1
ip = long2ip(ip_address_long)
return ip
# Find the home directory where the .eap.conf file is located
from os.path import expanduser
home = expanduser("~")
hosts = []
n = 1
config_list = []
socket.setdefaulttimeout(3)
file_object = open(home + "/.eapi.conf", 'x')
current_ip = addr
for i in range(int(num)):
api_url = "https://" + user + ":" + passwd + "@" + current_ip + "/command-api"
switch = Server(api_url)
try:
response = switch.runCmds(1, ["show hostname"])
hostname = response[0]["hostname"]
file_object.write("[connection:" + hostname + "]\n")
file_object.write("host: " + current_ip + "\n")
file_object.write("username: " + user + "\n")
file_object.write("password: " + passwd + "\n")
file_object.write("transport: https\n")
file_object.write("\n")
except socket.error as ERR:
print("Can't connect to " + current_ip)
current_ip = getNextIPAddress(current_ip)
file_object.close