11
11
12
12
import vrnetlab
13
13
14
+ STARTUP_CONFIG_FILE = "/config/startup-config.cfg"
15
+
14
16
15
17
def handle_SIGCHLD (signal , frame ):
16
18
os .waitpid (- 1 , os .WNOHANG )
@@ -79,9 +81,7 @@ def create_boot_image(self):
79
81
cfg_file .write ("exit\r \n " )
80
82
cfg_file .write ("license accept end user agreement\r \n " )
81
83
cfg_file .write ("yes\r \n " )
82
- cfg_file .write (
83
- "do license install tftp://10.0.0.2/license.lic\r \n \r \n "
84
- )
84
+ cfg_file .write ("do license install tftp://10.0.0.2/license.lic\r \n \r \n " )
85
85
86
86
cfg_file .write ("platform console serial\r \n \r \n " )
87
87
cfg_file .write ("do clear platform software vnic-if nvtable\r \n \r \n " )
@@ -108,9 +108,7 @@ def bootstrap_spin(self):
108
108
self .start ()
109
109
return
110
110
111
- (ridx , match , res ) = self .tn .expect (
112
- [b"Press RETURN to get started!" ], 1
113
- )
111
+ (ridx , match , res ) = self .tn .expect ([b"Press RETURN to get started!" ], 1 )
114
112
if match : # got a match!
115
113
if ridx == 0 : # login
116
114
if self .install_mode :
@@ -122,13 +120,13 @@ def bootstrap_spin(self):
122
120
123
121
# run main config!
124
122
self .bootstrap_config ()
123
+ self .startup_config ()
124
+ self .running = True
125
125
# close telnet connection
126
126
self .tn .close ()
127
127
# startup time?
128
128
startup_time = datetime .datetime .now () - self .start_time
129
129
self .logger .info ("Startup complete in: %s" % startup_time )
130
- # mark as running
131
- self .running = True
132
130
return
133
131
134
132
# no match, if we saw some output from the router it's probably
@@ -152,8 +150,7 @@ def bootstrap_config(self):
152
150
153
151
self .wait_write ("hostname %s" % (self .hostname ))
154
152
self .wait_write (
155
- "username %s privilege 15 password %s"
156
- % (self .username , self .password )
153
+ "username %s privilege 15 password %s" % (self .username , self .password )
157
154
)
158
155
self .wait_write ("ip domain name example.com" )
159
156
self .wait_write ("crypto key generate rsa modulus 2048" )
@@ -172,6 +169,29 @@ def bootstrap_config(self):
172
169
self .wait_write ("copy running-config startup-config" )
173
170
self .wait_write ("\r " , None )
174
171
172
+ def startup_config (self ):
173
+ """Load additional config provided by user."""
174
+
175
+ if not os .path .exists (STARTUP_CONFIG_FILE ):
176
+ self .logger .trace (f"Startup config file { STARTUP_CONFIG_FILE } is not found" )
177
+ return
178
+
179
+ self .logger .trace (f"Startup config file { STARTUP_CONFIG_FILE } exists" )
180
+ with open (STARTUP_CONFIG_FILE ) as file :
181
+ config_lines = file .readlines ()
182
+ config_lines = [line .rstrip () for line in config_lines ]
183
+ self .logger .trace (f"Parsed startup config file { STARTUP_CONFIG_FILE } " )
184
+
185
+ self .logger .info (f"Writing lines from { STARTUP_CONFIG_FILE } " )
186
+
187
+ self .wait_write ("configure terminal" )
188
+ # Apply lines from file
189
+ for line in config_lines :
190
+ self .wait_write (line )
191
+ # End and Save
192
+ self .wait_write ("end" )
193
+ self .wait_write ("copy running-config startup-config" )
194
+
175
195
176
196
class CSR (vrnetlab .VR ):
177
197
def __init__ (self , hostname , username , password , nics , conn_mode ):
@@ -219,9 +239,7 @@ def install(self):
219
239
parser .add_argument ("--username" , default = "vrnetlab" , help = "Username" )
220
240
parser .add_argument ("--password" , default = "VR-netlab9" , help = "Password" )
221
241
parser .add_argument ("--install" , action = "store_true" , help = "Install CSR" )
222
- parser .add_argument (
223
- "--hostname" , default = "csr1000v" , help = "Router Hostname"
224
- )
242
+ parser .add_argument ("--hostname" , default = "csr1000v" , help = "Router Hostname" )
225
243
parser .add_argument ("--nics" , type = int , default = 9 , help = "Number of NICS" )
226
244
parser .add_argument (
227
245
"--connection-mode" ,
0 commit comments