forked from mzbenami/training
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ping_test.py
64 lines (48 loc) · 1.79 KB
/
ping_test.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
import os
import sys
from netmiko import ConnectHandler
USERNAME = "cisco"
PASSWORD = "!cisco123!"
HOSTNAME = "68.170.147.164"
SNIPPET_DIR = 'snippets'
CHECKPOINT_NAME = 'my_checkpoint'
SUCCESS = 0
FAILURE = 1
def ping_is_successful(ping_result):
return True if '64 bytes' in ping_result else False
class ConnectivityTest:
def login(self):
self.cisco_switch = ConnectHandler(
device_type='cisco_nxos',
ip=HOSTNAME,
username=USERNAME,
password=PASSWORD,
verbose=False)
def setUp(self):
self.failure = False
self.login()
self.cisco_switch.send_command('checkpoint ' + CHECKPOINT_NAME)
def test_snippets(self):
for snippet_file in os.listdir(SNIPPET_DIR):
self.cisco_switch.send_config_from_file(os.path.join(SNIPPET_DIR, snippet_file))
ping_result = self.cisco_switch.send_command('ping 192.168.56.2')
print "=========================="
print snippet_file
print "--------------------------"
print ping_result
if not ping_is_successful(ping_result):
self.failure = True
def tearDown(self):
self.cisco_switch.send_command('rollback running-config checkpoint ' + CHECKPOINT_NAME)
self.cisco_switch.send_command('no checkpoint ' + CHECKPOINT_NAME)
self.cisco_switch.disconnect()
def run():
connTest = ConnectivityTest()
connTest.setUp() # Set up switch for testing by creating a checkpoint
connTest.test_snippets() # Apply all config snippets and run ping_test
connTest.tearDown() # Rollback to checkpoint created at start of testing and disconnect
if connTest.failure:
sys.exit(FAILURE)
sys.exit(SUCCESS)
if __name__ == "__main__":
run()