Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie: Need help with looping comm and seperating strings into vaules 1

Status
Not open for further replies.

LantanaCom

IS-IT--Management
Nov 4, 2009
8
0
0
US
Alright so, I am new to C++ and have been playing with it to create a program to help me configure Network Devices Quickly. Here is something I am trying to get past before I move on to some type of menu to add multiple VLANS, ect.

Below, My "First Question" of the program is, What Switch Chassis ID do you want to program? There are up to 10 switches in a Chassis, The ID's start at 0 through 9.

I would like to either run a range command here, numbers seprated by commas or both. After the numbers have been entered they would then need to be divided into different vaules and then run a looping condition on the next Question.

Example:
Configure Chassis 1,2,3
cValue1=1,cValue2=2,cValue3=3
Configure Chassis 2
cValue1=2
Configure Chassis 1,2,5-8
cValue1=1,cValue2=2,cValue3=5,cValue4=6,cValue5=7,cValue6=8

Given that there are 10 switches in a chassis, This could potentially be broken into 10 Values.

So after the vaules have been sorted it, How many ever vaules their are is how many time the next question would be asked, but representing a different chassis number.

I would want this line to change in the question representing the chassis number.
**THIS CONFIGURATION IS FOR CHASSIS NUMBER " << chassisNum << " **" << endl;

So this question I am just asking what ports do I want to configure for the given chassis ID. I would like the same options. I would like to either run a range command here, numbers seprated by commas or both. After the numbers have been entered they would then need to be divided into different vaules and then run a looping condition on the next Question.

Example:
Configure Ports 1,2,3
pValue1=1,pValue2=2,pValue3=3
Configure Ports 2
pValue1=2
Configure Ports 1,2,5-8
pValue1=1,pValue2=2,pValue3=5,pValue4=6,pValue5=7,pValue6=8

Given that there are 48 ports on a switch, This could potentially be broken into 48 Values.

After those two questions have been seperated out into vaules. Then they need to be put int something like this.

cout << "set interface ge-" << cValue# << "/0/" << pValue#

and then multiplied out how ever many times it needs to be. Im assuming that this could be up to 10 sets of code, 1 for each chassis ID.

If I configured 10 Switches with 48 ports, I should end up with 480 lines of output just for the interface configuration part. This is all I am trying to get accomplished.

Im sure there is an easier way of doing it than what I started on the bottom. I was not even sure if I was going in the right direction with that. Like I said I am no pro at this just a newbie so any help I would epreciate.

I also am not asking for anyone to complete this for me but just provide some verable to get me started on how seperate a string into multiple values and then carry those over to a function (Not sure) and make it loop so many times to get the appropriate amount of code for all interfaces that need to be configured for each chassis.

Thanks Again

// Juniper Port Configuration

#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;


