Skip to content

Instantly share code, notes, and snippets.

@doujiang24
Created November 23, 2015 03:49
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save doujiang24/fd5c7a46831a3df02f57 to your computer and use it in GitHub Desktop.
Save doujiang24/fd5c7a46831a3df02f57 to your computer and use it in GitHub Desktop.
Lua get hostname
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
@chobits
Copy link

chobits commented Dec 1, 2015

very cool lua.ffi example to get hostname

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment