summaryrefslogtreecommitdiff
path: root/src/mongo/gotools/test/qa-tests/jstests/libs/host_ipaddr.js
blob: f5412da156329615142931910d8322269e04436b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Returns non-localhost ipaddr of host running the mongo shell process
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) {
    // no testdata
  }

  var ipFile = path+"ipaddr.log";
  var windowsCmd = "ipconfig > "+ipFile;
  var unixCmd = "/sbin/ifconfig | grep inet | grep -v '127.0.0.1' > "+ipFile;
  var ipAddr = null;
  var hostType = null;

  try {
    hostType = getBuildInfo().sysInfo.split(' ')[0];

    // os-specific methods
    if (hostType === "windows") {
      runProgram('cmd.exe', '/c', windowsCmd);
      ipAddr = cat(ipFile).match(/IPv4.*: (.*)/)[1];
    } else {
      runProgram('bash', '-c', unixCmd);
      ipAddr = cat(ipFile).replace(/addr:/g, "").match(/inet (.[^ ]*) /)[1];
    }
  } finally {
    removeFile(ipFile);
  }
  return ipAddr;
}