473,770 Members | 2,004 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\mys hare 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 3537


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\mys hare 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("ipcoinf ig.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\mys hare 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.ukwro te:
>gr******@wendy s.com wrote:
>system("comman d.com net use z: \\xyzserver\mys hare 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.ukwro te:
>gr******@wendys .com wrote:
>>system("comma nd.com net use z: \\xyzserver\mys hare 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("ipconfi g.exe ip2.txt");
return 0;
}

I tried to use

system("ipconfi g.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_Keit h) 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******@wendy s.comwrote:
I tried to use

system("ipconfi g.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\syst em32>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\SYST EM32>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("ipconfi g " TEMP_FILENAME);
fp = fopen(TEMP_FILE NAME, "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_addr esses[i++], p+2, MAX_IP_LEN - 1);
if(i == MAX_IP_NUM) break;
}
}
}
remove(TEMP_FIL ENAME);
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.20 4.1"
IP 1: "192.168.15 4.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6055
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; Drive letter, folder, folder etc. The Windows file open dialog returns this path in the format C:\folder\folder\file.ext. What I need to know is what is the IP of the C:\ drive or any drive they select, be it P; U; or whatever.
21
1798
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 quietly, even with the most stringent and pedantic ANSI and warning levels, but generates code that only loads the address of "f" and fails to make the addition before returning a value from "main".
27
2250
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 (hopefully correctly) that (barring compiler optimisations) C will shallow copy the structure into val1.
0
2500
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: StringCollection propNames = new StringCollection(); ManagementClass driveClass = new ManagementClass("Win32_DiskDrive"); PropertyDataCollection props = driveClass.Properties; foreach (PropertyData driveProperty in props) { propNames.Add(driveProperty.Name);
9
2185
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 4 AMD64 CPUs and 64GB of RAM. (other vendor options also exist) OS: SUSE enterprise 8 linux for AMD (links to product info at bottom)
18
5155
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 observations I've recently made. The core of the problem is that Postgres is filling up my hard drive with swap files at the rate of around 3 to 7 GB per week (that's Gigabytes not Megabytes) . At this rate it takes roughly two months to fill up my 40...
0
1082
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 frm a C# application ,so that if I provide the API with the logical drive name it will give back the physical address of the drive e.g. If I pass Z: then it should return //MyMachine/ABC
10
22649
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 output from there. But I'm not sure how to extract command line strings using Python either. Anyway help would be appreciated. :)
17
3218
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 changed the file path and everythng according to server drives. we all in the office share some network drives in which ppl will keep pasting that file and the service on server has to pick it up. which its not doing. when i paste it in netwrok...
0
9618
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10101
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8933
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
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 we have to send another system

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.