int main()
{

int option_continue;
float chassisNum;
float portNum;

string nameVLAN;
string idVLAN;
string ipAddress;
string subnetMask;

float chassisOut;



// PROGRAM INTRODUCTION, START PROGRAM
cout << endl;
cout << " ******************************************************************" << endl;
cout << endl;
cout << " You are using Juniper Networks Configuration Utility. " << endl;
cout << " For 'EX' Switches, version 1.0" << endl;
cout << endl;
cout << " Answer the questions and this program will" << endl;
cout << " print out the configuration." << endl;
cout << endl;
cout << " Type '1' and then press 'Enter' to begin." << endl;
cout << endl;
cout << " ******************************************************************" << endl;
cout << endl;
cout << " ==>";cin >> option_continue;

// FIRST QUESTION, ENTER CHASSIS NUMBER OF SWITCH
cout << endl;
cout << " ******************************************************************" << endl;
cout << endl;
cout << " Whats the 'Chassis Number' of the switch you want to configure?" << endl;
cout << endl;
cout << " There is a Max of 10 Switches in a Chassis." << endl;
cout << " You can use the following numbers: 0,1,2,3,4,5,6,7,8,9." << endl;
cout << endl;
cout << " If you want to program multiple Chassis, you can enter" << endl;
cout << " multiple numbers seperated by a comma, DO NOT USE SPACES." << endl;
cout << endl;
cout << " Example: Type '0' if the Chassis number is '0' (GE-0/0/0)" << endl;
cout << " Type '1' if the Chassis number is '1' (GE-1/0/0)" << endl;
cout << " Type '2' if the Chassis number is '2' (GE-2/0/0)" << endl;
cout << " Type '4,6,8' if you want to program Chassis numbers" << endl;
cout << " '4,6,8' (GE-4/0/0,GE-6/0/0,GE-8/0/0)" << endl;
cout << endl;
cout << " ******************************************************************" << endl;
cout << endl;
cout << " ==>";cin >> chassisNum;

// CREAT LOOP HERE TO RUN THE NEXT QUESTION FOR HOW MANY VAULES PLACES THERE ARE IN FIRST QUESTION
// IF TWO NUMBERS SEPERATED BY A COMMA ARE ENTERED IN FIRST QUESTION THEN RUN THE SECOND QUESTION TWICE

// SECOND QUESTION,WHAT PORTS DO YOU WANT TO CONFIGURE FOR THE GIVEN CHASSIS
cout << endl;
cout << " ******************************************************************" << endl;
cout << endl;
cout << " **THIS CONFIGURATION IS FOR CHASSIS NUMBER " << chassisNum << " **" << endl;
cout << endl;
cout << " What 'Ports' do you want to configure on the switch?" << endl;
cout << endl;
cout << " There is a Max of 48 Ports per." << endl;
cout << " You can use the following numbers: 0,1,2,3,4,5,6,7,8,9,10,11,12," << endl;
cout << " 13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33," << endl;
cout << " 34,35,36,37,38,39,40,41,42,43,44,45,46,47" << endl;
cout << endl;
cout << " If you want to program multiple Ports, you can enter" << endl;
cout << " multiple numbers seperated by a comma, DO NOT USE SPACES." << endl;
cout << endl;
cout << " Example: Type '0' if the Port number is 0 (GE-0/0/0)" << endl;
cout << " Type '1' if the Port number is 1 (GE-0/0/1)" << endl;
cout << " Type '2' if the Port number is 2 (GE-0/0/2)" << endl;
cout << " Type '4,6,8' if you want to program Port numbers" << endl;
cout << " '4,6,8' (GE-0/0/4,GE-0/0/6,GE-0/0/8)" << endl;
cout << endl;
cout << " ******************************************************************" << endl;
cout << endl;
cout << " ==>";cin >> portNum;

//THIRD QUESTION, TYPE THE VLAN NAME
cout << endl;
cout << " ******************************************************************" << endl;
cout << endl;
cout << " Type the VLAN Name: " << endl;
cout << endl;
cout << " The 'VLAN Name' cannot have any spaces before, after" << endl;
cout << " or in the name." << endl;
cout << endl;
cout << " Example: Type 'Default' if the 'VLAN Name' is Default" << endl;
cout << endl;
cout << " ******************************************************************" << endl;
cout << endl;
cout << " ==>";cin >> nameVLAN;

//FORTH QUESTION, TYPE THE VLAN ID
cout << endl;
cout << " ******************************************************************" << endl;
cout << endl;
cout << " Type the VLAN ID: " << endl;
cout << " This can also be reffered to as 'VLAN Number' or 'VLAN Tag'." << endl;
cout << endl;
cout << " The 'VLAN ID' cannot have any spaces before, after" << endl;
cout << " or in the ID. 'VLAN ID' can only contain numbers." << endl;
cout << " You can uses numbers 1 through 4094." << endl;
cout << endl;
cout << " Example: Type '100' if the 'VLAN ID' is 100" << endl;
cout << endl;
cout << " ******************************************************************" << endl;
cout << endl;
cout << " ==>";cin >> idVLAN;

//FITH QUESTION, TYPE THE IP ADDRESS OF VLAN
cout << endl;
cout << " ******************************************************************" << endl;
cout << endl;
cout << " Type the IP address of the VLAN: " << endl;
cout << endl;
cout << " The 'address' cannot have any spaces before, after" << endl;
cout << " or in the address. The 'address' contains 4 Octets" << endl;
cout << " seperated by 3 periods" << endl;
cout << endl;
cout << " Example: Type '172.16.100.254' if the 'address' is 172.16.100.254" << endl;
cout << endl;
cout << " ******************************************************************" << endl;
cout << endl;
cout << " ==>";cin >> ipAddress;

//SIXTH QUESTION, TYPE THE SUBNET MASK IN CIDER NOTATION
cout << endl;
cout << " ******************************************************************" << endl;
cout << endl;
cout << " Type the Subnet Mask in Cider Notation: /" << endl;
cout << endl;
cout << " --- CLASS 'C' --- --- CLASS 'B' --- --- CLASS 'A' ---" << endl;
cout << " /30 = 255.255.255.252 /23 = 255.255.254.0 /15 = 255.254.0.0" << endl;
cout << " /29 = 255.255.255.248 /22 = 255.255.252.0 /14 = 255.252.0.0" << endl;
cout << " /28 = 255.255.255.240 /21 = 255.255.248.0 /13 = 255.248.0.0" << endl;
cout << " /27 = 255.255.255.224 /20 = 255.255.240.0 /12 = 255.240.0.0" << endl;
cout << " /26 = 255.255.255.192 /19 = 255.255.224.0 /11 = 255.224.0.0" << endl;
cout << " /25 = 255.255.255.128 /18 = 255.255.192.0 /10 = 255.192.0.0" << endl;
cout << " /24 = 255.255.255.0 /17 = 255.255.128.0 /9 = 255.128.0.0" << endl;
cout << " /16 = 255.255.0.0 /8 = 255.0.0.0" << endl;
cout << endl;
cout << " Only type the number. DO NOT TYPE THE SLASH" << endl;
cout << endl;
cout << " Example: Type '24' if the 'Subnet Mask' is 255.255.255.0" << endl;
cout << endl;
cout << " ******************************************************************" << endl;
cout << endl;
cout << " ==>";cin >> subnetMask;


chassisOut = chassisNum;

cout << endl;
cout << " ******************************************************************" << endl;
cout << endl;
cout << " Copy the configuration below and past into your," << endl;
cout << " Juniper Console under 'Edit' mode. At 'Top' of 'root'. (root#)" << endl;
cout << endl;
cout << " ******************************************************************" << endl;
cout << endl;
cout << "set vlan " << nameVLAN << " l3-interface vlan." << idVLAN << endl;
cout << "set interface vlan unit " << idVLAN << " family inet address " << ipAddress << "/" << subnetMask << endl;


if (portNum == 0)
{
cout << "set interface ge-" << chassisOut << "/0/0" << " family ethernet-switching vlan members " << nameVLAN << endl;
}
if (portNum == 1)
{
cout << "set interface ge-" << chassisOut << "/0/1" << " family ethernet-switching vlan members " << nameVLAN << endl;
}
if (portNum == 2)
{
cout << "set interface ge-" << chassisOut << "/0/2" << " family ethernet-switching vlan members " << nameVLAN << endl;
}
if (portNum == 3)
{
cout << "set interface ge-" << chassisOut << "/0/3" << " family ethernet-switching vlan members " << nameVLAN << endl;
}

return 0;
}
 
