Created
November 23, 2015 03:49
-
-
Save doujiang24/fd5c7a46831a3df02f57 to your computer and use it in GitHub Desktop.
Lua get hostname
This file contains hidden or 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
local ffi = require "ffi" | |
local C = ffi.C | |
ffi.cdef[[ | |
int gethostname(char *name, size_t len); | |
]] | |
local size = 50 | |
local buf = ffi.new("unsigned char[?]", size) | |
local res = C.gethostname(buf, size) | |
if res == 0 then | |
local hostname = ffi.string(buf, size) | |
local host = string.gsub(hostname, "%z+$", "") | |
ngx.say(#hostname, ": ", hostname) | |
ngx.say(#host, ": ", host) | |
end | |
local f = io.popen("/bin/hostname", "r") | |
if f then | |
local host = f:read("*l") | |
f:close() | |
ngx.say(#host, ": ", host) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
very cool lua.ffi example to get hostname