Joerg - 2019-08-16

Hi

when you need the VLAN / Port-Shutdown function for Brocade/Ruckus devices:

Change the vlanchange.pl like this:

  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
#!/usr/bin/perl
##########################################################################
# Change the vlan on a switchport
# Author: Jonathan Yantis <yantisj@gmail.com>
# Copyright (C) 2013 Jonathan Yantis
##########################################################################
# 
# About: This script takes in a switchname, port, data vlan and an optional
# voice vlan and makes this change on a cisco switch.
#
# Note: If you want to use this from the web interface, make sure the apache
# user has access to this script and can write to /var/log/netdb/vlanchange.log
#
##########################################################################
# License:
# 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:
# http://www.gnu.org/licenses/gpl.txt
#
# 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
###########################################################################
use NetDB;
use NetDBHelper;
use Getopt::Long;
use English qw( -no_match_vars );
use Net::DNS;
use IO::Socket::INET;
use strict;
use warnings;

# no nonsense
no warnings 'uninitialized';

my $config_file = "/etc/netdb.conf";
my $DEBUG          = 0;

# Program Variables
my ( $logfile, $ssh_session, $use_ssh, $use_telnet, $user );

$user = "CLI";

# Options
my ( $optVlan, $optDesc, $optShutNoShut );

# Input Options
if ( $ARGV[0] eq "" ) { &usage(); }

GetOptions(
    'vl=s'   => \$optVlan,
    'ds=s'   => \$optDesc,
    'u=s'    => \$user,
    'sns=s'  => \$optShutNoShut,
    'conf=s' => \$config_file,
    'v'      => \$DEBUG,

          )
or &usage();

&parseConfig();

#$DEBUG = 5;

# Load config file
altHelperConfig( $config_file, $DEBUG );

# VLAN Switch Option
if ( $optVlan ) {
    vlanSwitch( $optVlan );
}
elsif ( $optDesc ) {
    changeDesc( $optDesc );
}
elsif ( $optShutNoShut ) {
    shutNoShut( $optShutNoShut );
}

# Shut no shut by default, if switch,port,status where status is:
# status = shut, skip no shut
# status = noshut, skip shut
sub shutNoShut {

    my $input = shift;
    my $session;
    my $success = 1;
    my @results;
    my @cmdresults;

    my ( $host, $port, $status ) = split( /\,/, $input );

    # Check for port, if no description remove description from port
    if ( $port =~ /\// ) {

        logMessage( "Getting Session on $host" ) if $DEBUG;
        $session = connectDevice( $host );

        logMessage( "$status on $host,$port" ) if $DEBUG;

    # SSH
        if ( $ssh_session ) {
            @cmdresults = SSHCommand( $session, "configure terminal" );
            @cmdresults = SSHCommand( $session, "interface ethernet $port" );

        # Skip when noshut is issued
        if ( $status ne 'noShut' ) {
        @cmdresults = SSHCommand( $session, "disable" );
        sleep 5;
        }
          # 
        if ( $status ne 'shut' ) {
        @cmdresults = SSHCommand( $session, "enable" );
            }
        logMessage( "Successfully $status $host $port" );
    }

    # Telnet
    else {
        @cmdresults = $session->cmd( String => "configure terminal" );
            @cmdresults = $session->cmd( String => "interface ethernet $port" );
            @cmdresults = $session->cmd( String => "disable" );
        sleep 5;
            @cmdresults = $session->cmd( String => "disable" );

            if ( $cmdresults[0] =~ /Invalid/i ) {
                logErr( "Shut no Shut Error on $host $port: @cmdresults" );
                $success = undef;
            }

            if ( $success ) {
                logMessage( "Successfully shut no shut $host $port" );
            }

    }
    }
}