Look at bottom.

C:\CODE>Juniper.exe

******************************************************************

You are using Juniper Networks Configuration Utility.
For 'EX' Switches, version 1.0

Answer the questions and this program will
print out the configuration.

Type '1' and then press 'Enter' to begin.

******************************************************************

:>1

******************************************************************

Whats the 'Chassis Number' of the switch you want to configure?

There is a Max of 10 Switches in a Chassis.
You can use the following numbers: 0,1,2,3,4,5,6,7,8,9.

If you want to program multiple Chassis, you can enter
multiple numbers seperated by a comma, DO NOT USE SPACES.

Example: Type '0' if the Chassis number is '0' (GE-0/0/0)
Type '1' if the Chassis number is '1' (GE-1/0/0)
Type '2' if the Chassis number is '2' (GE-2/0/0)
Type '4,6,8' if you want to program Chassis numbers
'4,6,8' (GE-4/0/0,GE-6/0/0,GE-8/0/0)

******************************************************************

exit

******************************************************************

**THIS CONFIGURATION IS FOR CHASSIS NUMBER 5.98458e-039 **

What 'Ports' do you want to configure on the switch?

There is a Max of 48 Ports per.
You can use the following numbers: 0,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

If you want to program multiple Ports, you can enter
multiple numbers seperated by a comma, DO NOT USE SPACES.

