hanche@imf.unit.no (Harald Hanche-Olsen) (02/27/91)
In article <9102261044.AA02546@apo.esiee.fr> bonnetf@apo.esiee.fr (bonnet-franck) writes:
Does somebody knows how to get the internet address of a machine
when I am working on it ( whithout looking in /etc/hosts ) ?
% /etc/ifconfig eth0
eth0: flags=63<UP,BROADCAST,NOTRAILERS,RUNNING>
inet 129.241.15.4 netmask ffff0000 broadcast 129.241.255.255 macaddr 8:0:1e:1:ba:a4
I leave the interpretation of the output as an exercise :-)
- Harald Hanche-Olsen <hanche@imf.unit.no>
Division of Mathematical Sciences
The Norwegian Institute of Technology
N-7034 Trondheim, NORWAY
beierl_c@apollo.HP.COM (Christopher Beierl) (02/28/91)
In article <9102261044.AA02546@apo.esiee.fr> bonnetf@apo.esiee.fr (bonnet-franck) writes: >Hi, > >Does somebody knows how to get the internet address of a machine >when I am working on it ( whithout looking in /etc/hosts ) ? How about /etc/ifconfig <interface> where <interface> is one of dr0, dr1, eth0, etc. Apollo Token Ring dr0,1 IEEE 802.3 (ETHERNET) eth0,1 IEEE 802.5 (IBM Token Ring) itr0,1 Serial Line (SLIP) sl0 -Chris Here's a little program which interprets the network address info supplied by ifconfig. I have found it useful for explaining and understanding network addresses, subnetting, etc. --------------------------- Cut Here ---------------------------------- /* ** Pipe the output of "/etc/ifconfig <net>" into this program */ #include <stdio.h> #define MAXLEN 256 main() { int net_shift, subnet_shift; char buf[MAXLEN], *ptr, class; unsigned a1, a2, a3, a4, n1, n2, n3, n4, s1, s2, s3, s4, b1, b2, b3, b4; unsigned net, subnet, host, address, local_address, net_mask, subnet_mask; void print_bin(), print_hex(); fgets(buf, MAXLEN, stdin); printf(buf); if (fgets(buf, MAXLEN, stdin)) { printf(buf); for (ptr = buf; *ptr; ++ptr) { if (*ptr == '.') { *ptr = ' '; } } if (sscanf(buf, "%*s%u%u%u%u%*s%x%*s%u%u%u%u", &a1, &a2, &a3, &a4, &subnet_mask, &b1, &b2, &b3, &b4) == 9) { address = (a1 << 24) + (a2 << 16) + (a3 << 8) + a4; if ((address & 0x80000000) == 0) { class = 'A'; net_mask = 0xff000000; net_shift = 24; } else if ((address & 0x40000000) == 0) { class = 'B'; net_mask = 0xffff0000; net_shift = 16; } else if ((address & 0x20000000) == 0) { class = 'C'; net_mask = 0xffffff00; net_shift = 8; } else { class = 'D'; net_mask = 0xfffffff; net_shift = 4; } n1 = (net_mask & 0xff000000) >> 24; n2 = (net_mask & 0x00ff0000) >> 16; n3 = (net_mask & 0x0000ff00) >> 8; n4 = net_mask & 0x000000ff; s1 = (subnet_mask & 0xff000000) >> 24; s2 = (subnet_mask & 0x00ff0000) >> 16; s3 = (subnet_mask & 0x0000ff00) >> 8; s4 = subnet_mask & 0x000000ff; for (subnet_shift = 0; subnet_shift < net_shift; ++subnet_shift) { if (subnet_mask & (1 << subnet_shift)) { break; } } printf("\nADDRESS\n"); printf("(decimal) = %6u . %6u . %6u . %6u\n", a1, a2, a3, a4); print_hex(a1, a2, a3, a4); print_bin(a1, a2, a3, a4); printf("\nBROADCAST ADDRESS\n"); printf("(decimal) = %6u . %6u . %6u . %6u\n", b1, b2, b3, b4); print_hex(b1, b2, b3, b4); print_bin(b1, b2, b3, b4); printf("\nNET MASK\n"); printf("(decimal) = %6u . %6u . %6u . %6u\n", n1, n2, n3, n4); print_hex(n1, n2, n3, n4); print_bin(n1, n2, n3, n4); printf("\nSUBNET MASK\n"); printf("(decimal) = %6u . %6u . %6u . %6u\n", s1, s2, s3, s4); print_hex(s1, s2, s3, s4); print_bin(s1, s2, s3, s4); net = (address & net_mask) >> net_shift; subnet = ((address & ~net_mask) & subnet_mask) >> subnet_shift; host = address & ~subnet_mask; printf("\nThis means:\n"); printf(" Class: %c\n", class); printf(" Net: %d (%x hex)\n", net, net); printf(" Subnet: %d (%x hex)\n", subnet, subnet); printf(" Host: %d (%x hex)\n", host, host); } } } void print_hex(v1, v2, v3, v4) unsigned v1, v2, v3, v4; { void p_hex(); printf(" (hex) = "); p_hex(v1); printf(" . "); p_hex(v2); printf(" . "); p_hex(v3); printf(" . "); p_hex(v4); putchar('\n'); } void p_hex(d) unsigned d; { printf("%3x %3x", (d & 0xf0) >> 4, d & 0xf); } void print_bin(v1, v2, v3, v4) unsigned v1, v2, v3, v4; { void p_bin(); printf(" (binary) = "); p_bin(v1, 8); printf(". "); p_bin(v2, 8); printf(". "); p_bin(v3, 8); printf(". "); p_bin(v4, 8); putchar('\n'); } void p_bin(d, bits) unsigned d; int bits; { for (--bits; bits >= 0; --bits) { putchar((d & (1 << bits)) ? '1' : '0'); if (!(bits % 4)) { putchar(' '); } } } --------------------------- Cut Here ---------------------------------- -- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= Christopher T. Beierl Internet: beierl_c@apollo.HP.COM;beierl_c@apollo.com Apollo Computer, Inc. UUCP: {mit-eddie,yale,uw-beaver}!apollo!beierl_c A Subsidiary of Hewlett-Packard Phone: (508) 256-6600
ced@apollo.HP.COM (Carl Davidson) (02/28/91)
From article <9102261044.AA02546@apo.esiee.fr>, by bonnetf@apo.esiee.fr (bonnet-franck): > Hi, > > Does somebody knows how to get the internet address of a machine > when I am working on it ( whithout looking in /etc/hosts ) ? > I use a shell command: % /etc/ping `/etc/hostname` 64 1 which says: ping the host I'm logged into once, with a 64 byte packet. If you need to get just the Internet address, you can extract it from the with your favorite test reprocessor, i.e. sed, awk, perl,... Carl Davidson (508) 256-6600 x4361 | The Apollo Systems Divison of | It doesn't TAKE all kinds, The Hewlett-Packard Company | there just ARE all kinds. DOMAIN: ced@apollo.HP.COM |
krowitz@RICHTER.MIT.EDU (David Krowitz) (02/28/91)
The command "/etc/ping my_favorite_hostname" returns the IP number of the node in question, along with whether or not the node's TCP/IP daemon is up and running. == Dave
etb@milton.u.washington.edu (Eric Bushnell) (02/28/91)
/etc/hostid returns the ip address in hex, which isn't terribly difficult to translate to dot-format-decimal. Eric Bushnell
jf@ap.co.umist.ac.uk (John Forrest) (03/02/91)
In article <HANCHE.91Feb26215414@hufsa.imf.unit.no> hanche@imf.unit.no (Harald Hanche-Olsen) writes: >In article <9102261044.AA02546@apo.esiee.fr> bonnetf@apo.esiee.fr (bonnet-franck) writes: > > Does somebody knows how to get the internet address of a machine > when I am working on it ( whithout looking in /etc/hosts ) ? > >% /etc/ifconfig eth0 >eth0: flags=63<UP,BROADCAST,NOTRAILERS,RUNNING> > inet 129.241.15.4 netmask ffff0000 broadcast 129.241.255.255 macaddr 8:0:1e:1:ba:a4 > Surely this does not give the hostid of a machine, but of a port - not necessarily the same thing (esp if there is more than one port). I expect you need to use a program that invokes gethostid, but it is a pure guess, and I haven't tried looking further. John Forrest Dept of Computation UMIST
jf@ap.co.umist.ac.uk (John Forrest) (03/03/91)
In article <5011a909.20b6d@apollo.HP.COM> ced@apollo.HP.COM (Carl Davidson) writes: >From article <9102261044.AA02546@apo.esiee.fr>, by bonnetf@apo.esiee.fr (bonnet-franck): >> Hi, >> >> Does somebody knows how to get the internet address of a machine >> when I am working on it ( whithout looking in /etc/hosts ) ? >> > >I use a shell command: > > % /etc/ping `/etc/hostname` 64 1 > >which says: > > ping the host I'm logged into once, with a 64 byte packet. > >If you need to get just the Internet address, you can extract it from the >with your favorite test reprocessor, i.e. sed, awk, perl,... > I suddenly found myself needing a unique number for each workstation, and the Internet address seemed more portably than trying to discover how to get hold of the Apollo node id. Anyway, based on what I said before, gethostid(2) and/or hostid(1) seem the easiest way. Unfortunately on our OS10.1 systems you actually have to set this up (probably latter versions as well) by adding: hostid `hostname` to /etc/rc.local (preferably at the end once BIND and everything are working ok). Having done this, you can simply invoke: hostid from a shell script, or use: extern long gethostid(void) from a program. Easy isn't it, with no need of network usage nor sed/awk/perl scripts. Of course, hostid does return a hex value, so if you want it in some otherform, you had best write a program that invokes gethostid and prints out the value in the style you want. John Forrest Dept of Computation UMIST