sub changeDesc {
    my $input = shift;
    my $session;
    my $success = 1;
    my @results;
    my @cmdresults;

    my ( $host, $port, $desc ) = split( /\,/, $input );

    $desc =~ s/^\"(.*)\"$/$1/g;

    print "Sanitized Description: $desc\n" if $DEBUG;

    # Check for port, if no description remove description from port
    if ( $port =~ /\// ) {

    logMessage( "Getting Session on $host" ) if $DEBUG;
        $session = connectDevice( $host );

    logMessage( "Changing Description on $host,$port" ) if $DEBUG;

    if ( $ssh_session ) {
        @cmdresults = SSHCommand( $session, "configure terminal" );
        @cmdresults = SSHCommand( $session, "interface ethernet $port" );

        # Add a description
        if ( $desc ) {
        @cmdresults = SSHCommand( $session, "port-name $desc" );
        }

        # Remove existing description
        else {
        @cmdresults = SSHCommand( $session, "no port-name" );
        }

        if ( $cmdresults[0] =~ /Invalid/i ) {
        logErr( "Description Change Error on $host $port $desc: @cmdresults" );
        $success = undef;
        }
        # Save Config
        @cmdresults = SSHCommand( $session, "end" );
        @cmdresults = SSHCommand( $session, "wr mem\n\n" );

        if ( $cmdresults[0] =~ /Invalid/i ) {
        logErr( "Error saving configuration on $host: @cmdresults" );
        $success = undef;
        }

        if ( $success ) {
        logMessage( "Successfully changed $host $port description to $desc" );
        }
    }

    # Telnet
    else {
        @cmdresults = $session->cmd( String => "configure terminal" );
            @cmdresults = $session->cmd( String => "interface ethernet $port" );
            @cmdresults = $session->cmd( String => "description $desc" );

            if ( $cmdresults[0] =~ /Invalid/i ) {
                logErr( "Description Change Error on $host $port: @cmdresults" );
                $success = undef;
            }
            # Save Config                                                                                                                                                                          
            @cmdresults = $session->cmd( String => "end" );
            @cmdresults = $session->cmd( String => "wr mem\n\n" );

            if ( $success ) {
                logMessage( "Successfully changed $host $port description to $desc" );
            }
    }
    }
}

## Change VLAN on switchport string "switch,port,vlan_id";
sub vlanSwitch {
    my $input = shift;
    my $session;
    my $success = 1;
    my @results;
    my @cmdresults;

    my ( $host, $port, $dVlan, $vVlan ) = split( /\,/, $input );

    # Input sanity check
    if ( $host && $port && $dVlan =~ /\d+/ ) {

    # Get a session on device
    logMessage( "Getting Session on $host" ) if $DEBUG;
    $session = connectDevice( $host );

    if ( $session ) {

        logMessage( "Changing VLAN on $host" ) if $DEBUG;

        if ( $ssh_session ) {
        @cmdresults = SSHCommand( $session,  "configure terminal" );
        @cmdresults = SSHCommand( $session,  "interface ethernet $port" );
        @cmdresults = SSHCommand( $session,  "no dual-mode" );
        @cmdresults = SSHCommand( $session,  "dual-mode $dVlan" );

        if ( $cmdresults[0] =~ /Invalid/i ) {
            logErr( "Vlan Change Error on $host $port $dVlan: @cmdresults" );
            $success = undef;
        }

        # Voice Vlan Option
        if ( $vVlan ) {
            @cmdresults = SSHCommand( $session,  "voice $vVlan" );
        }

        logMessage( "Saving Config on $host" ) if $DEBUG;

        # Save Config
        @cmdresults = SSHCommand( $session,  "end" );
        @cmdresults = SSHCommand( $session,  "wr mem\n\n" );

        if ( $cmdresults[0] =~ /Invalid/i ) {
                    logErr( "Error saving configuration on $host: @cmdresults" );
            $success = undef;
                }

        if ( $success ) {
            logMessage( "Success: Changed $host $port to VLAN $dVlan,$vVlan for $user" );
        }

        }
        else {

        logMessage( "Changing VLAN on $host" ) if $DEBUG;

                @cmdresults = $session->cmd( String => "configure terminal" );
                @cmdresults = $session->cmd( String => "interface ethernet $port" );
                @cmdresults = $session->cmd( String => "dual-mode $dVlan" );

        # Voice Vlan Option
                if ( $vVlan ) {
                    @cmdresults = $session->cmd( String => "voice $vVlan" );
                }

        logMessage( "Saving Config on $host" ) if $DEBUG;

        # Save Config
        @cmdresults = $session->cmd( String => "end" );
                @cmdresults = $session->cmd( String => "wr mem\n\n" );

                if ( $success ) {
                    logMessage( "Successfully changed $host $port to VLAN $dVlan,$vVlan for $user" );
                }

        }

    }

    # No session returned
    else {
        logErr( "Failed to get session on $host for vlan change" ); 
    }

    }
    else {
    logErr( "Input Error for VLAN Change: $input\n" );
    }

}

