Find the answer to your Linux question:
Results 1 to 7 of 7
hello I’m trying to write bash script that need to do telnet session to some switches and change the port VLAN to the end user desired VLAN ID. I know ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jan 2012
    Posts
    21

    bash script for auto VLAN assignment


    hello


    I’m trying to write bash script that need to do telnet session to some switches and change the port VLAN to the end user desired VLAN ID.
    I know how to write the most of the script but I have problem with few things, we have more than 30 VLAN our network, each vlan is sometimes class B 255.255.0.0 and sometimes 4 “C” classes 255.255.252.0 or one “C” class 255.255.255.0.
    My goal is to let the user to write the desired ip + switch ip+ port number, then the script automatically will change the VLAN ID on the specific port.
    My problem is how to assign the vlan ip to the vlan id, I mean if the user write:
    Ip 10.7.4.100 , port 10 , switch-ip 10.7.4.1
    For example:
    How I can tell to the script that if the user wroth ip 10.7.4.x or 10.7.5.x or 10.7.6.x or 10.7.7.x then assign it to VLAN 11.
    Or if the user wroth desired ip 10.7.20.x or 10.7.21.x than assign the port to VLAN 20.

    I hope that you understand my question.

    If I’m not clear enough please let me know.

    Thanks

  2. #2
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    226
    hi,

    I don't understand quite well, use a case statement:
    Code:
    $ ip="10.7.4.10"
    $ case $ip in
       10.7.[4-7].*) vlan=11;;
       10.7.2[01].*) vlan=20;;
    esac
    $ echo "VLAN_$vlan = $ip"
    VLAN_11 = 10.7.4.10

  3. #3
    Just Joined!
    Join Date
    Jan 2012
    Posts
    21
    Quote Originally Posted by watael View Post
    hi,

    I don't understand quite well, use a case statement:
    Code:
    $ ip="10.7.4.10"
    $ case $ip in
       10.7.[4-7].*) vlan=11;;
       10.7.2[01].*) vlan=20;;
    esac
    $ echo "VLAN_$vlan = $ip"
    VLAN_11 = 10.7.4.10
    i will try it, thank you very much.

    hi, i tried to do it like that:

    echo " please enter your ip"
    read ip
    case $ip in

    10.7.[4-7].*) vlan=11;;
    10.7.[8-11].*) vlan=12;;
    #10.7.[12-15].*) vlan=14;;
    esac
    echo "VLAN_$vlan = $ip"


    but just the first case is working, i mean just 10.7.[4-7].*) vlan=11;;
    its look like the script didn't understand the range in the other statments.
    if i write it without range, like this: 10.7.8.*) vlan=12;; , its working fine, there is some problem with the range.

    also, i tried it like this:
    echo " please enter your ip"
    read ip
    case $ip in

    10.7.[4,5,6,7].*) vlan=11;;
    10.7.[8,9,10,11].*) vlan=12;;
    10.7.[12,13,14,15].*) vlan=14;;
    10.135.*.*) vlan=15;;
    esac
    echo "VLAN_$vlan = $ip"


    and now, the third case statement doesn't work, 10.7.[12,13,14,15].*) vlan=14;;
    whats the problem with Case? it can't calculate range after range?


    thanks
    Last edited by meirh; 01-01-2013 at 07:11 AM.

  4. $spacer_open
    $spacer_close
  5. #4
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,294
    Hi.
    Quote Originally Posted by meirh
    10.7.[12,13,14,15]
    Code:
           case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac
                  A case command first expands word, and tries to match it against
                  each pattern in turn, using the same matching rules as for
                  pathname expansion
    and
    Code:
    Pathname Expansion
    ...
           The special pattern characters have the following meanings:
    
           *      Matches any string, including the null string.
           ?      Matches any single character.
           [...]  Matches any one of the enclosed characters.  A pair of
                  characters separated by a hyphen denotes a range expression; any
                  character that sorts between those two characters, inclusive,
                  using the current locale's collating sequence and character set,
                  is matched.
    Both excerpts above are from man bash.

    Essentially you are trying to use whole strings in the [ .. ] construct. While it is syntactically correct, the semantics are not what you think. You need to focus on using single characters inside the [ ... ]

    You can use the alternation symbol (i.e. "|"), however, to allow choices among strings in a case selector.

    With extglob set, you can create more exotic expressions, as noted at patterns in case statement in bash scripting - Stack Overflow

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  6. #5
    Just Joined!
    Join Date
    Jan 2012
    Posts
    21
    Quote Originally Posted by drl View Post
    Hi.

    Code:
           case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac
                  A case command first expands word, and tries to match it against
                  each pattern in turn, using the same matching rules as for
                  pathname expansion
    and
    Code:
    Pathname Expansion
    ...
           The special pattern characters have the following meanings:
    
           *      Matches any string, including the null string.
           ?      Matches any single character.
           [...]  Matches any one of the enclosed characters.  A pair of
                  characters separated by a hyphen denotes a range expression; any
                  character that sorts between those two characters, inclusive,
                  using the current locale's collating sequence and character set,
                  is matched.
    Both excerpts above are from man bash.

    Essentially you are trying to use whole strings in the [ .. ] construct. While it is syntactically correct, the semantics are not what you think. You need to focus on using single characters inside the [ ... ]

    You can use the alternation symbol (i.e. "|"), however, to allow choices among strings in a case selector.



    Best wishes ... cheers, drl

    Hi
    Thank you very much for the replay.
    i found that the problem is with double digit numbers, 10,11 etc..
    do you know why?

    This is fine- 10.7.[4-7].*) vlan=11 ;;
    This is not- 10.7.[12,13,14,15].*) vlan=14 ;;
    did someone know why? what is the problem with range of 2 digits numbers?
    another example:
    This 10.7.[8,9,10,11].*) vlan=12 ;;
    10.7.8x.x and 10.7.9.x.x is ok, but when im trying to run with 2 digits number, the scripit ignored it.

    Thanks again.

  7. #6
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    226
    open man bash, and search (/) \[\.\.\.\]

  8. #7
    Just Joined!
    Join Date
    Jan 2012
    Posts
    21
    Hi all

    I finished to wroth the script and is working just perfect, thank you.
    i have another small problem with "expect" command:
    when i want to change the VLan in the switch, i need to to telnet to the switch, so i do telnet with expect commands, like this:

    if [ $vlan == $vlan1 ]

    then

    expect <<EOF
    set timeout 5
    spawn telnet $switchip
    expect "Username:";
    send "root\r";
    expect "Password:";
    send "\$moot\r";
    expect "#";
    send "conf\r";
    expect "#";
    send "vlan 1 untagged $portnumber\r";
    expect "#"
    send "logout"
    exit
    EOF


    as you can see, the password to my switch is starting with "$", and $ is a variable in bash/expect.
    there is strange thing, when i write the expect commands inside the bash script, the script think that the password is variable, i can't tell it to ignore from the $ sign.
    i have tried to do / and all other variation to tell it to "turn off" the $ sign.
    but when i making separate expect script and i calling to it from the bash script, and i'm using / before the $, then, everything is ok.

    so, when the expect is in the bash script, i can't turn off the $ sign and not use it as variable, but when i'm using separate script for the expect, everything is ok.
    does anyone know why?

    separate expect sample:
    #!/usr/bin/expect
    set timeout 5
    spawn telnet [lindex $argv 0]
    expect "Username:";
    send "root\r";
    expect "Password:";
    send "\$moot\r";
    expect "#";
    send "conf\r";
    expect "#";
    send "vlan [$argv 1] untagged [$argv 2]\r";
    expect "#"
    send "logout"
    exit


    I found something more strange,
    before some letter, i can ignore $ and before other letters the script thinking that is variable.
    for example:
    this is good:
    expect Password: ;
    send "$\oor\r" ;

    This is not:
    expect Password: ;
    send "$\toor\r" ;

    i found the problem
    \t is special characters in expect, its like \r and \n..
    so i do i escape from this character and tell expect to think that is regular character?



    Thank you very much.
    Last edited by meirh; 01-03-2013 at 03:05 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •