By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,341 Members | 1,407 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,341 IT Pros & Developers. It's quick & easy.

get the ip address and map a drive according to IP

P: n/a
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

Aug 14 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a


gr******@wendys.com wrote On 08/14/06 11:56,:
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.

--
Er*********@sun.com

Aug 14 '06 #2

P: n/a
gr******@wendys.com wrote:
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.
--
Flash Gordon
Still sigless on this computer.
Aug 14 '06 #3

P: n/a
In article <l6************@news.flash-gordon.me.uk>,
Flash Gordon <sp**@flash-gordon.me.ukwrote:
>gr******@wendys.com wrote:
>system("command.com net use z: \\xyzserver\myshare mypw /User:me")
>Same comments as before. Also you need to consider that the / character
has special meaning in C strings so you probably wanted "... //User:me"
Is it time for more caffine, Flash? ;-)
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Aug 14 '06 #4

P: n/a
Walter Roberson wrote:
In article <l6************@news.flash-gordon.me.uk>,
Flash Gordon <sp**@flash-gordon.me.ukwrote:
>gr******@wendys.com wrote:
>>system("command.com net use z: \\xyzserver\myshare mypw /User:me")
>Same comments as before. Also you need to consider that the / character
has special meaning in C strings so you probably wanted "... //User:me"

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.
--
Flash Gordon
Still sigless on this computer
Aug 14 '06 #5

P: n/a
#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

Aug 14 '06 #6

P: n/a
Not sure why

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

worked.

if you say ipconfig.exe then would not work.

Greg

Aug 14 '06 #7

P: n/a
gr******@wendys.com writes:
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().)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 14 '06 #8

P: n/a
On 2006-08-14, gr******@wendys.com <gr******@wendys.comwrote:
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.

--
Andrew Poelstra <http://www.wpsoftware.net/projects>
To reach me by email, use `apoelstra' at the above domain.
"Do BOTH ends of the cable need to be plugged in?" -Anon.
Aug 15 '06 #9

P: n/a
gr******@wendys.com wrote:
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.)

--
Simon.
Aug 15 '06 #10

P: n/a
On Tue, 15 Aug 2006 12:43:14 +1000, in comp.lang.c , Simon Biber
<ne**@ralmin.ccwrote:
>gr******@wendys.com wrote:
>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:
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
Aug 15 '06 #11

P: n/a
>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.
Aug 15 '06 #12

P: n/a
On 2006-08-15, Mark McIntyre <ma**********@spamcop.netwrote:
On Tue, 15 Aug 2006 12:43:14 +1000, in comp.lang.c , Simon Biber
<ne**@ralmin.ccwrote:
>>gr******@wendys.com wrote:
>>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:

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.)

--
Andrew Poelstra <http://www.wpsoftware.net/projects>
To reach me by email, use `apoelstra' at the above domain.
"Do BOTH ends of the cable need to be plugged in?" -Anon.
Aug 16 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.