473,505 Members | 13,925 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

get the ip address and map a drive according to IP

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
12 3501


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
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
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
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
#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
Not sure why

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

worked.

if you say ipconfig.exe then would not work.

Greg

Aug 14 '06 #7
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
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
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
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
>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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6038
by: Mr. Smith | last post by:
Hi All. I need to be able to determine the IP address a drive letter is mapped to. When a user selects a file from a file open dialog, They will, as usual, navigate to the file they want via;...
21
1746
by: Stephen Biggs | last post by:
Given this code: void f(void){} int main(void){return (int)f+5;} Is there anything wrong with this in terms of the standards? Is this legal C code? One compiler I'm working with compiles this...
27
2207
by: Adam Warner | last post by:
Hi all, In the code snippet below I successfully determine the address of val1:* struct o val1=l_SYM_2B(&a).o; print_aesthetic(&val1); The structure o is heavyweight. I understand...
0
2479
by: john doe | last post by:
How can I use WMI or a WqlObjectQuery to find the hard drive letter of the physical drive location index. For example the following code will give me the physical drive location:...
9
2154
by: Andy B | last post by:
If I bought one of these boxes/OS combos as a postgresql database server, would postgresql be able to make the best use of it with a huge (e.g. 40GB) database? Box: HP ProLiant DL585, with ...
18
5089
by: Joe Lester | last post by:
This thread was renamed. It used to be: "shared_buffers Question". The old thread kind of died out. I'm hoping to get some more direction by rephrasing the problem, along with some extra...
0
1066
by: Nasiq Ali | last post by:
I have a logical drive which is mapped to a folder in a remote machine . Due to some secuirity reasons the drive can't be accessed from a C#/C++ program . Do Windows have any API which can be used...
10
22583
by: kevinliu23 | last post by:
HI, I am new to Python and wanted to know how to check for the remaining disk space on my Windows machine using Python? I was thinking of using the command line "dir" and trying to extract the...
17
3185
by: ayush patel | last post by:
Hi all, I have created a windows service that has a file watcher and does some operation after it reads that file. i developed it in my local machine and installed the service on server. i have...
0
7213
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4698
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1526
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
406
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.