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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
|
#!/usr/bin/perl
use strict;
use warnings;
use Net::Telnet::Cisco;
use Net::Ping;
use Term::ANSIColor;
use threads;
use threads::shared;
use Thread::Queue;
use Getopt::Long;
use Net::IP;
use Net::OpenSSH;
BEGIN {
use File::Basename;
my $dlink_dir = dirname(__FILE__);
require "$dlink_dir/dlink-ng-config.pm";
}
# Make sure dlinkconfig.pm loads config (i.e. one config type has been uncommented)
die("No config type specified. Uncomment the wanted subroutine in the config file.\n") unless ($dlinkng::config::cisco_user);
# Stuff
my $switchq = Thread::Queue->new(); # Queue to put switches in
my $switches : shared = 0; # Number of successful switches
my $failed_switches : shared = 0; # Number of failed switches
my $total_time : shared = 0; # Total time spent for all switches
my %telnet_sessions : shared; # Number of sessions currently in use
my $DLINK_TEMPLATE; # Filehandle used for reading D-Link template
# Autoflush
$| = 1;
# Get options
my ($cisco_config, $single_switch, $dlink_config, $last_port_config, $last_port_desc);
if (@ARGV > 0) {
GetOptions(
'c|cisco|ciscoconfig' => \$cisco_config, # Configure on the Cisco-side only (Portchannel, interfaces, etc)
's|switch=s' => \$single_switch, # Configure a single switch
'd|dlink|dlinkconfig=s' => \$dlink_config, # Push D-Link-template-config to D-Link (not used @ TG)
'lastport' => \$last_port_config, # Configure the last port only (Cisco-side)
'lastportdesc' => \$last_port_desc, # Configure the description on the last port (Cisco-side)
)
}
# Exit if D-Link template file doesn't exist
if ($dlink_config){
unless(-e $dlink_config){
die("File '$dlink_config' does not exists. Aborting.\n");
}
}
# Exit if $cisco_config and $last_port_config or $last_port_desc is set
if ($cisco_config && ($last_port_config || $last_port_desc)){
die("\$cisco_config and \$last_port_config (or \$last_port_desc) can't be used together.\n");
}
# If $last_port_desc, assume $last_port_config
if ($last_port_desc){
$last_port_config = 1;
}
# Exit if we give $last_port_config or $last_port_desc parameters, but $configure_last_port is not set
if ($last_port_config || $last_port_desc){
unless($dlinkng::config::configure_last_port){
die("\$configure_last_port not set, but expected due to either \$last_port_config or \$last_port_desc.\n");
}
}
# Print stuff
sub log_it{
my ($logtype, $color, $switchname, $msg) = @_;
printf ("%-38s %s\n", colored("$logtype", "$color") . "/" . colored("$switchname", "bold") . ":", "$msg");
}
# INFO-logs
sub info{
my ($switchname, $msg) = @_;
log_it("INFO", "blue", $switchname, $msg);
return 1;
}
# ERROR-logs
sub error{
my ($switchname, $msg) = @_;
printf ("%-38s %s\n", colored("ERROR", "red") . "/" . colored("$switchname", "bold") . ":", "$msg");
return 0;
}
# Debug from Net::Telnet::Cisco and similar
sub debug{
my ($switchname, $errmsg) = @_;
if ($errmsg){
foreach my $line (split('\\n', $errmsg)){
error($switchname, $line);
}
}
}
# Abort
sub abort{
my ($switchname, $t1, $t2) = @_;
$t1->close if defined($t1);
$t2->close if defined($t2);
return error($switchname, "Aborting.");
}
# Set coreswos
sub set_coreswos{
my $coreswip = shift;
my $os = $dlinkng::config::default_coreswos;
foreach my $swos ( sort keys %$dlinkng::config::os_regex ){
$os = $swos if ($coreswip =~ m/$dlinkng::config::os_regex->{$swos}/);
}
return $os;
}
# Is normal IOS?
sub is_ios{
my $coreswos = shift;
return 1 if ($coreswos =~ m/^ios$/i);
return 0;
}
# Is NX-OS?
sub is_nx{
my $coreswos = shift;
return 1 if ($coreswos =~ m/^nx$/i);
return 0;
}
# Is IOS-XR?
sub is_xr{
my $coreswos = shift;
return 1 if ($coreswos =~ m/^xr$/i);
return 0;
}
# Cisco-ping
sub cisco_ping{
my ($cisco, $ip, $timeout, $coreswos, $vrf) = @_;
my $cmd;
if (is_nx($coreswos)){
$cmd = "ping $ip count 1 timeout 1";
$cmd .= " vrf ${dlinkng::config::vrf_prefix}${vrf}" if ($vrf && defined($vrf));
} elsif (is_xr($coreswos)){
# IOS-XR
$cmd = "ping";
$cmd .= " vrf ${dlinkng::config::vrf_prefix}${vrf}" if ($vrf && defined($vrf));
$cmd .= " $ip count 1 timeout 1";
} else {
# IOS
$cmd = "ping";
$cmd .= " vrf ${dlinkng::config::vrf_prefix}${vrf}" if ($vrf && defined($vrf));
$cmd .= " $ip repeat 1 timeout 1";
}
my $pong = 0;
my $tries = 0;
while (($pong == 0) && ($tries < $timeout)){
my @res = $cisco->cmd($cmd);
# if blank, try again
# happens at least on ME3600X
next unless @res;
# sleep if it complains about no valid source address
# % VRF does not have a usable source address
# this is caused by no link, or network not propagated
# observed on WS-C6506-E with sup720 running 122-33.SXJ5
sleep 1 if ("@res" =~ m/VRF does not have a usable source address/);
if (is_nx($coreswos)){
$pong = 1 if ($res[1] =~ m/^64 bytes from $ip/i);
} else {
$pong = 1 if ($res[-1] =~ m/^\s*Success rate is 100 percent/i);
}
$tries++;
}
return $pong;
}
# Net::Ping-ping
sub pong{
my ($ip, $timeout) = @_;
my $pong = 0;
my $tries = 0;
while(($pong == 0) && ($tries < $timeout)){
my $p = Net::Ping->new();
if ($p->ping($ip, 5)) {
$pong = 1;
}
$tries++;
}
return $pong;
}
# Create login to D-Link
sub dlink_login{
my ($switch, $ip, $vrf, $telnet_source) = @_;
my $dlink = Net::Telnet::Cisco->new(
Host => $switch->{coreswip} . $dlinkng::config::domain_name,
Errmode => 'return',
output_log => "$dlinkng::config::log_dir/dlink-output-$switch->{coreswip}-$switch->{switchname}.log",
input_log => "$dlinkng::config::log_dir/dlink-input-$switch->{coreswip}-$switch->{switchname}.log",
Prompt => '/\S+[#>]/',
Timeout => 60
);
unless (defined($dlink)) {
return error($switch->{switchname}, "Could not connect to '$switch->{coreswip}'.");
}
info($switch->{switchname}, "Logging in to coreswitch '$switch->{coreswip}' to telnet to D-Link.");
unless ($dlink->login($dlinkng::config::cisco_user, $dlinkng::config::cisco_pass)){
$dlink->close;
return error($switch->{switchname}, "Can't log in to coreswitch '$switch->{coreswip}' (to telnet to D-Link).");
}
# Don't do enable on IOS-XR
unless(is_xr($switch->{coreswos})){
$dlink->enable;
debug($switch->{switchname}, $dlink->errmsg);
}
my $cmd;
if (is_nx($switch->{coreswos})){
# NX-OS
$cmd = "telnet $ip";
$cmd .= " vrf ${dlinkng::config::vrf_prefix}${vrf}" if ($vrf && defined($vrf));
$cmd .= " source $telnet_source" if ($telnet_source && defined($telnet_source));
} elsif (is_xr($switch->{coreswos})){
# IOS-XR
$cmd = "telnet ";
$cmd .= "vrf ${dlinkng::config::vrf_prefix}${vrf} " if ($vrf && defined($vrf));
$cmd .= "$ip";
$cmd .= " source-interface $telnet_source" if ($telnet_source && defined($telnet_source));
} else {
# IOS
$cmd = "telnet $ip";
$cmd .= " /vrf ${dlinkng::config::vrf_prefix}${vrf}" if ($vrf && defined($vrf));
$cmd .= " /source-interface $telnet_source" if ($telnet_source && defined($telnet_source));
}
info($switch->{switchname}, "Telneting to D-Link.");
$dlink->print($cmd);
info($switch->{switchname}, "Waiting for login prompt.");
$dlink->waitfor('/User ?Name:/')
or return abort($switch->{switchname}, $dlink);
info($switch->{switchname}, "Got login prompt, logging in.");
telnet_print($switch, $dlink, $dlinkng::config::dlink_def_user, 0)
or return abort($switch->{switchname}, $dlink);
info($switch->{switchname}, "Waiting for prompt.");
$dlink->waitfor('/\S+\#/')
or return abort($switch->{switchname}, $dlink);
# disable CLI paging
telnet_print($switch, $dlink, "disable clipaging")
or return abort($switch->{switchname}, $dlink);
info($switch->{switchname}, "Logged in to D-Link");
return $dlink;
}
# Execute telnet-command
sub telnet_cmd{
my ($switch, $telnet, $cmd) = @_;
unless ($telnet->cmd($cmd)){
error($switch->{switchname}, "Command '$cmd' failed");
debug($switch->{switchname}, $telnet->errmsg);
if($cmd =~ m/commit/){
# If commit on IOS-XR failed, print the reason
if(is_xr($switch->{coreswos})){
# Redundant check, but whatever
my @failed = $telnet->cmd("show configuration failed");
if(@failed){
foreach my $line (@failed){
chomp($line);
error($switch->{switchname}, $line);
}
}
}
}
return 0;
}
return 1;
}
# Execute telnet-print
sub telnet_print{
my ($switch, $telnet, $cmd, $waitfor) = @_;
unless (defined($waitfor)){
$waitfor = 1;
}
unless ($telnet->print($cmd)){
error($switch->{switchname}, "Command '$cmd' failed.");
debug($switch->{switchname}, $telnet->errmsg);
return 0;
}
if ($waitfor){
$telnet->waitfor('/\S+\#/') or return 0;
}
return 1;
}
# Make sure interface actually is shut
sub no_no_shut{
my ($switch, $cisco, $port) = @_;
info($switch->{switchname}, "Making sure interface $port /REALLY/ gets shut.");
# There is a few reasons as to why we want to do this.
# On some boxes/OSes, default config for an interface is 'no shut'.
# On some boxes/OSes, the 'shut' command isn't applied right away after
# you do a 'shut'.
my $return = 0;
if(is_xr($switch->{coreswos})){
# don't need to do any checks on XR
telnet_cmd($switch, $cisco, "shut")
or return abort($switch->{switchname}, $cisco);
$return = 1;
} else {
# on all other OS
my $tries = 0;
while (1){
if($tries >= 5){
# max 5 tries
last;
}
sleep 1; # wait a bit
telnet_cmd($switch, $cisco, "shut")
or return abort($switch->{switchname}, $cisco);
# now we need to check that the 'running config' actually reflects this
my @shut_info = $cisco->cmd("do sh run int $port | i shutdown");
unless(@shut_info){
$tries++;
next;
}
my $shut = "@shut_info";
chomp($shut);
if ($shut =~ m/shutdown/i){
$return = 1;
last;
} else {
$tries++;
next;
}
}
}
return $return;
}
# Reset all interfaces
sub reset_interfaces{
my ($switch, $cisco) = @_;
# Take down interface
info($switch->{switchname}, "Resetting interfaces.");
telnet_cmd($switch, $cisco, "conf t")
or return abort($switch->{switchname}, $cisco);
# Remove old config/return to defaults on interfaces
# We also shut them to avoid D-Link looping
foreach my $port (@{$switch->{ports}}){
telnet_cmd($switch, $cisco, "default int $port")
or return abort($switch->{switchname}, $cisco);
telnet_cmd($switch, $cisco, "int $port")
or return abort($switch->{switchname}, $cisco);
no_no_shut($switch, $cisco, $port)
or return abort($switch->{switchname}, $cisco);
unless(is_xr($switch->{coreswos})){
# only do this if not IOS-XR
telnet_cmd($switch, $cisco, "no switchport")
or return abort($switch->{switchname}, $cisco);
}
}
# Remove portchannel
if(is_ios($switch->{coreswos})){
# IOS fails on the next command, if the portchannel doesn't exist
# Ignore error on this command
$cisco->cmd("no int Po$switch->{etherchannel}");
} elsif(is_xr($switch->{coreswos})){
# Other syntax on XR, called Bundle-Ether
telnet_cmd($switch, $cisco, "no int Bundle-Ether$switch->{etherchannel}")
or return abort($switch->{switchname}, $cisco);
} else {
# Other OS, that doesnt fail
telnet_cmd($switch, $cisco, "no int Po$switch->{etherchannel}")
or return abort($switch->{switchname}, $cisco);
}
# Commit on XR
if(is_xr($switch->{coreswos})){
telnet_cmd($switch, $cisco, "commit")
or return abort($switch->{switchname}, $cisco);
}
telnet_cmd($switch, $cisco, "end")
or return abort($switch->{switchname}, $cisco);
return 1;
}
# Set up single interface
sub configure_interface{
my ($switch, $cisco, $port, $ip, $mask, $vrf) = @_;
# Configure port
telnet_cmd($switch, $cisco, "int $port")
or return abort($switch->{switchname}, $cisco);
unless(is_xr($switch->{coreswos})){
# only do this if not IOS-XR
telnet_cmd($switch, $cisco, "no switchport")
or return abort($switch->{switchname}, $cisco);
}
# Only do VRF-config if $vrf is defined
if($vrf && defined($vrf)){
if(is_nx($switch->{coreswos})){
# No error-check on the next command, as NX-OS
# spits out "% Deleted all L3 config on interface Ethernet1/45"
# making Net::Telnet::Cisco think it's an error
$cisco->cmd("vrf member ${dlinkng::config::vrf_prefix}${vrf}");
} elsif(is_xr($switch->{coreswos})){
# IOS-XR
telnet_cmd($switch, $cisco, "vrf ${dlinkng::config::vrf_prefix}${vrf}")
or return abort($switch->{switchname}, $cisco);
} else {
# IOS
telnet_cmd($switch, $cisco, "ip vrf forwarding ${dlinkng::config::vrf_prefix}${vrf}")
or return abort($switch->{switchname}, $cisco);
}
}
# Add IP
info($switch->{switchname}, "Adding IP-address to interface.");
if(is_xr($switch->{coreswos})){
# IOS-XR
telnet_cmd($switch, $cisco, "ipv4 address $ip $mask")
or return abort($switch->{switchname}, $cisco);
} else {
# All other
telnet_cmd($switch, $cisco, "ip address $ip $mask")
or return abort($switch->{switchname}, $cisco);
}
# 'no shut'-patrol reporting in!
telnet_cmd($switch, $cisco, "no shut")
or return abort($switch->{switchname}, $cisco);
# Commit on XR
if(is_xr($switch->{coreswos})){
telnet_cmd($switch, $cisco, "commit")
or return abort($switch->{switchname}, $cisco);
}
telnet_cmd($switch, $cisco, "end")
or return abort($switch->{switchname}, $cisco);
return 1;
}
# Push D-Link template config to a D-Link
sub push_dlink_template_config{
my $switch = shift; # switchinfo
# No need to bounce via Cisco-box; login directly to D-Link
my $dlink = Net::Telnet::Cisco->new(
Host => $switch->{ipv4address},
Errmode => 'return',
output_log => "$dlinkng::config::log_dir/dlink-output-$switch->{coreswip}-$switch->{switchname}.log",
input_log => "$dlinkng::config::log_dir/dlink-input-$switch->{coreswip}-$switch->{switchname}.log",
Prompt => '/\S+[#>]/',
Timeout => 60
);
unless (defined($dlink)) {
return error($switch->{switchname}, "Could not connect to '$switch->{switchname}' ($switch->{ipv4}).");
}
info($switch->{switchname}, "Logging in to D-Link '$switch->{switchname}' ($switch->{ipv4address}).");
$dlink->waitfor('/User ?Name:/')
or return abort($switch->{switchname}, $dlink);
info($switch->{switchname}, "Got login prompt, logging in.");
telnet_print($switch, $dlink, $dlinkng::config::dlink_def_user, 0)
or return abort($switch->{switchname}, $dlink);
info($switch->{switchname}, "Waiting for prompt.");
$dlink->waitfor('/\S+\#/')
or return abort($switch->{switchname}, $dlink);
# disable CLI paging
telnet_print($switch, $dlink, "disable clipaging")
or return abort($switch->{switchname}, $dlink);
info($switch->{switchname}, "Logged in to D-Link");
# Done logging in, let's configure stuff
info($switch->{switchname}, "Opening D-Link template file ($dlink_config).");
open $DLINK_TEMPLATE, '<', $dlink_config or return error($switch->{switchname}, "Couldn't open D-Link template ($dlink_config): $!");
info($switch->{switchname}, "Applying config from D-Link template file ($dlink_config).");
while (my $line=<$DLINK_TEMPLATE>) {
chomp $line;
next if ($line =~ m/^\s*(((#|\!).*)|$)/); # skip if comment, or blank line
telnet_print($switch, $dlink, $line)
or return abort($switch->{switchname}, $dlink);
sleep 1; # The D-Link's are a bit slow...
}
close $DLINK_TEMPLATE or return error($switch->{switchname}, "Couldn't close D-Link template ($dlink_config): $!");
info($switch->{switchname}, "Done applying config from D-Link template file ($dlink_config). Saving...");
# Save config + logout
telnet_print($switch, $dlink, "save", 0)
or return abort($switch->{switchname}, $dlink);
telnet_print($switch, $dlink, "Y")
or return abort($switch->{switchname}, $dlink);
telnet_print($switch, $dlink, "logout")
or return abort($switch->{switchname}, $dlink);
# Done
log_it("SUCCESS", "green", $switch->{switchname}, "Done pushing D-Link template config to switch $switch->{switchname} ($switch->{ipv4address}). \\o/");
$dlink->close;
return 1;
}
# Setup a switch
sub setup{
my $switch = shift; # switchinfo
my $vrf = threads->tid(); # use thread ID as VRF-number
# Remove last port if we're skipping it
my $last_port;
if ($dlinkng::config::configure_last_port){
# assume we want to skip a port, so we check against regex
if($switch->{switchname} =~ m/$dlinkng::config::last_port_regex/){
$last_port = pop(@{$switch->{ports}});
}
}
if($last_port_config){
info($switch->{switchname}, "Configuring last port only.");
}
if($cisco_config){
info($switch->{switchname}, "Configuring things on the Cisco-side only.");
}
unless($cisco_config || $last_port_config){
info($switch->{switchname}, "Starting configuration of $switch->{switchname} ($switch->{ipv4address}).");
info($switch->{switchname}, "Trying to ping $switch->{ipv4address}.");
if (pong($switch->{ipv4address}, 1)){
# push template-config to D-Link if template-file is given as argument
if($dlink_config){
log_it("INFO", "green", $switch->{switchname}, "Switch $switch->{switchname} ($switch->{ipv4address}) is already responding to ping! \\o/");
info($switch->{switchname}, "Going to push D-Link template config to switch $switch->{switchname} ($switch->{ipv4address})");
return push_dlink_template_config($switch);
} else {
# if not
log_it("INFO", "green", $switch->{switchname}, "Skipping $switch->{switchname} ($switch->{ipv4address}): is already responding to ping! \\o/");
return 1;
}
}
info($switch->{switchname}, "Not responding to ping, configuring device.");
}
info($switch->{switchname}, "Connecting to coreswitch '$switch->{coreswip}'.");
my $cisco;
if ($dlinkng::config::use_ssh_cisco){
# Use SSH
my $ssh = Net::OpenSSH->new( $dlinkng::config::cisco_user . ':' .
$dlinkng::config::cisco_pass . '@' .
$switch->{coreswip} . $dlinkng::config::domain_name,
timeout => 60);
if ($ssh->error){
return debug($switch->{switchname}, $ssh->error);
}
my ($pty, $pid) = $ssh->open2pty({stderr_to_stdout => 1})
or return debug($switch->{switchname}, $ssh->error);
$cisco = Net::Telnet::Cisco->new(
Fhopen => $pty,
Telnetmode => 0,
#Cmd_remove_mode => 1,
Errmode => 'return',
output_log => "$dlinkng::config::log_dir/cisco-output-$switch->{coreswip}-$switch->{switchname}.log",
input_log => "$dlinkng::config::log_dir/cisco-input-$switch->{coreswip}-$switch->{switchname}.log",
Prompt => '/\S+[#>]/',
#Prompt => '/(?m:^\\s?(?:[\\w.\/]+\:)?(?:[\\w.-]+\@)?[\\w.-]+\\s?(?:\(config[^\)]*\))?\\s?[\$#>]\\s?(?:\(enable\))?\\s*$)/',
);
} else {
# Don't use SSH
$cisco = Net::Telnet::Cisco->new(
Host => $switch->{coreswip} . $dlinkng::config::domain_name,
Errmode => 'return',
output_log => "$dlinkng::config::log_dir/cisco-output-$switch->{coreswip}-$switch->{switchname}.log",
input_log => "$dlinkng::config::log_dir/cisco-input-$switch->{coreswip}-$switch->{switchname}.log",
Prompt => '/\S+[#>]/',
#Prompt => '/(?m:^\\s?(?:[\\w.\/]+\:)?(?:[\\w.-]+\@)?[\\w.-]+\\s?(?:\(config[^\)]*\))?\\s?[\$#>]\\s?(?:\(enable\))?\\s*$)/',
Timeout => 60
);
}
unless (defined($cisco)){
return error($switch->{switchname}, "Could not connect to '$switch->{coreswip}'.");
}
unless ($dlinkng::config::use_ssh_cisco){
info($switch->{switchname}, "Logging in to coreswitch '$switch->{coreswip}'.");
unless ($cisco->login($dlinkng::config::cisco_user, $dlinkng::config::cisco_pass)){
$cisco->close;
return error($switch->{switchname}, "Can't log in to '$switch->{coreswip}'.");
}
}
# Don't do enable on IOS-XR
unless(is_xr($switch->{coreswos})){
$cisco->enable;
debug($switch->{switchname}, $cisco->errmsg);
}
# Disable paging
telnet_cmd($switch, $cisco, "terminal length 0")
or return abort($switch->{switchname}, $cisco);
unless($cisco_config || $last_port_config){
# Prepare ports
reset_interfaces($switch, $cisco)
or return abort($switch->{switchname}, $cisco);
# Set up port 1 for D-Link telneting
telnet_cmd($switch, $cisco, "conf t")
or return abort($switch->{switchname}, $cisco);
info($switch->{switchname}, "Enabling VRF (${dlinkng::config::vrf_prefix}${vrf}). Applying to interface.");
if(is_nx($switch->{coreswos})){
# Remove previous VRF
# No error-check on the next command, as NX-OS
# spits out "% VRF dlink-X not found"
# making Net::Telnet::Cisco think it's an error
# Extra overhead to delete, not used
#$cisco->cmd("no vrf context ${dlinkng::config::vrf_prefix}${vrf}");
#sleep 10; # NX-OS seems to need some time to delete a VRF
# Create VRF
telnet_cmd($switch, $cisco, "vrf context ${dlinkng::config::vrf_prefix}${vrf}")
or return abort($switch->{switchname}, $cisco);
} elsif(is_xr($switch->{coreswos})){
# IOS-XR
telnet_cmd($switch, $cisco, "vrf ${dlinkng::config::vrf_prefix}${vrf}")
or return abort($switch->{switchname}, $cisco);
} else {
# Default OS
# IOS fails on the next command, if the vrf doesn't exist
# Ignore error on this command
# Extra overhead to delete, not used
#$cisco->cmd("no ip vrf ${dlinkng::config::vrf_prefix}${vrf}");
# Create VRF
telnet_cmd($switch, $cisco, "ip vrf ${dlinkng::config::vrf_prefix}${vrf}")
or return abort($switch->{switchname}, $cisco);
telnet_cmd($switch, $cisco, "rd $vrf:$vrf")
or return abort($switch->{switchname}, $cisco);
}
# Configure port #1
configure_interface($switch, $cisco, @{$switch->{ports}}[0], $dlinkng::config::dlink_router_ip, $dlinkng::config::dlink_def_mask, $vrf)
or return abort($switch->{switchname}, $cisco);
info($switch->{switchname}, "Waiting for D-Link to answer on $dlinkng::config::dlink_def_ip in VRF ${dlinkng::config::vrf_prefix}${vrf} on port #1 (" . @{$switch->{ports}}[0] . ").");
# Use port 1 if ping succeeds
my $p_count = 0;
my $dlink_port = @{$switch->{ports}}[$p_count];
until (cisco_ping($cisco, $dlinkng::config::dlink_def_ip, 30, $switch->{coreswos}, $vrf)) {
# Did not ping, let's try next port
# We do this because port#1 might be damaged/not patched
# First we need to check if there are more ports to test
if ($p_count >= $#{$switch->{ports}}){
# Current port is last port available
# Since it did not ping, we reset all interfaces
reset_interfaces($switch, $cisco)
or return abort($switch->{switchname}, $cisco);
$cisco->close;
return error($switch->{switchname}, "No more ports on $switch->{switchname}. Aborting.");
}
# Use next port
$p_count++;
info($switch->{switchname}, "D-Link on port #" . ($p_count) . " ($dlink_port) did not respond. Trying next port (" . @{$switch->{ports}}[$p_count] . ").");
# Try next port, reset old port
telnet_cmd($switch, $cisco, "conf t")
or return abort($switch->{switchname}, $cisco);
telnet_cmd($switch, $cisco, "default int $dlink_port")
or return abort($switch->{switchname}, $cisco);
telnet_cmd($switch, $cisco, "int $dlink_port")
or return abort($switch->{switchname}, $cisco);
no_no_shut($switch, $cisco, $dlink_port)
or return abort($switch->{switchname}, $cisco);
# Set new port
$dlink_port = @{$switch->{ports}}[$p_count];
# Configure new port
configure_interface($switch, $cisco, $dlink_port, $dlinkng::config::dlink_router_ip, $dlinkng::config::dlink_def_mask, $vrf)
or return abort($switch->{switchname}, $cisco);
info($switch->{switchname}, "Waiting for D-Link to answer on $dlinkng::config::dlink_def_ip in VRF ${dlinkng::config::vrf_prefix}${vrf} on port #" . ($p_count+1) . " (" . @{$switch->{ports}}[$p_count] . ").");
}
# Telnet to D-Link
info($switch->{switchname}, "Starting D-Link config phase 1.");
my $dlink = dlink_login($switch, $dlinkng::config::dlink_def_ip, $vrf)
or return abort($switch->{switchname}, $cisco);
info($switch->{switchname}, "Setting hostname to $switch->{switchname}${dlinkng::config::dlink_host_suffix}");
telnet_print($switch, $dlink, "config snmp system_name $switch->{switchname}${dlinkng::config::dlink_host_suffix}")
or return abort($switch->{switchname}, $cisco, $dlink);
info($switch->{switchname}, "Adding LACP. Enabling STP.");
telnet_print($switch, $dlink, "create link_aggregation group_id 1 type lacp")
or return abort($switch->{switchname}, $cisco, $dlink);
telnet_print($switch, $dlink, "config link_aggregation group_id 1 ports 1:($dlinkng::config::dlink_lacp_start-$dlinkng::config::dlink_lacp_end)")
or return abort($switch->{switchname}, $cisco, $dlink);
telnet_print($switch, $dlink, "enable stp")
or return abort($switch->{switchname}, $cisco, $dlink);
info($switch->{switchname}, "STP enabled, logging in again.");
$dlink->close;
unless (cisco_ping($cisco, $dlinkng::config::dlink_def_ip, 60, $switch->{coreswos}, $vrf)) {
reset_interfaces($switch, $cisco)
or return abort($switch->{switchname}, $cisco);
$cisco->close;
return error($switch->{switchname}, "Can't login to $switch->{switchname} on $dlinkng::config::dlink_def_ip, aborting.");
}
$dlink = dlink_login($switch, $dlinkng::config::dlink_def_ip, $vrf)
or return abort($switch->{switchname}, $cisco);
info($switch->{switchname}, "Running STP config.");
telnet_print($switch, $dlink, "config stp version mstp")
or return abort($switch->{switchname}, $cisco, $dlink);
telnet_print($switch, $dlink, "config stp priority 61440 instance_id 0")
or return abort($switch->{switchname}, $cisco, $dlink);
telnet_print($switch, $dlink, "config stp ports 1:(1-44) edge true")
or return abort($switch->{switchname}, $cisco, $dlink);
telnet_print($switch, $dlink, "enable lldp")
or return abort($switch->{switchname}, $cisco, $dlink);
info($switch->{switchname}, "Setting IP address.");
telnet_print($switch, $dlink, "config ipif System ipaddress $switch->{ipv4address}/$dlinkng::config::dlink_prefix vlan default")
or return abort($switch->{switchname}, $cisco, $dlink);
info($switch->{switchname}, "Closing D-Link link.");
$dlink->close;
info($switch->{switchname}, "Configuring IP on gateway.");
telnet_cmd($switch, $cisco, "conf t")
or return abort($switch->{switchname}, $cisco);
telnet_cmd($switch, $cisco, "default int $dlink_port")
or return abort($switch->{switchname}, $cisco);
configure_interface($switch, $cisco, $dlink_port, $switch->{ipv4gateway}, $switch->{netmask})
or return abort($switch->{switchname}, $cisco);
# Wait for network convergence
info($switch->{switchname}, "Waiting for network to converge.");
unless (cisco_ping($cisco, $switch->{ipv4address}, 60, $switch->{coreswos})) {
reset_interfaces($switch, $cisco)
or return abort($switch->{switchname}, $cisco);
$cisco->close;
return error($switch->{switchname}, "Can't ping $switch->{switchname} on $switch->{ipv4address}, aborting.");
}
# Do it again!
info($switch->{switchname}, "D-Link config phase 2.");
$dlink = dlink_login($switch, $switch->{ipv4address}, '', $dlink_port) or return abort($switch->{switchname}, $cisco);
info($switch->{switchname}, "Setting default route on switch. Saving config.");
telnet_print($switch, $dlink, "create iproute default $switch->{ipv4gateway}")
or return abort($switch->{switchname}, $cisco, $dlink);
telnet_print($switch, $dlink, "save", 0)
or return abort($switch->{switchname}, $cisco, $dlink);
telnet_print($switch, $dlink, "Y")
or return abort($switch->{switchname}, $cisco, $dlink);
telnet_print($switch, $dlink, "logout")
or return abort($switch->{switchname}, $cisco, $dlink);
info($switch->{switchname}, "Closing D-Link link.");
$dlink->close;
}
unless($last_port_config){
# Configure final IOS stuff
info($switch->{switchname}, "Final IOS config phase. Setting up all interfaces + Port-Channel.");
# Reset interfaces
reset_interfaces($switch, $cisco)
or return abort($switch->{switchname}, $cisco);
telnet_cmd($switch, $cisco, "conf t")
or return abort($switch->{switchname}, $cisco);
# Portchannel needs to exist before we can assign interfaces to it
# This needs to be done on IOS-XE, but we do it on all
if(is_xr($switch->{coreswos})){
# Other syntax on XR, called Bundle-Ether
telnet_cmd($switch, $cisco, "int Bundle-Ether$switch->{etherchannel}")
or return abort($switch->{switchname}, $cisco);
} else {
telnet_cmd($switch, $cisco, "int Po$switch->{etherchannel}")
or return abort($switch->{switchname}, $cisco);
}
# Rest of the config
my $etherchan_desc;
foreach my $port (@{$switch->{ports}}){
telnet_cmd($switch, $cisco, "int $port")
or return abort($switch->{switchname}, $cisco);
unless(is_xr($switch->{coreswos})){
# only do this if not IOS-XR
telnet_cmd($switch, $cisco, "no switchport")
or return abort($switch->{switchname}, $cisco);
}
# 'no shut'-patrol reporting in!
telnet_cmd($switch, $cisco, "no shut")
or return abort($switch->{switchname}, $cisco);
telnet_cmd($switch, $cisco, "description D-Link $switch->{switchname}; RJ-45; 1G;")
or return abort($switch->{switchname}, $cisco);
# disable CDP if specified
unless($dlinkng::config::cdp_enable){
telnet_cmd($switch, $cisco, "no cdp enable")
or return abort($switch->{switchname}, $cisco);
}
# Assign to Etherchannel
if(is_xr($switch->{coreswos})){
# Other syntax on XR, called Bundle-Ether
telnet_cmd($switch, $cisco, "bundle id $switch->{etherchannel} mode passive")
or return abort($switch->{switchname}, $cisco);
} else {
telnet_cmd($switch, $cisco, "channel-group $switch->{etherchannel} mode passive")
or return abort($switch->{switchname}, $cisco);
}
# add port to descr
$etherchan_desc .= "$port; ";
}
if(is_xr($switch->{coreswos})){
# Other syntax on XR, called Bundle-Ether
telnet_cmd($switch, $cisco, "int Bundle-Ether$switch->{etherchannel}")
or return abort($switch->{switchname}, $cisco);
} else {
telnet_cmd($switch, $cisco, "int Po$switch->{etherchannel}")
or return abort($switch->{switchname}, $cisco);
}
telnet_cmd($switch, $cisco, "description D-Link $switch->{switchname}; $etherchan_desc")
or return abort($switch->{switchname}, $cisco);
if(is_xr($switch->{coreswos})){
# IOS-XR
telnet_cmd($switch, $cisco, "ipv4 address $switch->{ipv4gateway} $switch->{netmask}")
or return abort($switch->{switchname}, $cisco);
} else {
# All other
telnet_cmd($switch, $cisco, "ip address $switch->{ipv4gateway} $switch->{netmask}")
or return abort($switch->{switchname}, $cisco);
}
# IPv6-stuff
unless (is_nx($switch->{coreswos})){
# IOS + IOS-XR
telnet_cmd($switch, $cisco, "ipv6 enable")
or return abort($switch->{switchname}, $cisco);
}
telnet_cmd($switch, $cisco, "ipv6 address $switch->{ipv6address}")
or return abort($switch->{switchname}, $cisco);
# DHCP-relay/forwarding + MBD/Blazon-forwarding (broadcast)
if (is_nx($switch->{coreswos})){
# NX-OS
telnet_cmd($switch, $cisco, "ip dhcp relay address $dlinkng::config::dhcprelay4_pri")
or return abort($switch->{switchname}, $cisco);
# define secondary if present
if (defined($dlinkng::config::dhcprelay4_sec) && $dlinkng::config::dhcprelay4_sec){
telnet_cmd($switch, $cisco, "ip dhcp relay address $dlinkng::config::dhcprelay4_sec")
or return abort($switch->{switchname}, $cisco);
}
} elsif (is_xr($switch->{coreswos})){
# IOS-XR
telnet_cmd($switch, $cisco, "ipv4 helper-address vrf default $dlinkng::config::dhcprelay4_pri")
or return abort($switch->{switchname}, $cisco);
# define secondary if present
if (defined($dlinkng::config::dhcprelay4_sec) && $dlinkng::config::dhcprelay4_sec){
telnet_cmd($switch, $cisco, "ipv4 helper-address vrf default $dlinkng::config::dhcprelay4_sec")
or return abort($switch->{switchname}, $cisco);
}
} else {
# IOS
telnet_cmd($switch, $cisco, "ip helper-address $dlinkng::config::dhcprelay4_pri")
or return abort($switch->{switchname}, $cisco);
# define secondary if present
if (defined($dlinkng::config::dhcprelay4_sec) && $dlinkng::config::dhcprelay4_sec){
telnet_cmd($switch, $cisco, "ip helper-address $dlinkng::config::dhcprelay4_sec")
or return abort($switch->{switchname}, $cisco);
}
}
# Custom Portchannel-config
# Dynamically applied depending on OS
foreach my $cmd (@{$dlinkng::config::po_config->{$switch->{coreswos}}}){
telnet_cmd($switch, $cisco, $cmd)
or return abort($switch->{switchname}, $cisco);
}
# 'no shut'-patrol reporting in!
telnet_cmd($switch, $cisco, "no shut")
or return abort($switch->{switchname}, $cisco);
# Done with interface-config
# Apply DHCP-profiling if using IOS-XR
if (is_xr($switch->{coreswos})){
telnet_cmd($switch, $cisco, "dhcp ipv4")
or return abort($switch->{switchname}, $cisco);
telnet_cmd($switch, $cisco, "interface Bundle-Ether$switch->{etherchannel} proxy profile $dlinkng::config::dhcp4_proxyprofile")
or return abort($switch->{switchname}, $cisco);
if(defined($dlinkng::config::dhcp6_proxyprofile) && $dlinkng::config::dhcp6_proxyprofile){
# Do DHCPv6 proxy as well
telnet_cmd($switch, $cisco, "dhcp ipv6")
or return abort($switch->{switchname}, $cisco);
telnet_cmd($switch, $cisco, "interface Bundle-Ether$switch->{etherchannel} proxy profile $dlinkng::config::dhcp6_proxyprofile")
or return abort($switch->{switchname}, $cisco);
}
}
# Commit on XR
if(is_xr($switch->{coreswos})){
telnet_cmd($switch, $cisco, "commit")
or return abort($switch->{switchname}, $cisco);
}
telnet_cmd($switch, $cisco, "end")
or return abort($switch->{switchname}, $cisco);
}
# If we skipped last port at the start, we configure it now
if ($dlinkng::config::configure_last_port && $last_port){
if ($last_port_desc){
info($switch->{switchname}, "Configuring last port... (description only)");
} else {
info($switch->{switchname}, "Configuring last port...");
}
telnet_cmd($switch, $cisco, "conf t")
or return abort($switch->{switchname}, $cisco);
unless ($last_port_desc){
telnet_cmd($switch, $cisco, "default int $last_port")
or return abort($switch->{switchname}, $cisco);
}
telnet_cmd($switch, $cisco, "int $last_port")
or return abort($switch->{switchname}, $cisco);
telnet_cmd($switch, $cisco, "description AP \@ D-Link $switch->{switchname}; RJ-45; 1G;")
or return abort($switch->{switchname}, $cisco);
unless ($last_port_desc){
foreach my $cmd (@{$dlinkng::config::last_port_config->{$switch->{coreswos}}}){
telnet_cmd($switch, $cisco, $cmd)
or return abort($switch->{switchname}, $cisco);
}
# 'no shut'-patrol reporting in!
telnet_cmd($switch, $cisco, "no shut")
or return abort($switch->{switchname}, $cisco);
}
# Commit on XR
if(is_xr($switch->{coreswos})){
telnet_cmd($switch, $cisco, "commit")
or return abort($switch->{switchname}, $cisco);
}
telnet_cmd($switch, $cisco, "end")
or return abort($switch->{switchname}, $cisco);
}
# If only cisco-config
if($cisco_config){
log_it("CONFIG", "green", $switch->{switchname}, "Cisco-config relevant to switch '$switch->{switchname}' ($switch->{ipv4address}) done! \\o/");
}
# Check if all is OK, but not if configuring skipped port only
my $return = 0;
if ($last_port_config){
info($switch->{switchname}, "Done doing skipped port config. Not checking if anything is online.");
$return = 1;
} else {
if(pong($switch->{ipv4address}, 15)){
# pingable, OK
log_it("SUCCESS", "green", $switch->{switchname}, "Switch $switch->{switchname} ($switch->{ipv4address}) set up! \\o/");
$return = 1;
# push template-config to D-Link if template-file is given as argument
if($dlink_config){
info($switch->{switchname}, "Going to push D-Link template config to switch $switch->{switchname} ($switch->{ipv4address})");
$return = push_dlink_template_config($switch);
}
} else {
# Not pingable
if (cisco_ping($cisco, $switch->{ipv4address}, 60, $switch->{coreswos})) {
# we can reach from core, but not from other places, lets warn
log_it("ERROR", "red", $switch->{switchname}, "Switch $switch->{switchname} reachable only from core/distro.");
} else {
reset_interfaces($switch, $cisco)
or return abort($switch->{switchname}, $cisco);
$cisco->close;
return error($switch->{switchname}, "Can't ping $switch->{switchname} on $switch->{ipv4address}, aborting.");
}
}
}
if($dlinkng::config::save_config && !is_xr($switch->{coreswos})){
# save the cisco-config, but not if IOS-XR
info($switch->{switchname}, "Saving config on core-switch ($switch->{coreswip}).");
telnet_cmd($switch, $cisco, "write")
or return abort($switch->{switchname}, $cisco);
}
$cisco->close;
return $return;
}
# Process switches
sub process_switches {
while (my $switch = $switchq->dequeue()){
last if ($switch eq 'DONE'); # all done
my $time_start = time();
# Wait till there is sessions/VTYs available
while (1){
$telnet_sessions{$switch->{coreswip}} = 0 unless $telnet_sessions{$switch->{coreswip}};
$telnet_sessions{$switch->{coreswip}} += 2;
if ($telnet_sessions{$switch->{coreswip}} <= $dlinkng::config::os_info->{$switch->{coreswos}}{max_sessions}){
# lower or equal to max_sessions on current switch
# lets move on
last;
} else {
# we would exceed max_sessions on current switch
# we wait until there are free sessions
info($switch->{switchname}, "There are no more free sessions left on '$switch->{coreswip}' ($switch->{coreswos}). Waiting...");
$telnet_sessions{$switch->{coreswip}} -= 2;
sleep (int(rand(10)) + 10);
}
}
info($switch->{switchname}, "Number of sessions on switch '$switch->{coreswip}': $telnet_sessions{$switch->{coreswip}}.");
if (setup($switch)) {
# Count number of switches
$switches++;
} else {
log_it("FAILED", "red", $switch->{switchname}, "Configuring $switch->{switchname} failed.");
# Count failed switches
$failed_switches++;
}
# Remove session
$telnet_sessions{$switch->{coreswip}} -= 2;
# Summarize total time spent, used to calculate average per switch
$total_time += time() - $time_start;
}
# detach thread -- we're done
threads->detach;
}
# Let's start
my $time_start = time();
log_it("INFO", "yellow", "dlink-ng", "Starting dlink-ng with $dlinkng::config::max_threads threads...");
log_it("INFO", "yellow", "dlink-ng", "Configured to skip last port on all switches.") if $dlinkng::config::configure_last_port;
# Let's add all switches to the queue
while (<STDIN>){
next if /^(.*#|\s+$)/; # skip if comment, or blank line
my ($switchname, $coreswip, $etherchannel, $cidr, $ipv4address, $ipv4gateway, $ipv6address, @ports) = split;
# skip if less than 1 port is provided
if (scalar(@ports) < 1){
log_it("FAILED", "red", $switchname, "Switch '$switchname' had less than 1 port configured. Skipping.");
next;
}
# define OS of the coresw
my $coreswos = set_coreswos($coreswip);
# find netmask
my $netmask = Net::IP->new($cidr)->mask();
my %switch = (
switchname => $switchname,
coreswip => $coreswip,
coreswos => $coreswos,
etherchannel => $etherchannel,
ipv4address => $ipv4address,
ipv4gateway => $ipv4gateway,
netmask => $netmask,
ipv6address => $ipv6address,
ports => \@ports,
);
# Only configure a single switch?
if (defined $single_switch){
unless ($single_switch eq $switchname){
next;
}
}
# Add switch to queue
$switchq->enqueue(\%switch);
}
# Let the threads know when they're done
$switchq->enqueue("DONE") for (1..$dlinkng::config::max_threads);
# Start processing the queue
threads->create("process_switches") for (1..$dlinkng::config::max_threads);
# Wait till all threads is done
sleep 5 while (threads->list(threads::running));
# If 0 switches
my $runtime = time() - $time_start;
my $total_switches = $switches + $failed_switches;
if($total_switches == 0){
log_it("INFO", "yellow", "dlink-ng", "Finished!");
log_it("INFO", "yellow", "dlink-ng", "Crunched 0 switches in $runtime seconds.");
exit 1;
}
# Done
my $avg = sprintf("%.1f", $total_time / $total_switches);
log_it("INFO", "yellow", "dlink-ng", "Finished!");
log_it("INFO", "yellow", "dlink-ng", "Crunched $total_switches switches in $runtime seconds.");
log_it("INFO", "yellow", "dlink-ng", "$switches of these were successful, while $failed_switches failed.");
log_it("INFO", "yellow", "dlink-ng", "Average of $avg seconds per switch.");
|