Skip to content

Instantly share code, notes, and snippets.

@tyhenry
Created May 12, 2019 05:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyhenry/1aa01efef4fa036927d50f62ac8509a8 to your computer and use it in GitHub Desktop.
Save tyhenry/1aa01efef4fa036927d50f62ac8509a8 to your computer and use it in GitHub Desktop.
grab local machine ip addresses (oF)
vector<string> ofApp::getLocalIPs()
{
vector<string> result;
#ifdef TARGET_WIN32
string commandResult = ofSystem("ipconfig");
//ofLogVerbose() << commandResult;
for (int pos = 0; pos >= 0; )
{
pos = commandResult.find("IPv4", pos);
if (pos >= 0)
{
pos = commandResult.find(":", pos) + 2;
int pos2 = commandResult.find("\n", pos);
string ip = commandResult.substr(pos, pos2 - pos);
pos = pos2;
if (ip.substr(0, 3) != "127") // let's skip loopback addresses
{
result.push_back(ip);
//ofLogVerbose() << ip;
}
}
}
#else
string commandResult = ofSystem("ifconfig");
for (int pos = 0; pos >= 0; )
{
pos = commandResult.find("inet ", pos);
if (pos >= 0)
{
int pos2 = commandResult.find("netmask", pos);
string ip = commandResult.substr(pos + 5, pos2 - pos - 6);
pos = pos2;
if (ip.substr(0, 3) != "127") // let's skip loopback addresses
{
result.push_back(ip);
//ofLogVerbose() << ip;
}
}
}
#endif
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment