I'm a linux user and would like to create a basic program that outputs the local and internet IP that is currently in use by the machine. Does anyone know where I might obtain these 2 things from the system?
I'm a linux user and would like to create a basic program that outputs the local and internet IP that is currently in use by the machine. Does anyone know where I might obtain these 2 things from the system?
When you say local do you mean 127.0.0.1, because it will always be that. Or do you mean you have 2 NICs in the box and 1 connects to the Internet and another one connects to a LAN?
If you understand what you're doing, you're not learning anything.
I should have been a bit more specific. What I am after is the IP you are assigned by a central server. My setup is that computer 1 assigns me an IP. I want to find that IP and the current IP given to me by my internet service provider.
The /etc/hosts file - the first valid entry is the node name and node IP address of the current box the process is running on.
See also gethostbyaddr() or gethostent() in the man pages.
uname() also shows the node name in the utsname struct.
This example doesn't work but good luck fixing it:
Code:#include <stdio.h> #include <stdlib.h> #include <al/etl.h> #include <sfl.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #define EXIT_CRAP 1 #define SOME_PORT_I_GUESS 80 void ........( const char *msg ) { printf( "........: %s", msg ); exit( EXIT_CRAP ); } void makeReceiverSA(struct sockaddr_in *sa, int port) { sa->sin_family = AF_INET; sa->sin_port = htons( port ); sa-> sin_addr.s_addr = htonl( INADDR_ANY ); } int main( void ) { struct sockaddr_in STUPID_SOCKET; int s; if( ( s = socket( AF_INET, SOCK_DGRAM, 0 ) ) < 0 ) ........( "Stupid ass sockets" ); makeReceiverSA( &STUPID_SOCKET, SOME_PORT_I_GUESS ); printf( "sa = %d, %s, %d\n", STUPID_SOCKET.sin_family, inet_ntoa( STUPID_SOCKET.sin_addr ), ntohs( STUPID_SOCKET.sin_port ) ); if( bind( s, ( struct sockaddr * )&STUPID_SOCKET, sizeof( struct sockaddr_in ) ) != 0 ) { close( s ); ........( "WTF is bind!?\n" ); } close( s ); return EXIT_SUCCESS; }