Tech Support Guy banner
Status
Not open for further replies.
1 - 20 of 29 Posts

·
Registered
Joined
·
309 Posts
Discussion Starter · #1 ·
I'm working on a program to save and restore the network configuration. This part is a program to save the network configuration. It's still incomplete, but at this time, I'm having trouble designing a loop. This loop is supposed to generate the IP Address information. I'll include the minimum files needed for the task.

Test is a test profile. I manually generated that one when I was creating restore netstate. The other package contains the code I'm working on. The loop is in save netstate. At the very end, in the last big loop you see is the loop(s) I am intending to write. These are encased in that big loop so that I generate the information for all network cards simultaniously. The loop should exist in that loop.
 

Attachments

·
Registered
Joined
·
309 Posts
Discussion Starter · #3 ·
The loop needs to be in Windows XP style batch file code.

Now if you look at the other code, most of the controlling code is written in that language too, but some is written in AutoIt code because it can't be written purely in batch code. In other words, some code makes use of autoit to compute certain tasks, like reading from GUI's and such.

But mainly the controlling program is in a batch file. Therefore, the same should be done in the other code.
 

·
Registered
Joined
·
9,053 Posts
Finally got a chance to take a look.
I'm having trouble designing a loop.
What trouble? Is it not outputting the correct info, or is it generating an error?
This loop is supposed to generate the IP Address information. I'll include the minimum files needed for the task.
You didn't include OUT2COM or FREGSTRVALUE, or any files those two may call, so we can't run the loop, let alone the entire file.
Without those files, or a description/sample of the data they return, there is no way to tell if the problem is in save_netstate or in the data those files return.

The data (all zeros) in the net0_IP_Address_Info.ncnf and net1_IP_Address_Info.ncnf files are what you would expect if DHCP is enabled. The loop sets ADDRESSES, NETMASKS, and GATEWAYS to 0 if DHCP is enabled, and METRICS will be 0 if OUT2COM returns an error, or will be whatever OUT2COM returns in the OUTCMD variable. If you need the actual values, you need to call a routine that will get them.

Did find the following errors:
Code:
Line 482     SET SUBINDEX2=!SUBINDEX2!+1
Line 489     SET SUBINDEX2=!SUBINDEX2!+1
Line 496     SET SUBINDEX2=!SUBINDEX2!+1
Line 503     SET SUBINDEX2=!SUBINDEX2!+1
Line 510     SET SUBINDEX1=!SUBINDEX1!+1
You forgot the /A switch for the Set command.
Also, might be easier to use this format for incrementing a variable:
Code:
Set /A SUBINDEX2+=1
The number of times the Echo statement in line 507 will be executed depends on the value of INDEX3. If INDEX3 is zero, it will be executed just once. The first time it will use the current value of these variables:
ADDRESS
NETMASK
GATEWAY
METRIC

These values are set in the preceding For loops only if SUBINDEX2 and SUBINDEX1 are not equal, which only happens if INDEX3 is 1 or more.
And SUBINDEX2 will always be zero when the If statement is executed.
So the first time it outputs the current Echo setting to the file (unless they have been defined someplace previously).
The 2nd and subsequent times, it will use the first value it parses from these variables in the For loop in line 457:
ADDRESSES
NETMASKS
GATEWAYS
METRICS


Without knowing the format of the data in these 4 variables, there is no way to accurately troubleshoot this section, but, for example, if ADDRESSES is a list of comma/space delimited values, ADDRESS will be set to the first item only, it will never be set to any other value in ADDRESSES.

So no matter what the value of INDEX3, it will only return the first value from those 4 variables.

So, to troubleshoot this we need either all the files it will call, or the values these variables have at line 457:
ADDRESS
NETMASK
GATEWAY
METRIC
ADDRESSES
NETMASKS
GATEWAYS
METRICS


And the value of INDEX3 at line 476.

