5
5
import os
6
6
import re
7
7
import signal
8
- import subprocess
9
8
import sys
10
- import telnetlib
11
9
import time
12
10
13
11
import vrnetlab
14
12
13
+
15
14
def handle_SIGCHLD (signal , frame ):
16
15
os .waitpid (- 1 , os .WNOHANG )
17
16
17
+
18
18
def handle_SIGTERM (signal , frame ):
19
19
sys .exit (0 )
20
20
21
+
21
22
signal .signal (signal .SIGINT , handle_SIGTERM )
22
23
signal .signal (signal .SIGTERM , handle_SIGTERM )
23
24
signal .signal (signal .SIGCHLD , handle_SIGCHLD )
24
25
25
26
TRACE_LEVEL_NUM = 9
26
27
logging .addLevelName (TRACE_LEVEL_NUM , "TRACE" )
28
+
29
+
27
30
def trace (self , message , * args , ** kws ):
28
31
# Yes, logger takes its '*args' as 'args'.
29
32
if self .isEnabledFor (TRACE_LEVEL_NUM ):
30
33
self ._log (TRACE_LEVEL_NUM , message , args , ** kws )
34
+
35
+
31
36
logging .Logger .trace = trace
32
37
33
38
@@ -37,15 +42,15 @@ def __init__(self, username, password, install_mode=False):
37
42
if re .search (".qcow2$" , e ):
38
43
disk_image = "/" + e
39
44
40
- super (ASAv_vm , self ).__init__ (username , password , disk_image = disk_image , ram = 2048 )
45
+ super (ASAv_vm , self ).__init__ (
46
+ username , password , disk_image = disk_image , ram = 2048
47
+ )
41
48
self .nic_type = "e1000"
42
49
self .install_mode = install_mode
43
50
self .num_nics = 8
44
51
45
-
46
52
def bootstrap_spin (self ):
47
- """ This function should be called periodically to do work.
48
- """
53
+ """This function should be called periodically to do work."""
49
54
50
55
if self .spins > 300 :
51
56
# too many spins with no result -> give up
@@ -54,8 +59,8 @@ def bootstrap_spin(self):
54
59
return
55
60
56
61
(ridx , match , res ) = self .tn .expect ([b"ciscoasa>" ], 1 )
57
- if match : # got a match!
58
- if ridx == 0 : # login
62
+ if match : # got a match!
63
+ if ridx == 0 : # login
59
64
if self .install_mode :
60
65
self .logger .debug ("matched, ciscoasa>" )
61
66
self .wait_write ("" , wait = None )
@@ -80,7 +85,7 @@ def bootstrap_spin(self):
80
85
81
86
# no match, if we saw some output from the router it's probably
82
87
# booting, so let's give it some more time
83
- if res != b'' :
88
+ if res != b"" :
84
89
self .logger .trace ("OUTPUT: %s" % res .decode ())
85
90
# reset spins if we saw some output
86
91
self .spins = 0
@@ -89,10 +94,8 @@ def bootstrap_spin(self):
89
94
90
95
return
91
96
92
-
93
97
def bootstrap_config (self ):
94
- """ Do the actual bootstrap config
95
- """
98
+ """Do the actual bootstrap config"""
96
99
self .logger .info ("applying bootstrap configuration" )
97
100
self .wait_write ("" , None )
98
101
self .wait_write ("enable" , wait = "ciscoasa>" )
@@ -104,15 +107,17 @@ def bootstrap_config(self):
104
107
self .wait_write ("" , wait = "(config)#" )
105
108
self .wait_write ("aaa authentication ssh console LOCAL" )
106
109
self .wait_write ("aaa authentication enable console LOCAL" )
107
- self .wait_write ("username %s password %s privilege 15" % (self .username , self .password ))
110
+ self .wait_write (
111
+ "username %s password %s privilege 15" % (self .username , self .password )
112
+ )
108
113
self .wait_write ("interface Management0/0" )
109
114
self .wait_write ("nameif management" )
110
115
self .wait_write ("ip address 10.0.0.15 255.255.255.0" )
111
116
self .wait_write ("no shutdown" )
112
117
self .wait_write ("ssh 0.0.0.0 0.0.0.0 management" )
113
118
self .wait_write ("ssh version 2" )
114
119
self .wait_write ("ssh key-exchange group dh-group14-sha256" )
115
- self .wait_write ("crypto key generate ecdsa" )
120
+ self .wait_write ("crypto key generate ecdsa" )
116
121
self .wait_write ("write" )
117
122
self .wait_write ("end" )
118
123
self .wait_write ("\r " , None )
@@ -121,15 +126,15 @@ def bootstrap_config(self):
121
126
class ASAv (vrnetlab .VR ):
122
127
def __init__ (self , username , password ):
123
128
super (ASAv , self ).__init__ (username , password )
124
- self .vms = [ ASAv_vm (username , password ) ]
129
+ self .vms = [ASAv_vm (username , password )]
125
130
126
131
127
132
class ASAv_installer (ASAv ):
128
- """ ASAv installer
129
- """
133
+ """ASAv installer"""
134
+
130
135
def __init__ (self , username , password ):
131
136
super (ASAv , self ).__init__ (username , password )
132
- self .vms = [ ASAv_vm (username , password , install_mode = True ) ]
137
+ self .vms = [ASAv_vm (username , password , install_mode = True )]
133
138
134
139
def install (self ):
135
140
self .logger .info ("Installing ASAv" )
@@ -141,13 +146,16 @@ def install(self):
141
146
self .logger .info ("Installation complete" )
142
147
143
148
144
- if __name__ == ' __main__' :
149
+ if __name__ == " __main__" :
145
150
import argparse
146
- parser = argparse .ArgumentParser (description = '' )
147
- parser .add_argument ('--trace' , action = 'store_true' , help = 'enable trace level logging' )
148
- parser .add_argument ('--username' , default = 'vrnetlab' , help = 'Username' )
149
- parser .add_argument ('--password' , default = 'VR-netlab9' , help = 'Password' )
150
- parser .add_argument ('--install' , action = 'store_true' , help = 'Install ASAv' )
151
+
152
+ parser = argparse .ArgumentParser (description = "" )
153
+ parser .add_argument (
154
+ "--trace" , action = "store_true" , help = "enable trace level logging"
155
+ )
156
+ parser .add_argument ("--username" , default = "vrnetlab" , help = "Username" )
157
+ parser .add_argument ("--password" , default = "VR-netlab9" , help = "Password" )
158
+ parser .add_argument ("--install" , action = "store_true" , help = "Install ASAv" )
151
159
args = parser .parse_args ()
152
160
153
161
LOG_FORMAT = "%(asctime)s: %(module)-10s %(levelname)-8s %(message)s"
0 commit comments