dwhitney67
October 24th, 2007, 02:59 AM
I've been trying to obtain a system's IP address, netmask, and broadcast IP programatically using a combination of C++ and shell scripting.
I am limited to using basic shell scripting constructs because I am using the ash-shell. It is similar to bash, but does not support the full set of features.
Here's what I got so far:
#include <string>
#include <fstream>
#include <iostream>
#include <unistd.h> // for unlink()
...
// attempt to obtain ifconfig information
system( "echo -n 'IP ' > /tmp/sysinfo" );
system( "/sbin/ifconfig eth0 2> /dev/null"
"| /bin/grep -m 1 addr: | /usr/bin/cut -d : -f2"
"| /usr/bin/cut -d ' ' -f1 >> /tmp/sysinfo;" );
system( "echo -n 'BCAST ' >> /tmp/sysinfo" );
system( "/sbin/ifconfig eth0 2> /dev/null"
"| /bin/grep -m 1 Bcast: | /usr/bin/cut -d : -f3"
"| /usr/bin/cut -d ' ' -f1 >> /tmp/sysinfo;" );
system( "echo -n 'MASK ' >> /tmp/sysinfo" );
system( "/sbin/ifconfig eth0 2> /dev/null"
"| /bin/grep -m 1 Mask: | /usr/bin/cut -d : -f4 >> /tmp/sysinfo;" );
// read ifconfig information from flat-file
std::string ipAddr;
std::string broadcast;
std::string netmask;
std::string fieldID;
std::ifstream sysinfo( "/tmp/sysinfo" );
if ( sysinfo )
{
sysinfo >> fieldID >> ipAddr;
sysinfo >> fieldID >> broadcast;
sysinfo >> fieldID >> netmask;
sysinfo.close();
unlink( "/tmp/sysinfo" );
}
std::cout << "IP = " << ipAddr << std::endl;
std::cout << "BCAST = " << broadcast << std::endl;
std::cout << "MASK = " << netmask << std::endl;
...
I've looked for a C-library function that can mimic or provides similar features as ifconfig, however I have come up empty. Is there an easier way to get the system's ethernet information?
Btw, the code above works (under Linux), however only when the system is connected to the LAN. If it is not connected, then ifconfig still reports eth0 info, but not the data I require, thus the contents in the flat-file is not built correctly --- and hence the reason I am seeking help!
P.S. My system does not have Python, Perl, Bash, or even sed. I am limited to C++, C, and Ash-shell script.
I am limited to using basic shell scripting constructs because I am using the ash-shell. It is similar to bash, but does not support the full set of features.
Here's what I got so far:
#include <string>
#include <fstream>
#include <iostream>
#include <unistd.h> // for unlink()
...
// attempt to obtain ifconfig information
system( "echo -n 'IP ' > /tmp/sysinfo" );
system( "/sbin/ifconfig eth0 2> /dev/null"
"| /bin/grep -m 1 addr: | /usr/bin/cut -d : -f2"
"| /usr/bin/cut -d ' ' -f1 >> /tmp/sysinfo;" );
system( "echo -n 'BCAST ' >> /tmp/sysinfo" );
system( "/sbin/ifconfig eth0 2> /dev/null"
"| /bin/grep -m 1 Bcast: | /usr/bin/cut -d : -f3"
"| /usr/bin/cut -d ' ' -f1 >> /tmp/sysinfo;" );
system( "echo -n 'MASK ' >> /tmp/sysinfo" );
system( "/sbin/ifconfig eth0 2> /dev/null"
"| /bin/grep -m 1 Mask: | /usr/bin/cut -d : -f4 >> /tmp/sysinfo;" );
// read ifconfig information from flat-file
std::string ipAddr;
std::string broadcast;
std::string netmask;
std::string fieldID;
std::ifstream sysinfo( "/tmp/sysinfo" );
if ( sysinfo )
{
sysinfo >> fieldID >> ipAddr;
sysinfo >> fieldID >> broadcast;
sysinfo >> fieldID >> netmask;
sysinfo.close();
unlink( "/tmp/sysinfo" );
}
std::cout << "IP = " << ipAddr << std::endl;
std::cout << "BCAST = " << broadcast << std::endl;
std::cout << "MASK = " << netmask << std::endl;
...
I've looked for a C-library function that can mimic or provides similar features as ifconfig, however I have come up empty. Is there an easier way to get the system's ethernet information?
Btw, the code above works (under Linux), however only when the system is connected to the LAN. If it is not connected, then ifconfig still reports eth0 info, but not the data I require, thus the contents in the flat-file is not built correctly --- and hence the reason I am seeking help!
P.S. My system does not have Python, Perl, Bash, or even sed. I am limited to C++, C, and Ash-shell script.