Created
May 12, 2019 05:52
-
-
Save tyhenry/1aa01efef4fa036927d50f62ac8509a8 to your computer and use it in GitHub Desktop.
grab local machine ip addresses (oF)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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