child_process.exec를 통해 nodejs에서 터미널 명령을 실행하면 실제 터미널에서 명령을 실행할 때보 다 다른 출력이 발생합니다.nodejs exec가 실제 터미널에서 함수 호출보다 다른 출력을 제공합니다.
내 코드는 다음과 같습니다
$ ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'
$ 192.168.178.222
을하지만 nodejs에 나는 표준 출력으로이 문자열을 얻을 : 내가 진짜 터미널에서이 명령을 실행하면
는function checkLocalIP() {
logger.debug("Checking the local IP");
execute("ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'", function(localIP) {
if(isIp.v4(localIP)) {
logger.debug("Local IP found",{localIP:localIP});
return true;
} else {
logger.error("Local IP not found",{localIP:localIP});
return false;
}
});
}
function execute(command, callback){
logger.debug("Executing command: "+command,{command:command});
exec(command, function(error, stdout, stderr){
callback(stdout);
});
}
나는 다음과 같은 경우에만 IP를 얻을 수 :
"ine\nt 127.0\n.0.1\nine\nt 192.168\n.178.222"
왜 출력이 유사하지 않은지, 왜 nodejs 호출이 내게 127.0을 출력하는지 궁금합니다. 0.1 IP는 명령에서 제외 되었기 때문입니다.