0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-27 23:27:11 +01:00
mongodb/jstests/libs/host_ipaddr.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

40 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

// Returns non-localhost ipaddr of host running the mongo shell process
export function get_ipaddr() {
// set temp path, if it exists
var path = "";
try {
path = TestData.tmpPath;
if (typeof path == "undefined") {
path = "";
} else if (path.slice(-1) != "/") {
// Terminate path with / if defined
path += "/";
}
} catch (err) {
}
var ipFile = path + "ipaddr-" + Random.srand() + ".log";
var windowsCmd = "ipconfig > " + ipFile;
var unixCmd =
"(/sbin/ifconfig || /usr/sbin/ip addr) | grep 'inet ' | grep -v '127.0.0.1' > " + ipFile;
var ipAddr = null;
var hostType = null;
try {
hostType = getBuildInfo().buildEnvironment.target_os;
// os-specific methods
if (hostType == "windows") {
runProgram('cmd.exe', '/c', windowsCmd);
ipAddr = cat(ipFile).match(/IPv4.*: (.*)/)[1];
} else {
runProgram('/bin/sh', '-c', unixCmd);
ipAddr =
cat(ipFile).replace(/addr:/g, "").match(/inet ([\d]+\.[\d]+\.[\d]+\.[\d]+)/)[1];
}
} finally {
removeFile(ipFile);
}
return ipAddr;
}