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: IP Lookup command


On Wed, 30 May 2001 18:21:00 +0200, Fabrice Rouillier <Rouillier@tcl.ite.mee.com> wrote:
> 
> How can I get system information within cygwin env.
> 
> My need is to get hostid.

On Thu, 31 May 2001 11:19:33 -0600, C. Porter Bassett <porter@et.byu.edu> wrote:
> 
> I need a command that I can call in the following manner:
> $ unknown_command host_on_my_network
> 192.168.20.119

Attached is a program I wrote some time ago, it is very similar to the
code sent by Francis Litterio <litterio@pyxsys.net>. It was tested on
Linux and Cygwin.

Run it without any arguments to get your hostname and IP (i.e. myhostip).
On a machine with more than one NIC (e.g. real NIC and Dial connection)
you'll get both IPs. You can use the program with -1 or -2 to get the
1st or 2nd IP in scripts (i.e. IP=`myhostip -1`).

To get the IP for a HOST do: IP=`myhostip -1 HOST`
To see all the IPs for a HOST do: myhostip HOST (e.g. myhostip cnn.com).

Ehud.


-- 
 Ehud Karni     Mivtach - Simon  Insurance   /"\
 Tel: +972-3-6212-757 Fax: +972-3-6292-544   \ /  ASCII Ribbon Campaign
 (USA) Fax and  voice  mail: 1-815-5509341    X   Against  HTML  Mail
     Better     Safe     Than     Sorry      / \
     mailto:ehud@unix.simonwiesel.co.il    http://www.simonwiesel.co.il
/* ip-check - Display IP for host

   use: myhostip [-n/-<1-9>] [host-name]
   if host-name is not given - use local host name.
   optional 1st arg: omitted - report all ip's of this name
                     -n/-0   - report name only (same as `hostname' or argv[1])
                     -i/-1   - report 1st ip only
                     -<2-9>  - report ip #2 ... #9
*/

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */

int main ( int argc , char *argv [ ] )
{
struct  hostent *hent ;                        /* host entry */
char svname [ 250 ] = "" ;                     /* empty host for start */
int req = -1 ;                                 /* request -1=normal, 0-name, 1-n=ip[n] */
int ix = 0 ;
unsigned char *ad ;
char *myname = svname ;
int p = 1 ;

   while ( p < argc )
   {
       if ( argv [ p ] [ 0 ] == '-' )          /* parameter (not host name) */
           switch ( argv [ p ] [ 1 ] | ('a' ^ 'A') )   /* 2nd char (lower case) */
           {
               case 'n':                       /* -n[ame] */
                       req = 0 ;
                       break ;                 /* OK */

               case 'i':                       /* -i[p] */
                       req = 1 ;
                       break ;                 /* OK */

               case '0':                       /* -0 = -name */
               case '1':                       /* -1 = -ip */
               case '2':                       /* -2 = ip 2 */
               case '3':                       /* -3 = ip 3 */
               case '4':                       /* -4 = ip 4 */
               case '5':                       /* -5 = ip 5 */
               case '6':                       /* -6 = ip 6 */
               case '7':                       /* -7 = ip 7 */
               case '8':                       /* -8 = ip 8 */
               case '9':                       /* -9 = ip 9 */
                       req = argv [ p ] [ 1 ] - '0' ;
                       break ;                 /* OK */

               default:
                       fprintf ( stderr , "Arg %d (%s) illegal, should be -n, -i -[2-9]\n" ,
                                 p , argv [ p ] ) ;
                       exit ( 3 ) ;
           }
       else
           myname = argv [ p ] ;               /* use this as host name */

       p++ ;                                   /* next arg */
   }


   if ( ( *myname == 0 ) &&                    /* empty host name */
        ( gethostname ( myname , 250 ) < 0 ) ) /* get hostname - must be successfull */
       {
           fprintf ( stderr , "Gethostname failed\n" ) ;
           exit ( 1 ) ;
       }

   if ( req < 0 )                              /* no stdout request */
       fprintf ( stderr , "Hostname to check is %s\n" , myname ) ;

   if ( req == 0 )                             /* my hostname only */
   {
       printf ( "%s\n" , myname ) ;            /* name + Newline */
       exit ( 0 ) ;                            /* exit (OK) */
   }


   hent = gethostbyname ( myname ) ;           /* search host */

   if ( hent == NULL )
   {
       fprintf ( stderr , "Gethostbyname failed\n" ) ;
       exit ( 2 ) ;
   }

   req -- ;                                    /* -2 or 0-8 */
   while ( hent -> h_addr_list [ ix ] != NULL )
   {
       ad = hent -> h_addr_list [ ix ] ;
       if ( req == ix )
       {
           printf ( "%d.%d.%d.%d\n" , *ad , *(ad+1) , *(ad+2) , *(ad+3) ) ;
           exit ( 0 ) ;                        /* exit (OK) */
       }

       if ( req < 0 )                          /* output every address ? */
           fprintf ( stderr , "Address %d: %d.%d.%d.%d\n" ,
                     ix + 1 , *ad , *(ad+1) , *(ad+2) , *(ad+3) ) ;
       ix ++ ;                                 /* next ip address */
   }

   exit ( 0 ) ;
}
/*===========================================================================*/

--
Want to unsubscribe from this list?
Check out: http://cygwin.com/ml/#unsubscribe-simple

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