LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-24-2009, 03:20 AM   #1
dwijesh
LQ Newbie
 
Registered: Apr 2009
Posts: 8

Rep: Reputation: 0
How can I get the ip address of my host system


Hello
I have tried to get the ip address of my host mechine , I have used the following code but here I am geting only MAC address of eth0.

How can I find mac address of eth1 and also init addr of ppp0 when I will connect any data card,

Actually I have to make a C equivalent code of the following shell script


ifconfig eth0 up
eth0=`echo ""`
echo $eth0
for fn in `ifconfig eth0 | grep "Ethernet HWaddr" | sed "s/.*Ethernet HWaddr \(.*\) .*/\1/"`; do
eth0=`echo "$fn$eth0"`;
done
echo $eth0 > eth0.txt

sleep 10
wvdial&
sleep 30
ifconfig ppp0 up
ping valuefirstmessaging.com > /dev/null &
ppp0=`echo ""`
echo $ppp0
for fn in `ifconfig ppp0 | grep "inet addr" | cut -d: -f2 | awk '{print $1}'`; do
ppp0=`echo "$fn$ppp0"`;
done
echo $ppp0 > ppp0.txt



the c code is for getting the eth0 address is

#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#ifdef Linux
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <linux/if.h>
#endif

#ifdef HPUX
#include <netio.h>
#endif

#ifdef AIX
#include <sys/ndd_var.h>
#include <sys/kinfo.h>
#endif

long mac_addr_sys ( u_char *addr)
{
/* implementation for Linux */
#ifdef Linux
struct ifreq ifr;
struct ifreq *IFR;
struct ifconf ifc;
char buf[1024];
int s, i;
int ok = 0;

s = socket(AF_INET, SOCK_DGRAM, 0);
if (s==-1) {
return -1;
}

ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = buf;
ioctl(s, SIOCGIFCONF, &ifc);

IFR = ifc.ifc_req;
for (i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; IFR++) {

strcpy(ifr.ifr_name, IFR->ifr_name);
if (ioctl(s, SIOCGIFFLAGS, &ifr) == 0) {
if (! (ifr.ifr_flags & IFF_LOOPBACK)) {
if (ioctl(s, SIOCGIFHWADDR, &ifr) == 0) {
ok = 1;
break;
}
}
}
}

close(s);
if (ok) {
bcopy( ifr.ifr_hwaddr.sa_data, addr, 6);
}
else {
return -1;
}
return 0;
#endif

/* implementation for HP-UX */
#ifdef HPUX

#define LAN_DEV0 "/dev/lan0"

intfd;
struct fisiocnt_block;
inti;
charnet_buf[sizeof(LAN_DEV0)+1];
char*p;

(void)sprintf(net_buf, "%s", LAN_DEV0);
p = net_buf + strlen(net_buf) - 1;

/*
* Get 802.3 address from card by opening the driver and interrogating it.
*/
for (i = 0; i < 10; i++, (*p)++) {
if ((fd = open (net_buf, O_RDONLY)) != -1) {
iocnt_block.reqtype = LOCAL_ADDRESS;
ioctl (fd, NETSTAT, &iocnt_block);
close (fd);

if (iocnt_block.vtype == 6)
break;
}
}

if (fd == -1 || iocnt_block.vtype != 6) {
return -1;
}

bcopy( &iocnt_block.value.s[0], addr, 6);
return 0;

#endif /* HPUX */

/* implementation for AIX */
#ifdef AIX

int size;
struct kinfo_ndd *nddp;

size = getkerninfo(KINFO_NDD, 0, 0, 0);
if (size <= 0) {
return -1;
}
nddp = (struct kinfo_ndd *)malloc(size);

if (!nddp) {
return -1;
}
if (getkerninfo(KINFO_NDD, nddp, &size, 0) < 0) {
free(nddp);
return -1;
}
bcopy(nddp->ndd_addr, addr, 6);
free(nddp);
return 0;
#endif

/* Not implemented platforms */
return -1;
}

/***********************************************************************/
/*
* Main (only for testing)
*/
#ifdef MAIN
int main( int argc, char **argv)
{
long stat;
int i;
u_char addr[6];
FILE *fp;
fp = fopen("eth0.txt","w");
stat = mac_addr_sys( addr);
if (0 == stat) {
//printf( "MAC address = ");
for (i=0; i<6; ++i) {
fprintf(fp,"%2.2x", addr[i]);
}
printf( "\n");
}
else {
fprintf( stderr, "can't get MAC address\n");
exit( 1);
}
return 0;
}
#endif
/****************************************************************************************
E.g you want to use on Linux, save it as file.c simply compile like this:

gcc -O2 -DMAIN -DLinux file.c -o file

And run it

./file
MAC address = 0008c7e9e386

Compare with /sbin/ifconfig result:

/sbin/ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:08:C7:E9:E3:86
inet addr:***.***.***.*** Bcast:***.***.***.*** Mask:255.255.255.0
inet6 addr: ********************* Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:265555947 errors:0 dropped:0 overruns:0 frame:0
TX packets:50507373 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:4090310752 (3.8 GiB) TX bytes:1085018636 (1.0 GiB)

*********************************************************************************************/
please respond me.
Thanks in advance
Dwijesh.
 
Old 04-25-2009, 03:50 AM   #2
janhe
Member
 
Registered: Jul 2007
Location: Belgium
Distribution: slackware64 14.2, slackware 13.1
Posts: 371

Rep: Reputation: 54
please put fragments of code inside code tags (code and /code between [ and ] )

you can read up with
Code:
man grep
man ifconfig 
man sed
man cut
also, look at the output from the following command in the terminal:
Code:
ifconfig
That should get you going
 
Old 04-25-2009, 07:42 AM   #3
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,925
Blog Entries: 44

Rep: Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159
Hi,

I agree that using the vbcode tag '#' for code will make your post cleaner.

You should also 'man ethtool' along with janhe's suggestions.
 
Old 04-27-2009, 04:53 AM   #4
dwijesh
LQ Newbie
 
Registered: Apr 2009
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by janhe View Post
please put fragments of code inside code tags (code and /code between [ and ] )

you can read up with
Code:
man grep
man ifconfig 
man sed
man cut
also, look at the output from the following command in the terminal:
Code:
ifconfig
That should get you going

Thanks for your kind replay ,please suggest me how can I ping a URL
like ping valuefirstmessaging.com through c code I have tried to use
cURLpp which is a c++ application . I want c code to implement actually wgen I am calling the .cpp file where all the entire code is writen in like c and compiled with g++ I get a error at realloc() . Is there any function in c++ that can be replaced realloc().

Thanks & Regards
Dwijesh
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Installing LILO from host system to hdb to boot as hda in target system A7G1-6 Linux - Hardware 2 07-25-2009 10:37 AM
windows server 2003 as a host system for LINUX guest system VPS h@foorsa.biz Linux - General 2 09-22-2008 06:17 AM
is there a canonical method to get host IP address? bigearsbilly Linux - Networking 2 10-24-2006 07:57 AM
Error IP address already used by another host lngmn7 Linux - Networking 2 08-29-2005 04:41 AM
eth0 and host address tommytomato Linux - Newbie 2 12-03-2003 04:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration