-
Notifications
You must be signed in to change notification settings - Fork 0
/
posmos.pl
304 lines (225 loc) · 7.49 KB
/
posmos.pl
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/perl
#
# pms - linksys/switch management system
#
# Copyright (C) 2011 Mathias Bøhn Grytemark
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
use warnings;
use strict;
use Net::Telnet::Cisco;
use Net::Ping;
###### SETTINGS BELOW HERE ######
# This is default settings for Linksys SRW2024
my $defip = '192.168.1.254';
my $defmask = '255.255.255.0';
my $defgw = '192.168.1.1';
my $defuser = 'admin';
my $defpass = '';
# Party settings
my $tftpserver = '10.20.30.1';
my $iosuser = 'iosuser';
my $iospass = 'iospass';
# This is per switch settings!
my %switch = (
name => 'sw1',
interface => 'gig 1/0/11',
core => '10.20.31.1',
defip => $defip,
defmask => $defmask,
defgw => $defgw,
tftpserver => $tftpserver,
iosuser => $iosuser,
iospass => $iospass,
defuser => $defuser,
defpass => $defpass,
mgmtvlan => '100',
realvlan => '101',
realgw => '10.20.32.1',
realmask => '255.255.255.0',
);
###### SETTINGS ABOVE HERE ######
sub prov
{
my %switch = @_;
print "\n";
print "Pinging default gw...\n";
my $ping = Net::Ping->new();
if ($ping->ping ($switch{defgw}, 5))
{
print "$switch{defgw} is already responding!\n";
return 0;
}
print "$switch{defgw} does not respond. Starting config...\n";
print "Connecting to core $switch{core}\n";
# FIXME: can probably remove _log after finishing...
my $ios = Net::Telnet::Cisco->new (
Host => $switch{core},
Errmode => 'return',
output_log => "out-core$switch{core}.log",
input_log => "in-core$switch{core}.log",
Prompt => '/\S+[#>]/',
);
if (!defined ($ios))
{
print "Could not connect to core $switch{core}\n";
return 0;
}
print "Connected to core $switch{core}... Logging in.\n";
$ios->login ($switch{iosuser}, $switch{iospass}) or die ("Login failed on core $switch{core}");
# With priv 15 we're already in enabled mode.. but just in case. (if using tacacs, the default setting puts you into unprivileged mode...)
$ios->enable;
# Smart to check somewhere on the way...
print $ios->errmsg;
# This does it easier... -- Mathias
$ios->cmd ("term length 0");
print "Configuring $switch{core} $switch{interface}\n";
$ios->cmd ("conf t");
$ios->cmd ("default int $switch{interface}");
$ios->cmd ("int $switch{interface}");
$ios->cmd ("no shut");
$ios->cmd ("description $switch{name} * config *");
$ios->cmd ("no switchport");
$ios->cmd ("ip address $switch{defgw} $switch{defmask}");
$ios->cmd ("end");
# Smart to check somewhere on the way...
print $ios->errmsg;
# Closing the connection for now.
$ios->close;
# Here starts the interesting part... Thanks to the TG Tech:Server-crew for doing this before me.
# We have to telnet to the gw to telnet to the (probably) linksys switch as it lacks a default route per now.
# Some copy and pasting from the $ios-part above here... Could probably made a function of it but what the heck.
tsleep (10);
my $new = Net::Telnet::Cisco->new (
Host => $switch{core},
Errmode => 'return',
output_log => "out-core$switch{core}.log",
input_log => "in-core$switch{core}.log",
Prompt => '/\S+[#>]/',
);
if (!defined ($new))
{
print "Could not connect to core $switch{core}\n";
return 0;
}
print "Connected to core $switch{core}... Logging in.\n";
$new->login ($switch{iosuser}, $switch{iospass}) or die ("Login failed on core $switch{core}");
$new->enable;
# Smart to check somewhere on the way...
print $new->errmsg;
print "Telnet'ing to switch $switch{defip}...\n";
# cmd() waits for return... seems print() is a better option.
$new->print ("telnet $switch{defip}");
# FIXME: this telnet-in-telnet needs some errorchecking... if it fails, we actually try to run the following commands on the core gw instead of on the stupid switch
# the commands I've used doesn't do any harm, as they fail on "conf" instead of "conf t" but it's not pretty! -- Mathias
# Okay... now we got a connection to the switch...
# The linksys telnet interface is a menu(!), but we can exit that after logging in.
print "Waiting for login prompt\n";
$new->waitfor ('/Password:/');
print "Entering username\n";
$new->print ("$switch{defuser}");
# Default for new linksys firmware is to login after admin\n
#$new->print ("$switch{defpass}\n");
# This is... ctrl+z !
# Exits from the menu to an "cli" from which we enter "lcli"...
$new->print ("\cZ");
print "Waiting for basic cli...\n";
$new->waitfor ("/\>/");
print "Got to the cli... Now start 'lcli'!\n";
$new->print ("lcli\n");
print "Waiting for username-prompt...\n";
$new->waitfor ("/User Name\:/");
print "Enter username $switch{defuser}\n";
$new->print ("admin");
print "Waiting for switch prompt\n";
$new->waitfor('/#/');
print "Got switch prompt...\n";
print "Setting default gateway..\n";
$new->cmd ("conf");
$new->cmd ("interface vlan 1");
$new->cmd ("ip address 192.168.1.254 255.255.255.0");
$new->cmd ("ip default-gateway 192.168.1.1");
$new->cmd ("exit");
$new->print ("exit");
print "Copying config for $switch{name} from $switch{tftpserver}...\n";
$new->print ("copy tftp://$switch{tftpserver}/base/$switch{name}.conf startup-config\n\n");
tsleep (10);
$new->cmd ("\n");
print "Reloading $switch{name}...\n";
$new->cmd ("reload");
$new->cmd ("reload");
$new->cmd ("reload");
sleep 1;
print "Replying 'y'..\n";
$new->cmd ("y");
$new->errmsg;
$new->close ();
print "Connecting to core $switch{core}\n";
undef $ios;
$ios = Net::Telnet::Cisco->new (
Host => $switch{core},
Errmode => 'return',
output_log => "out-core$switch{core}.log",
input_log => "in-core$switch{core}.log",
Prompt => '/\S+[#>]/',
);
if (!defined ($ios))
{
print "Could not connect to core $switch{core}\n";
return 0;
}
print "Connected to core $switch{core}... Logging in.\n";
$ios->login ($switch{iosuser}, $switch{iospass}) or die ("Login failed on core $switch{core}");
$ios->enable;
# Smart to check somewhere on the way...
print $ios->errmsg;
# This does it easier... Alfa
$ios->cmd ("term length 0");
print "Configuring $switch{core} $switch{interface}\n";
$ios->cmd ("conf t");
$ios->cmd ("default int $switch{interface}");
$ios->cmd ("int $switch{interface}");
$ios->cmd ("no shut");
$ios->cmd ("description $switch{name}");
$ios->cmd ("switchport trunk encap dot1q");
$ios->cmd ("switchport mode trunk");
$ios->cmd ("switchport trunk allowed vlan $switch{realvlan},$switch{mgmtvlan}");
$ios->cmd ("exit");
$ios->cmd ("vlan $switch{realvlan}");
$ios->cmd ("exit");
$ios->cmd ("no int vlan $switch{realvlan}");
$ios->cmd ("int vlan $switch{realvlan}");
$ios->cmd ("ip address $switch{realgw} $switch{realmask}");
$ios->cmd ("exit");
## FIXME: more config here!
$ios->cmd ("end");
$ios->cmd ("wr");
# Smart to check somewhere on the way...
print $ios->errmsg;
# Closing the connection for now.
$ios->close;
}
sub tsleep
{
my ($time) = @_;
while ($time)
{
print "$time...\n";
sleep (1);
$time--;
}
}
prov (%switch);