# Get a session on a device
# Checks to see if telnet or ssh is enabled, tries to login and get a $session
sub connectDevice {
    my $session;
    my $fqdn = shift;
    my $ssh_enabled;
    my $pid = $$;

    print "PID($pid): Connecting to $fqdn using SSH($use_ssh) Telnet($use_telnet)...\n" if $DEBUG>1;

    ## Test to see if SSH port is open
    if ( $use_ssh ) {
        print "PID($pid): Testing port 22 on $fqdn for open state\n" if $DEBUG>1;

        my $hostip;

        # Get IP Address
        eval {
            $hostip = inet_ntoa(inet_aton($fqdn));
            print "IP for $fqdn:\t$hostip\n\n" if $DEBUG>1;
        };

        # Create a Socket Connection
        my $remote = IO::Socket::INET -> new (
             Proto => "tcp",
             Timeout => 2,
             PeerAddr => $hostip,
             PeerPort => "22" );

        # Set $ssh_enabled if port is open
        if ($remote) { 
            close $remote;
            $ssh_enabled = 1;
            print "PID($pid): $fqdn SSH port open\n" if $DEBUG>1;
        }
        else {
            print "PID($pid): $fqdn SSH port closed\n" if $DEBUG>1;
        }
    }

    # Attempt SSH Session, return 0 if failure and print to stderr
    if ( $ssh_enabled ) {

        $EVAL_ERROR = undef;
        eval {
            $session = get_cisco_ssh_auto( $fqdn );

            $ssh_session = 1
        };

        if ($EVAL_ERROR || !$session) {
            die "PID($pid): |ERROR|: Could not open SSH session to $fqdn: $EVAL_ERROR\n";
        }

        return $session;
    }
    # Fallback to Telnet if allowed
    elsif ( $use_telnet )  {
        print "PID($pid): Could not SSH to $fqdn on port 22, trying telnet\n" if $DEBUG && $use_ssh;

        # Attempt Session, return 0 if failure and print to stderr
        $EVAL_ERROR = undef;
        eval {
            $session = get_cisco_session_auto($fqdn);
            $session->cmd( String => "terminal length 0" ); # nx-os fix
        };

        if ($EVAL_ERROR || !$session) {
            die "PID($pid): |ERROR|: Could not open a telnet session to $fqdn: $EVAL_ERROR\n";
        }

        return $session;
    }

    # Failed to get a session, report back
    else {
        die "PID($pid): |ERROR|: Failed to get a session on $fqdn, no available connection methods\n";
    }
}

## Log message to STDOUT and to a logfile
#  Pass in either a single message or an array
sub logMessage {

    my $message = shift;
    my $date = localtime;

    # Open logfile
    open( my $LOG, '>>', "$logfile" ) or die "Can't log to $logfile: $!\n";

    while ( $message ) {

        chomp $message;
        print $message . "\n";
        print $LOG "$date: $message\n"; 

        # Implement file based logging here

        $message = shift; # Get next log message if array
    }

    close $LOG;
}

## Log errors differently (Still to STDOUT for now)
sub logErr {
    my $message = shift;
    my $date = localtime;

    # Open logfile
    open( my $LOG, '>>', "$logfile" ) or die "Can't log to $logfile: $!\n";

    while ( $message ) {

        chomp $message;
        print "VLAN Change ERROR: " . $message . "\n";
        print $LOG "$date: VLAN Change ERROR: $message\n";

        # Implement file based logging here

        $message = shift; # Get next log message if array
    }

    close $LOG;
}

## Parse the config file and populate script options from config file
sub parseConfig {
    # Create any variables that do not exist to avoid errors
    my $config = AppConfig->new({
                                 CREATE => 1,
                                });

    $config->define( "vlan_log=s", "use_ssh", "use_telnet" );
    $config->file( "$config_file" );

    $logfile     = $config->vlan_log();
    $use_ssh     = $config->use_ssh();
    $use_telnet  = $config->use_telnet();

    if ( !$logfile ) {
    die "Error: Could not enable logging, make sure vlan_log is defined in $config_file\n";
    }

}

## Output the usage info
sub usage {

    print <<USAGE;

  About: Changes the VLAN on switchports
  Usage: vlanchange [options]

   -vl  switch,port,vlan[,voice_vlan]  Change the vlan on a port
   -ds  switch,port,description        Change the description on a port
   -sns switch,port                    Shut/no shut port
   -conf                               Alternate Config File
   -v                                  Verbose

USAGE
    exit;
}

Make sure that you run a firmware version below 8.0.6 on the device, because they have change the "dual-mode" settings - that will no longer work.

I have only edited the command of Cisco to that one what is used for the Brocade / Ruckus Switches.

Additional add in the /etc/netdb-cgi.conf

shutnoshut_script = /opt/netdb/vlanchange.pl -sns
shut_script = /opt/netdb/vlanchange.pl -sns
noshut_script = /opt/netdb/vlanchange.pl -sns

For testing purpose change the default login level to 3 or add you / the current user to the Level3_access group.

Make sure that the user who runs the apache service have write access to the log file of the /etc/netdb.conf -> "vlan_log = /yourpathtothelog