I don't have any systems with a multi-homed NIC, so a sample for those variables from a system that does would be useful. I'm assuming the whole point is to keep the data aligned, for example a NIC with multiple IPs/Gateways; you might have 3 addresses but only 1 or two Gateways.

Another suggestion. For the usage sections I would use %~n0 rather than just %0. %0 returns the batch file name as called, not just the base name as %~n0 does. So if someone calls the file like C:\Scripts\save_network.cmd, usage will display:

Usage:
C:\Scripts\save_network.cmd [Profile Name], not just
Usage: save_network [Profile Name]

Jerry
 

·
Registered
Joined
·
309 Posts
Discussion Starter · #7 ·
OK, so the incrementing tip helps a lot. I used the other form so much, I don't think I'll change it, but I'll start now to use the += form. I would've preferred the equivelant of ++ if it existed, as in i++. C++ has those capabillities, and so does C. Obviously though, this is a different language. But if it happened to have those operators, I'd use them instead, as that's more appropriate. I think I tried and found it doesn't though. I'm glad it does have the += operator though.

The other sugesstion, the %~n0, is good to know in my bag of tricks. But if it returns the extension with it, it's NOT what I want. I only wanted to display the usage using the name, but NOT the extension, because I want to emphasize the name so that the user get's the idea of the function, but I don't want them to see the extension. But that helps, because I didn't know that one existed either.

So basically, the loop is only returning the first values, huh? I wondered if that was why the output was going on so wacky. I want to use all the values, and they are seperated by commas. One thing that threw me off though, is Windows XP allows you to have different numbers of gateways and metrics, then it does IP Addresses and Netmasks. So IP Addresses and Netmasks, are synchronized in the number, and then gateways and metrics are synchronized in number, though there may not be the same number of both of those two. But it can be. Well, that's to do with multiple information for the same network card. Windows XP supports it, therefore my programs should too.

Yes, I've got multiple cards on this system, and that's a lot of what the need for this program is. I only have one card so far per type of card. But I've got one wired card, and one wireless card, and one firewire card. That's not counting virtual cards I may later install.

So you need to run the program huh? Well, it seems obvious that you might, but I didn't want to send the rest unless I had to. For one, it's turned into a bigger package than I origionally planned. But I'll get it there then. I'll attach it.

Looks like it's not working. It's probably too big. So for the remainder of this thread, here's what I'll do. I'll put it up on my NAS (a handy thing), and then give the user name and password.

The username is forumhelp, and the password is guest. The share name is forumhelp. So you can access the relevant files on ftp://sunraysvillanet.dyndns.org/disk1/forumhelp. It's only read only. And it will only stay up long enough for me to figure this out. But maybe that's the easiest way, because these files seem to calculate to too big.
 

·
Registered
Joined
·
309 Posts
Discussion Starter · #8 ·
There, data should be up now. This is a wireless link, so sometimes it has trouble functioning. But right now, it's up (mostly) 24/7. Please only copy it from there to your local machine before reading or viewing it.

You'd have to install using the setup batch file for the standard scripts to get it to work. (It will put out a folder named C:\BATCH, and MUST BE IN THIS FOLDER!) Then the included autoexec.bat will most likely be at least partially needed, so that the programs execute. I would be careful in not replacing anything of yours that's important, if you've got stuff named like that.

Now, the network folder contains the files, now as I've been working on them since, trying at both ends therefore to debug this somehow. The copy before is the copy it was before. Either copy the most recent (which just has those lines changed), or the copy I sent is fine to work on. I "think" that if you install the standard scripts, it will work just fine. If some parts don't work just fine, it's only because that these files might rely on other programs I'd forgotten about, because I hadn't documented it yet, because I found that there was some little pieces missing. But just post any errors if you need to run a program that isn't working for these purposes, and then I'll try to find the dependency or point you to what it's dependent on, where I downloaded it and how to install it.

