This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: read the IP automatically???


if you just want to run the program on cygwin, then simple enough, you 
can just parse the output from ipconfig.exe

but if you want to maintain a cross compatibility with linux, then i 
have a gift for you. it is a ipconfig to dhcpcd wrapper. it is written 
in perl and it emulates a subset of what linux dhcpcd has to offer. 
after you run dhcpcd, the file containing gateway and interface IP is 
generated at /etc/dhcpc/dhcpcd-ethn.info (whereas ethn is the linux 
style interface name, such as eth0, eth1, eth2, ...)

this is a primitive hack, and it has proven to be workable.

installation: just put the file in /sbin where linux usually put it.

if you run into problems, first check if you have /etc/dhcpc directory 
set with a permission that dhcpcd can access. dhcpcd is a user mode 
program that does not bear special privilege. also, it does not perform 
error checking. so if there is an error with ipconfig, you will get an 
empty /etc/dhcpc/dhcpcd-ethn.info file. there will be more ways to break 
dhcpcd, so please bear with me. i haven't worked on it for a long time, 
and if it is needed, i *could* ...

liulk

hongxun lee wrote:

>Hi, all
>cygwin/xfree is installed on win2000. Is it possible to read the IP
>automatically (and then exported to a variale for display configration)
>whenevr xwindow is started? Thanks
>
>
>
>--
>Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
>Bug reporting:         http://cygwin.com/bugs.html
>Documentation:         http://cygwin.com/docs.html
>FAQ:                   http://cygwin.com/faq/
>
>


#!/usr/bin/perl -w
# emulated dhcpcd daemon for cygwin on win2k
# we need access to the system ipconfig
# this is a very crude hack.

# first of all, the original win2k ipconfig must be visible in the file
# system. supply the complete path here.

$sys_ipconfig = "/mnt/hdc/winnt/system32/ipconfig.exe";

# list the mapping of linux ethN to win2k interface name using
# associative hash.

$eth = { "eth0" => "Local Area Connection" };

# default action

$action = "RENEW";

foreach $opt (@ARGV)
{
    if ($opt eq "-k") { $action = "RELEASE" }
    elsif ($opt eq "-h")
    {
	print <<EOQ;
dhcpcd - emulated dhcp client daemon for cygwin on win2k.
written by likai liu <liulk\@bu.edu>, version 0.2

this version of dhcpcd emulates a subset of linux dhcpcd functions.
before usage, you must edit this program and change applicable setting
at the beginning of the file, typically the path to ipconfig.exe and
the mapping of linux style ethn name to win2k name.

usage:	dhcpcd [-r] [-k] ethn

	-r	renew the interface ethn
	-k	release the interface ethn

note that, unlike the linux dhcp client daemon, this dhcpcd does not
reside in memory, so -k does not kill any processes but only releases
the IP address.

when you get a new IP address, a dhcp style info file is generated at
/etc/dhcpc/dhcpcd-ethn.info

this file is an unreleased part of cygwin-magixpak project.
EOQ
    }
    elsif ($opt =~ /eth[0-9]/)
    {
	$int = $opt;
    }
}

if (!$int) { $int = "eth0" };

die "dhcpcd: interface undefined: $int" unless ( $eth->{$int} );

if ($action eq "RELEASE")
{
    open (DHCPOUT, "$sys_ipconfig /release \"$eth->{$int}\" |")
	or die "dhcpcd: cannot parse ipconfig output";
    while (<DHCPOUT>)
    {
	if ( $_ =~ /success/ ) {
	    unlink "/etc/dhcpc/dhcpcd-$int.info";
	}
    }

    close DHCPOUT;
}
else
{
    open (DHCPINFO, ">/etc/dhcpc/dhcpcd-$int.info")
	or die "dhcpcd: cannot create dhcp info";
    open (DHCPOUT, "$sys_ipconfig /renew \"$eth->{$int}\" |")
	or die "dhcpcd: cannot parse ipconfig output";
    while (<DHCPOUT>)
    {
	if ( $_ =~ /IP Address.+ : ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/ )
	{
	    print DHCPINFO "IPADDR=$1\n";
	}
	elsif ( $_ =~ /Default Gateway.+ : ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/ )
	{
	    print DHCPINFO "GATEWAY=$1\n";
	}
    }

    close DHCPOUT;
    close DHCPINFO;
}


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]