Example: Type '0' if the Port number is 0 (GE-0/0/0)
Type '1' if the Port number is 1 (GE-0/0/1)
Type '2' if the Port number is 2 (GE-0/0/2)
Type '4,6,8' if you want to program Port numbers
'4,6,8' (GE-0/0/4,GE-0/0/6,GE-0/0/8)

******************************************************************


******************************************************************

Type the VLAN Name:

The 'VLAN Name' cannot have any spaces before, after
or in the name.

Example: Type 'Default' if the 'VLAN Name' is Default

******************************************************************


******************************************************************

Type the VLAN ID:
This can also be reffered to as 'VLAN Number' or 'VLAN Tag'.

The 'VLAN ID' cannot have any spaces before, after
or in the ID. 'VLAN ID' can only contain numbers.
You can uses numbers 1 through 4094.

Example: Type '100' if the 'VLAN ID' is 100

******************************************************************


******************************************************************

Type the IP address of the VLAN:

The 'address' cannot have any spaces before, after
or in the address. The 'address' contains 4 Octets
seperated by 3 periods

Example: Type '172.16.100.254' if the 'address' is 172.16.100.254

******************************************************************


******************************************************************

Type the Subnet Mask in Cider Notation: /

--- CLASS 'C' --- --- CLASS 'B' --- --- CLASS 'A' ---
/30 = 255.255.255.252 /23 = 255.255.254.0 /15 = 255.254.0.0
/29 = 255.255.255.248 /22 = 255.255.252.0 /14 = 255.252.0.0
/28 = 255.255.255.240 /21 = 255.255.248.0 /13 = 255.248.0.0
/27 = 255.255.255.224 /20 = 255.255.240.0 /12 = 255.240.0.0
/26 = 255.255.255.192 /19 = 255.255.224.0 /11 = 255.224.0.0
/25 = 255.255.255.128 /18 = 255.255.192.0 /10 = 255.192.0.0
/24 = 255.255.255.0 /17 = 255.255.128.0 /9 = 255.128.0.0
/16 = 255.255.0.0 /8 = 255.0.0.0

Only type the number. DO NOT TYPE THE SLASH

Example: Type '24' if the 'Subnet Mask' is 255.255.255.0

******************************************************************


******************************************************************