Now also after you install the standard scripts, you have to copy the binaries from the network folder to the installed folder copy of the standard scripts (C:\BATCH). Some of those things expect to run from ONLY there. The binaries, are basically, all the .exes (which are just the compiled autoit's pretty much), plus all the batch files, plus the Profiles folder, which includes the Test profile, which was manually generated, and is an example of the language I'm generating to, though it does NOT show it's complete capabillities or anything, and then copy all the stuff from inside the modified folder to C:\BATCH as well. DO NOT just copy the modified folder, it won't work. It expects to have the contents, not the modified folder. That folder was just there to show me what I modified when I was creating stuff, due to different bugs.

Once you install those, you should be able to run the program now, that is, unless I forgot to include anything. It has also occasionally failed due to some code. So if it fails with things like "Bad access value" or something like that it was, then just wait and run it again. I don't know why it does that yet.

The syntax name, is save_netstate [Profile Name].
 

·
Registered
Joined
·
9,053 Posts
The other sugesstion, the %~n0, is good to know in my bag of tricks. But if it returns the extension with it, it's NOT what I want.
%~n will return just the name. %~nx will return name and extension. Type Call /? or For /? into a command prompt to see the complete list of modifiers

Might be easier to just add a couple of echo statements to capture the variables into a file and post that, rather than make sure nothing is missing, or in a wrong folder. But I'll see if I can get it working. Put it in a virtual machine with a couple of virtual cards and see what happens.

To capture the variables, in the save_netstate file, put this in Line 475 (right after INDEX3 is set):
Code:
For %%a In (1 2 3) Do Echo INDEX%%a=!INDEX%%a!>>forumhelpdata.txt
And this in Line 456, (Right before the For statement):
Code:
For %%a In (ADDRESSES NETMASKS GATEWAYS METRICS ADDRESS NETMASK GATEWAY METRIC) Do Echo %%a=!%%a!>>forumhelpdata.txt
There must not be a space between the ! and the >>
Then just attach the forumhelpdata.txt file to a post
Edit the IP Addresses in the file if needed so the first two numbers are 192.168, or similar if you want to keep the real ones private.

Try this though, if the data is formatted the way I expect, this should do the trick:
Code:
For %%I In (ADDRESSES NETMASKS GATEWAYS METRICS) Do (
Set _count=0
Set _tmp=!%%I!
FOR %%A IN ("!_tmp:,=","!") DO (
Set /A _count+=1
Set _tmp=%%A                   .
Set _%%I!_count!=!_tmp:~0,19!
))
Echo.IP Address       NetMask          Gateway          Metric>>!CARD_CARD_IP!_Address_Info.ncnf
For /L %%I In (1,1,!_count!) Do Echo !_ADDRESSES%%I:"=!!_NETMASKS%%I:"=!!_GATEWAYS%%I:"=!!_METRICS%%I:"=!>>!CARD_CARD_IP!_Address_Info.ncnf
Will produce this output:
Code:
IP Address       NetMask          Gateway          Metric
192.168.100.100  255.255.255.128  192.168.100.1    1                
192.168.200.150  255.255.255.0                                      
1.2.3.4          255.0.0.0        1.2.3.1          3
If the data looks like this:

ADDRESSES=192.168.100.100,192.168.200.150,1.2.3.4
NETMASKS=255.255.255.128,255.255.255.0,255.0.0.0
GATEWAYS=192.168.100.1,,1.2.3.1
METRICS=1,,3

Blank values must be represented by consecutive commas or there is no way to insure the values will match up.

HTH

Jerry
 

·
Registered
Joined
·
9,053 Posts
Haven't got it installed yet, but was able to follow the chain to see how the data for the loop is generated. This should work better.
This parses the data and puts it into temp variables. If there are unequal numbers of Addresses/NetMasks vs Gateways/Metrics, it will create dummy entries to "fill the blanks" so the output will look OK.
Code:
Set _pad=                   .
For %%I In ([COLOR=Red]ADDRESSES NETMASKS[/COLOR]) Do Call :_parse1 %%I [COLOR=Red]_AddCount[/COLOR]
For %%I In ([COLOR=Red]GATEWAYS METRICS[/COLOR]) Do Call :_parse1 %%I [COLOR=Red]_GateCount[/COLOR]
If !_AddCount! EQU !_GateCount! (
  Set _max=!_AddCount!
  ) Else (
  If !_AddCount! GTR !_GateCount! (
    Call :_parse2 [COLOR=Red]GATEWAYS METRICS[/COLOR] !_GateCount! !_AddCount!
    ) Else (
    Call :_parse2 [COLOR=Red]ADDRESSES NETMASKS[/COLOR] !_AddCount! !_GateCount!
))  
Echo.IP Address       NetMask          Gateway          Metric>>!CARD_CARD_IP!_Address_Info.ncnf
For /L %%I In (1,1,!_max!) Do Echo !_ADDRESSES%%I:"=!!_NETMASKS%%I:"=!!_GATEWAYS%%I:"=!!_METRICS%%I:"=!>>!CARD_CARD_IP!_Address_Info.ncnf
Goto done
::Subroutines to parse Addresses, Netmasks, Gateways, and Metrics for proper Display
:_parse1
Set _count=0
Set _tmp=!%1!
FOR %%A IN ("!_tmp:,=","!") DO (
  Set /A _count+=1
  Set _tmp=%%A%_pad%
  Set _%1!_count!=!_tmp:~0,19!
)
Set %2=!_Count!&Goto :EOF
:_parse2
Set _max=%4
Set /A _tmp=%3+1
For /L %%I In (!_tmp!,1,%4) Do (
  Set _%1%%I=%_pad:~0,17%
  Set _%2%%I=%_pad:~0,17%
  )
Goto :EOF
EDIT Color coded variable names that are being used as strings rather than values. The should not have ! or % around them.
Also added missing output line.
Jerry
 

·
Registered
Joined
·
309 Posts
Discussion Starter · #11 ·
Thanks. I'm checking it to see if it's working. I had to change a few things to the correct syntax first, as the variable names needed ! around them. ! means delayed variable expansion which all of these needed.
 

·
Registered
Joined
·
9,053 Posts
All the variables that need the ! for delayed expansion are already set.
If you are referring to these lines for example:
Code:
 For %%I In ([B]ADDRESSES NETMASKS[/B]) Do Call :_parse1 %%I _AddCount
For %%I In ([B]GATEWAYS METRICS[/B]) Do Call :_parse1 %%I _GateCount
ADDRESSES NETMASKS GATEWAYS METRICS are text strings, not variables. The routine needs to pass the name of the variable, not the value.
If you put the ! marks around them it will not work
 

·
Registered
Joined
·
9,053 Posts
Just noticed I left out the line that actually outputs the data.:eek:

I've added it back in, and color coded the variable names in those locations where they are being used as names rather than values.
 

·
Registered
Joined
·
309 Posts
Discussion Starter · #17 ·
So it's been close to working. I found some issues with it though. One, the code uses a bunch of spaces instead of one tab inbetween the elements. Two, it fills things with "nothing". I want to fill things with 0's. I'm closer to figuring this out so far, but not there yet. I'm away from my normal Internet access right now, but will be back to it, so I hadn't posted a lot more yet.
 

·
Registered
Joined
·
9,053 Posts
I used spaces because tabs won't always line up. It will depend on what the tab stops are set to in whatever program is used to view the resulting file. Notepad defaults to multiples of 8, but Notepad++ defaults to multiples of 4, and Notetab is multiples of 6.
Spaces will line up in the Command Prompt, or any editor using a mono-spaced font, like the ones mentioned above. Using spaces just seemed easier.
Of course, spaces won't line up if it's viewed with a proportional font, like in Wordpad (tabs in multiples of 6 as well).
Example if using Notepad:
IP Address of xxx.xxx.xxx.xxx with one tab will work, there will be one space after the IP Address
IP Address of x.x.x.x needs two tabs (three in Notepad++)
At some point in between Notepad++ would need two tabs
If you put just a 0 in there, you need two (four in Notepad++)

You could check the length to determine how many tabs to add, but you would need to keep track of the length of the first three values.

If you want zeros instead of blanks, change these two lines:
Code:
[COLOR=Blue]29[/COLOR] Set _%1%%I=%_pad:~0,17%
[COLOR=Blue]30[/COLOR] Set _%2%%I=%_pad:~0,17%
To this
Code:
[COLOR=Blue]29[/COLOR] Set _%1%%I=[COLOR=Red]0[/COLOR]%_pad:~0,1[COLOR=Red]6[/COLOR]%
[COLOR=Blue]30[/COLOR] Set _%2%%I=[COLOR=Red]0[/COLOR]%_pad:~0,1[COLOR=Red]6[/COLOR]%
If you want 0.0.0.0, put that in and use 10 instead of 16. You'd then have to special case Metrics or it would also show as 0.0.0.0 if not present:
Code:
[COLOR=Blue]29[/COLOR] Set _%1%%I=[COLOR=Red]0.0.0.0[/COLOR]%_pad:~0,1[COLOR=Red]0[/COLOR]%[COLOR=Blue]
30[/COLOR] If %2==METRICS (Set _%2%%I=[COLOR=Red]0[/COLOR]%_pad:~0,1[COLOR=Red]6[/COLOR]%) Else (Set _%2%%I=[COLOR=Red]0.0.0.0[/COLOR]%_pad:~0,1[COLOR=Red]0[/COLOR]%)
If you want to use tabs, then change these lines:
Code:
[COLOR=Blue]01[/COLOR] Set _pad=                   .
[COLOR=Blue]22[/COLOR] Set _%1!_count!=!_tmp:~0,19!
[COLOR=Blue]29[/COLOR] Set _%1%%I=%_pad:~0,17%
[COLOR=Blue]30[/COLOR] Set _%2%%I=%_pad:~0,17%
.
Code:
[COLOR=Blue]01[/COLOR] Set _pad=[COLOR=Red]{TAB}[/COLOR]  [COLOR=Blue][B]<-- type a tab character for[/B][/COLOR] [COLOR=Red]{TAB}[/COLOR].
[COLOR=Blue]22[/COLOR] Set _%1!_count!=!_tmp!%_pad%
[COLOR=Blue]29[/COLOR] Set _%1%%I=0%_pad%
[COLOR=Blue]30[/COLOR] Set _%2%%I=0%_pad%
and remove this line:
Code:
[COLOR=Blue]21[/COLOR] Set _tmp=%%A%_pad%
The Line Numbers refer to code in post #10; could be off, but will get you the the right part at least.

For the zero lines (29,30) add another %_pad% for two tabs
Code:
Set _%1%%I=0%_pad%%_pad%
more if needed.

You could also create several variables (_pad1, _pad2, _pad3, _pad4) each with a different number of tabs, then select the correct one based on length.

Jerry
 

·
Registered
Joined
·
309 Posts
Discussion Starter · #19 ·
Thanks. I'll try that. I agree, tabs won't always line up for pretty printing. The only reason to use tabs instead is because this is machine generated output to be read back in by the machine. Though if someone wants to generate these manually, they certainly may, and they may use comments to make it look prettier. But the machine will simply use one tab inbetween the stuff.
 

·
Registered
Joined
·
9,053 Posts
Gotcha:up:

Don't know if a trailing tab would be a problem, depends on how the data is parsed when read in.
If so, you can use this to skip adding a tab for the Metrics value, as it's last on the line:
Code:
[COLOR=Blue]30[/COLOR] If %2==METRICS (Set _%2%%I=0) Else (Set _%2%%I=0%_pad%)
Jerry
 
1 - 20 of 29 Posts
Status
Not open for further replies.
Top