Kanrisuru

last

Get a list of the last logged in users on a remote server in ruby.
linux manual

Basic Usage

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

result = host.last
result.success?
true

result[0].user
'ubuntu'

result[0].sessions[1].tty
'pts/1'

result[0].sessions[1].ip_address
#<IPAddr: IPv4:172.16.50.10/255.255.255.255>

Parameters

Field Type Description
failed_attempts boolean
If true, show only failed user login attempts.
file string
Read login information from a file path instead of wtmp file.

Result

Returns an array of OpenFile structs.

LoginUser Fields

Field Type Description
user string The username of the person logged in.
sessions array An array of SessionDetail pertaining to the logged in user.

SessionDetail Fields

Field Type Description
tty string The terminal the user was logged into. A terminal entry of :0 means they were logged in on the Linux computer itself.
login_at DateTime The timestamp of when the session was started.
logout_at DateTime The timestamp of when the session was ended.
ip_address IPAddr The ip address of server
success boolean True if the login session was successful, false if it was a failed login attempt.

Return Example

result.to_a
[
  [0] #<Struct:Kanrisuru::Core::System::LoginUser:0x000006b8
    sessions = [
      [0] #<Struct:Kanrisuru::Core::System::SessionDetail:0x00000708
        ip_address = #<IPAddr: IPv4:172.16.50.1/255.255.255.255>,
        login_at = #<DateTime: 2022-01-21T23:02:23+00:00 ((2459601j,82943s,0n),+0s,2299161j)>,
        logout_at = nil,
        success = true,
        tty = "pts/0"
      >,
      [1] #<Struct:Kanrisuru::Core::System::SessionDetail:0x000216d8
        ip_address = #<IPAddr: IPv4:10.1.1.221/255.255.255.255>,
        login_at = #<DateTime: 2020-12-05T10:45:12+00:00 ((2459189j,38712s,0n),+0s,2299161j)>,
        logout_at = #<DateTime: 2020-12-05T10:46:54+00:00 ((2459189j,38814s,0n),+0s,2299161j)>,
        success = true,
        tty = "pts/0"
      >
    ],
    user = "ubuntu"
  >,
  [1] #<Struct:Kanrisuru::Core::System::LoginUser:0x000217c8
    sessions = [
      [ 0] #<Struct:Kanrisuru::Core::System::SessionDetail:0x00021818
        ip_address = #<IPAddr: IPv4:0.0.0.0/255.255.255.255>,
        login_at = #<DateTime: 2021-12-06T13:30:19+00:00 ((2459555j,48619s,0n),+0s,2299161j)>,
        logout_at = nil,
        success = true,
        tty = "system boot"
      >
    ],
    user = "reboot"
  >,
  [2] #<Struct:Kanrisuru::Core::System::LoginUser:0x00022718
    sessions = [
      [0] #<Struct:Kanrisuru::Core::System::SessionDetail:0x00022768
        ip_address = #<IPAddr: IPv4:172.16.50.1/255.255.255.255>,
        login_at = #<DateTime: 2020-12-13T01:20:23+00:00 ((2459197j,4823s,0n),+0s,2299161j)>,
        logout_at = #<DateTime: 2020-12-13T01:20:28+00:00 ((2459197j,4828s,0n),+0s,2299161j)>,
        success = true,
        tty = "pts/1"
      >
    ],
    user = "root"
  >
]

Exit Status

Code Description
0 Success
1 Failure

Tested On

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