Copy the configuration below and past into your,
Juniper Console under 'Edit' mode. At 'Top' of 'root'. (root#)

******************************************************************

set vlan l3-interface vlan.
set interface vlan unit family inet address /

C:\CODE>Juniper.exe

******************************************************************

You are using Juniper Networks Configuration Utility.
For 'EX' Switches, version 1.0

Answer the questions and this program will
print out the configuration.

Type '1' and then press 'Enter' to begin.

******************************************************************

==>1

******************************************************************

Whats the 'Chassis Number' of the switch you want to configure?

There is a Max of 10 Switches in a Chassis.
You can use the following numbers: 0,1,2,3,4,5,6,7,8,9.

If you want to program multiple Chassis, you can enter
multiple numbers seperated by a comma, DO NOT USE SPACES.

Example: Type '0' if the Chassis number is '0' (GE-0/0/0)
Type '1' if the Chassis number is '1' (GE-1/0/0)
Type '2' if the Chassis number is '2' (GE-2/0/0)
Type '4,6,8' if you want to program Chassis numbers
'4,6,8' (GE-4/0/0,GE-6/0/0,GE-8/0/0)

******************************************************************

==>2

******************************************************************

**THIS CONFIGURATION IS FOR CHASSIS NUMBER 2 **

What 'Ports' do you want to configure on the switch?

There is a Max of 48 Ports per.
You can use the following numbers: 0,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

If you want to program multiple Ports, you can enter
multiple numbers seperated by a comma, DO NOT USE SPACES.

Example: Type '0' if the Port number is 0 (GE-0/0/0)
Type '1' if the Port number is 1 (GE-0/0/1)
Type '2' if the Port number is 2 (GE-0/0/2)
Type '4,6,8' if you want to program Port numbers
'4,6,8' (GE-0/0/4,GE-0/0/6,GE-0/0/8)

******************************************************************

==>3

******************************************************************

Type the VLAN Name:

The 'VLAN Name' cannot have any spaces before, after
or in the name.

Example: Type 'Default' if the 'VLAN Name' is Default

******************************************************************

==>TEST

******************************************************************

Type the VLAN ID:
This can also be reffered to as 'VLAN Number' or 'VLAN Tag'.

The 'VLAN ID' cannot have any spaces before, after
or in the ID. 'VLAN ID' can only contain numbers.
You can uses numbers 1 through 4094.

Example: Type '100' if the 'VLAN ID' is 100

******************************************************************

==>100

******************************************************************

Type the IP address of the VLAN:

The 'address' cannot have any spaces before, after
or in the address. The 'address' contains 4 Octets
seperated by 3 periods

Example: Type '172.16.100.254' if the 'address' is 172.16.100.254

******************************************************************

==>172.16.100.254

******************************************************************

Type the Subnet Mask in Cider Notation: /

--- CLASS 'C' --- --- CLASS 'B' --- --- CLASS 'A' ---
/30 = 255.255.255.252 /23 = 255.255.254.0 /15 = 255.254.0.0
/29 = 255.255.255.248 /22 = 255.255.252.0 /14 = 255.252.0.0
/28 = 255.255.255.240 /21 = 255.255.248.0 /13 = 255.248.0.0
/27 = 255.255.255.224 /20 = 255.255.240.0 /12 = 255.240.0.0
/26 = 255.255.255.192 /19 = 255.255.224.0 /11 = 255.224.0.0
/25 = 255.255.255.128 /18 = 255.255.192.0 /10 = 255.192.0.0
/24 = 255.255.255.0 /17 = 255.255.128.0 /9 = 255.128.0.0
/16 = 255.255.0.0 /8 = 255.0.0.0

Only type the number. DO NOT TYPE THE SLASH

Example: Type '24' if the 'Subnet Mask' is 255.255.255.0

******************************************************************

==>24

******************************************************************

Copy the configuration below and past into your,
Juniper Console under 'Edit' mode. At 'Top' of 'root'. (root#)

******************************************************************

// This Is the Output I get when I run the program with the following values which is exactly what I want although I want to be able to justify for multiple interfaces at the same time for multiple chassis.

[color=00FFCC]2[/color] (Question 1, Chassis ID)
[color=CCFF00]3[/color] (Question 2, Switch Port)
TEST (Question 3, VLAN Name)
100 (Question 4, VLAN ID)
[color=CC0099]172.16.100.254[/color] (Question 5, IP Address)
[color=FF9933]24[/color] (Question 6, Subnet Mask)

set vlan TEST l3-interface vlan.100
set interface vlan unit 100 family inet address [color=CC0099]172.16.100.254[/color]/[color=FF9933]24[/color]
set interface ge-[color=00FFCC]2[/color]/0/[color=CCFF00]3[/color] family ethernet-switching vlan members TEST
 
Why not configure separately?

Also why not feed everything into an [string] array?

Default each node to -1 (as an example) and then if there is a value then there you go... you can read it in as what you are already doing (separated by commas) but then place them into a separate class controlled by an array?

Just saying...
(I might have missed something in the code so I don't know if this is what you are looking for)
 
That is what I want to do, I just do not know enough about C++ to create the array and then a loop for step/question 2.

To you it might be simple but I do not have enough expeience with C++, this is my first go around.
 
Feed all of your port info into an array:
//Declare 2d array
int iPortArray[47][5];//an array of 47 (0 to 47) arrays that correspond to the number of ports
//Most likely if you want to keep all the information about a port use a CString instead of an int
// Then refer to port '0' as iPortArray[0]

Also if you are actaully using C++ then create a class that holds each port as an object...
(You're better off creating a class for these, here's a link: but this is only a quick tut. about classes)

this place is a GREAT reference: also I recommend Sam's Teach Yourself c++ in 24 Hours (if you interrested in working in c++ after this project)
also the same series but C# (which is easier to use than c++ but generally on the same level)

If you REALLY REALLY need this like now let me know and if I have the time I will help out with more information...

(sorry I'm at work right now otherwise I'd be more helpful, also do you know anymore languages other than c?)
 
Demonpiggies" Thanks for the information, I will take a look at those sites and try to piece together on how to do an array.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top