get the ip address and map a drive according to IP

G

greg_chu

Hi, I know I can use ipconfig.exe is DOS to get my IP address, the
situation is I need to automat this process.

I need to write a DOS program which issue a command to run IPCONFIG to
get the IP address then issue a net use command to map a drive on the
network.

I know you can use system

system ('command.com /c ipcoinfig.exe")

or

system ('command.com ipcoinfig.exe")

the start the shell, but how do I capture the output from it?

after I capture the IP address then from the IP I can know which server
on the network I need to map to, so I can issue another

system("command.com net use z: \\xyzserver\myshare mypw /User:me")

to map the drive, I need to also check if the net use worked correctly.

Thanks!

Greg
 
E

Eric Sosman

Hi, I know I can use ipconfig.exe is DOS to get my IP address, the
situation is I need to automat this process.

I need to write a DOS program which issue a command to run IPCONFIG to
get the IP address then issue a net use command to map a drive on the
network.

I know you can use system

system ('command.com /c ipcoinfig.exe")

or

system ('command.com ipcoinfig.exe")

the start the shell, but how do I capture the output from it?

This is Question 19.30 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.c-faq.com/
after I capture the IP address then from the IP I can know which server
on the network I need to map to, so I can issue another

system("command.com net use z: \\xyzserver\myshare mypw /User:me")

to map the drive, I need to also check if the net use worked correctly.

These are Questions 19.28 and 19.29.
 
F

Flash Gordon

Hi, I know I can use ipconfig.exe is DOS to get my IP address, the
situation is I need to automat this process.

I need to write a DOS program which issue a command to run IPCONFIG to
get the IP address then issue a net use command to map a drive on the
network.

I know you can use system

system ('command.com /c ipcoinfig.exe")

or

system ('command.com ipcoinfig.exe")

You almost certainly don't need to explicitly run command.com since the
system function runs the specified command using the shell.

system("ipcoinfig.exe")

Also please copy and paste actual code. Since you used a single quote at
the start it obviously was not the code you had tried.
the start the shell, but how do I capture the output from it?

Using some system specific method. Ask in a DOS or Windows group for the
details. They will probably tell you that there is an API that can give
you the information far more reliably.
after I capture the IP address then from the IP I can know which server
on the network I need to map to, so I can issue another

system("command.com net use z: \\xyzserver\myshare mypw /User:me")

to map the drive, I need to also check if the net use worked correctly.

Same comments as before. Also you need to consider that the / character
has special meaning in C strings so you probably wanted "... //User:me"

Also, are you sure a C program is the appropriate thing? This sounds to
me more like a job for a scripting language.

We don't cover the details of what the strings passed to system mean
since they are system specific. We also don't cover the APIs that
Windows, *nix and other systems have for networking. Not do we cover the
alternative methods of invoking external programs that give you more
direct access to what the program invoked outputs.
 
F

Flash Gordon

Walter said:
Is it time for more caffine, Flash? ;-)

It's always time for more caffeine ;-)

It is, of course, the other direction of slash that is the problem.
 
G

greg_chu

#include <stdio.h>
int main()
{
system("CWSDPMI.EXE -p");
system("command /c IP1.bat > IP1.TXT");
system("ipconfig.exe > ip2.txt");
return 0;
}

I tried to use

system("ipconfig.exe > ip2.txt");

but ip2.txt is empty

not sure why

I did get it worked once, but could not make it work again

I am using djgpp to compile to run in DOS, and it asked me to load
CWSDPMI first

so my code has CWSDPMI in it.

also IP1.bat has only

ipconfig.exe

in it.

Thanks!

Greg
 
G

greg_chu

Not sure why

system("command /c ipconfig > ip1.txt");

worked.

if you say ipconfig.exe then would not work.

Greg
 
K

Keith Thompson

Not sure why

system("command /c ipconfig > ip1.txt");

worked.

if you say ipconfig.exe then would not work.

This has nothing to do with C, and everything to do with MS-DOS or MS
Windows.

The system() function just executes a command; how the string is
interpreted as a command is entirely system-specific. If you want
more information, you'll need to ask in a system-specific newsgroup.

(There may also be system-specific methods to get the output of a
command directly rather than by writing it to a temporary file and
then reading the file. Such methods are likely to be more efficient
than what you're doing, and are really no more or less portable than
using system().)
 
A

Andrew Poelstra

I tried to use

system("ipconfig.exe > ip2.txt");

but ip2.txt is empty

That's the problem with system(). It's neither portable nor predictable.

Since this question is already off-topic, I'll suggest that you use a
shell script for this.
 
S

Simon Biber

Hi, I know I can use ipconfig.exe is DOS to get my IP address, the
situation is I need to automat this process.

No, you can't use ipconfig.exe in DOS. The ipconfig.exe program is a
Win32 console executable:

C:\WINDOWS\system32>file ipconfig.exe
ipconfig.exe: PE executable for MS Windows (console) Intel 80386 32-bit

Attempting to run it from DOS gives the error:

C:\WINDOWS\SYSTEM32>ipconfig
This program cannot be run in DOS mode.
I need to write a DOS program which issue a command to run IPCONFIG to
get the IP address then issue a net use command to map a drive on the
network.

You'll find that net.exe is also a Win32 console app.

That said, perhaps you don't really know what DOS means. You may be
thinking of the Windows console. You can certainly write a Windows
console program to run the ipconfig.exe and net.exe programs.

Here's an example program to get you started. I have tested it and it
works perfectly on DJGPP's gcc, Cygwin's gcc, Borland's bcc32 and
Microsoft's cl compilers.

It does not work in pure DOS, of course, since ipconfig.exe cannot run
in DOS.

Under DOSBox 0.65, DJGPP will compile the program but when I try to run
it, DOSBox crashes.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_IP_LEN 16
#define MAX_IP_NUM 10
#define TEMP_FILENAME "ip.tmp"

int main(void)
{
char ip_addresses[MAX_IP_NUM][MAX_IP_LEN] = {{0}};
FILE *fp;
char *p;
char buf[256];
int i = 0, j;

system("ipconfig > " TEMP_FILENAME);
fp = fopen(TEMP_FILENAME, "r");
if(fp)
{
while(fgets(buf, sizeof buf, fp))
{
if(strstr(buf, "IP Address"))
{
if(NULL != (p = strchr(buf, '\r'))) *p = 0;
if(NULL != (p = strchr(buf, '\n'))) *p = 0;
if(NULL != (p = strstr(buf, ": ")))
{
strncpy(ip_addresses[i++], p+2, MAX_IP_LEN - 1);
if(i == MAX_IP_NUM) break;
}
}
}
remove(TEMP_FILENAME);
for(j = 0; j < i; j++)
{
printf("IP %d: \"%s\"\n", j, ip_addresses[j]);
}
}
else
{
fprintf(stderr, "Error opening " TEMP_FILENAME "\n");
}
return 0;
}

Example run:

C:\docs\prog\c>gcc -dumpmachine
djgpp

C:\docs\prog\c>gcc getip.c

C:\docs\prog\c>a
IP 0: "192.168.204.1"
IP 1: "192.168.154.1"
IP 2: "192.168.0.2"

(The first two are VMware virtual adapters, and the third is my LAN
connection.)
 
M

Mark McIntyre

No, you can't use ipconfig.exe in DOS. The ipconfig.exe program is a
Win32 console executable:

See, this is why we avoid offtopic answers. To anyone under the age of
around 20, DOS means a Windows command prompt, not your actual DOS...
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
G

Gordon Burditt

Hi, I know I can use ipconfig.exe is DOS to get my IP address, the
situation is I need to automat this process.

I need to write a DOS program which issue a command to run IPCONFIG to
get the IP address then issue a net use command to map a drive on the
network.

You say "the IP address" like it was unique. It's not. Most systems
where you even care about IP addresses have at least two (and sometimes
hundreds per network interface), and one of them is 127.0.0.1 .
I know you can use system

system ('command.com /c ipcoinfig.exe")

or

system ('command.com ipcoinfig.exe")

ipcoinfig.exe is limited to systems with the coin-slot attachment
on the keyboard. This can't compile due to the types of quotes
used above.
the start the shell, but how do I capture the output from it?

Some shells allow I/O redirection, in a system-dependent manner.
Just about *ANY* command you feed to system() is system-dependent
(exception: a NULL pointer). You may need a trap and suitable bait.

I recommend you hand-type any command you intend passing to system()
to the shell used by system() and verify that it works the way
you expect before proceeding.
after I capture the IP address

There you go with *the* IP address again. Press CTRL-ALT-DEL with
*THE* finger (hint: it's attached to *MY* hand, not yours, and
it's not long enough to reach from CTRL to DEL).
then from the IP I can know which server
on the network I need to map to, so I can issue another

system("command.com net use z: \\xyzserver\myshare mypw /User:me")

You probably don't want a literal string here, as you've indicated
that some of the content of this string is dependent on your
IP address information. What is \m inside a string? You probably
need to double up on all the backslashes inside a quoted string literal.
to map the drive, I need to also check if the net use worked correctly.

Does "net use" return status to the shell, which might be returned
to system() (all together now:) IN A SYSTEM-DEPENDENT MANNER?

Is there a way of testing whether it worked? E.g. try to open
a file you know is supposed to exist, and if it fails, your drive mapping
may have failed.
 
A

Andrew Poelstra

See, this is why we avoid offtopic answers. To anyone under the age of
around 20, DOS means a Windows command prompt, not your actual DOS...

I detest that comment. I've used DOS for longer than I've used Windows.
I never really learned to like those BSOD's. ;-)

(And I'm under 20.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,263
Messages
2,571,064
Members
48,769
Latest member
Clifft

Latest Threads

Top