Kanrisuru

lsof

Get a list of open files on a system in ruby.
linux manual

Basic Usage

require 'kanrisuru'
host = Kanrisuru::Remote::Host.new(host: '127.0.1.1', username: 'ubuntu', keys: ['~/.ssh/id_rsa'])

host.su('root')
result = host.lsof
result.success?
true

result[6483].uid
0

result[6483].pid
171544

result[6483].command
"lsof"

This command needs escalated privileges to read file information for processes run by other users or root.

Result

Returns an array of OpenFile structs.

OpenFile Fields

Field Type Description
command string Contains the first nine characters of the name of the UNIX command associated with the process
pid integer The Process Identification number of the process.
uid integer The user ID number or login name of the user to whom the process belongs.
file_descriptor string The File Descriptor number of the file or various other options.
type string The type of the node associated with the file, eg; REG, DIR, CHR, IPv4 as an example.
device string Contains the device numbers, separated by commas, for a character special, block special, regular, directory or NFS file.
fsize integer The size of the file or the file offset in bytes.
inode integer Contains a unique identifier for the file node.
name string The name of the mount point and file system on which the file resides, or the name of a file specified in the names option (after any symbolic links have been resolved), or the name of a character special or block special device, or the local and remote Internet addresses of a network file, or the address or name of a UNIX domain socket, or the local and remote mount point names of an NFS file.

Return Example

result.data
[
  [   0] #<Struct:Kanrisuru::Core::System::OpenFile:0x000006e0
    fsize = 4096,
    command = "systemd",
    device = "0xfc01",
    file_descriptor = "cwd",
    inode = 2,
    name = "/",
    pid = 1,
    type = "DIR",
    uid = 0
  >,
  [   1] #<Struct:Kanrisuru::Core::System::OpenFile:0x000007d0
    fsize = 4096,
    command = "systemd",
    device = "0xfc01",
    file_descriptor = "rtd",
    inode = 2,
    name = "/",
    pid = 1,
    type = "DIR",
    uid = 0
  >,
  [   2] #<Struct:Kanrisuru::Core::System::OpenFile:0x00000898
    fsize = 1620224,
    command = "systemd",
    device = "0xfc01",
    file_descriptor = "txt",
    inode = 6732,
    name = "/usr/lib/systemd/systemd",
    pid = 1,
    type = "REG",
    uid = 0
  >,
  [   3] .. [6483],
  [6484] #<Struct:Kanrisuru::Core::System::OpenFile:0x00138328
    fsize = 191472,
    command = "lsof",
    device = "0xfc01",
    file_descriptor = "mem",
    inode = 31763,
    name = "/usr/lib/x86_64-linux-gnu/ld-2.31.so",
    pid = 171544,
    type = "REG",
    uid = 0
  >,
  [6485] #<Struct:Kanrisuru::Core::System::OpenFile:0x001383f0
    fsize = nil,
    command = "lsof",
    device = "0xd",
    file_descriptor = "4",
    inode = 918467,
    name = "pipe",
    pid = 171544,
    type = "FIFO",
    uid = 0
  >,
  [6486] #<Struct:Kanrisuru::Core::System::OpenFile:0x001384b8
    attr_reader :size = nil,
    command = "lsof",
    device = "0xd",
    file_descriptor = "7",
    inode = 918468,
    name = "pipe",
    pid = 171544,
    type = "FIFO",
    uid = 0
  >
]

Exit Status

Code Description
0 Success
1 Failure

Tested On

  • Ubuntu, Debian, Centos, Fedora, Redhat, OpenSuse, SLES