This is the development version of Kanrisuru. Since this is still a pre-1.0 release, we don't recommend using this in system-critical production environments.

Kanrisuru

Host

Kanrisuru::Remote::Host encapsulates the connection to the remote host. This is the main class that performs various commands on a remote instance using a ssh connection.

Attributes

  • [R] host, username, password, port, keys

Public Class Methods

new

Create a new host instance. Requires a host name / ip address, username and either a password or ssh key path depending on the host configuration. Can also pass in a shell to use.

host = Kanrisuru::Remote::Host.new(host: '127.0.0.1', username: 'ubuntu', keys: ['~/.ssh/id_rsa'])
host.ping?
true

host.hostname
'ubuntu'

Public Instance Methods

remote_user

Gets the remote user. This is set to the username in the initialize method by default.

host.remote_user
'ubuntu'

hostname

Returns the hostname set on the remote host.

host.hostname
'localhost'

os

Access the os manager. Checkout the os docs for more details.

host.os.kernel
'linux'

host.os.release
'ubuntu'

env

Access the environment variables. Used in conjunction with running a command that you want to set the environment variables for. Checkout the env docs for more details.

host.env['VAR1'] = 'hello'
host.env['VAR2'] = 'world'

host.echo("$VAR1 $VAR2").to_s
"hello world"

fstab

Loads the fstab from the remote host, which is scoped to the host instance. Optionally pass in the path of the fstab file on the remote host. Checkout the fstab docs for more details.

fstab = host.fstab
fstab.to_s
"LABEL=cloudimg-rootfs / ext4 defaults 0 0"
"LABEL=UEFI /boot/efi vfat defaults 0 0"

chdir

Alias to cd

cd

Changes the current working directory. This is a pseudo command as there isn’t actually a change of directory, instead the directory is tracked on the host instance, and then the current working directory is used in conjunction with the execute_shell command.

host.pwd.path
'/home/ubuntu/'

host.cd '../'
host.pwd.path
'/home/'

cpu

Access the cpu manager. Checkout the cpu docs for more details.

host.cpu.load_average
[0.19, 0.05, 0.01]

host.cpu.architecture
'x86_64'

host.cpu.cores
8

memory

Access the memory manager. Checkout the memory docs for more details.

host.memory.total :gb
16.39

su

Changes the remote user. This can be used to escalate prividges, eg running sudo commands. Care should be taken when operating as root.

host.su('root')
host.blkid(device: '/dev/vda1').name
'/dev/vda1'

file

Creates an instance of a remote file that is scoped to the host instance. Checkout the file docs for more details.

file = host.file('/home/ubuntu/file')
file.exists?
false

file.touch
file.exists?
true

ssh

The underlying ssh connection that the host uses to run commands with.

ping?

Returns true if the remote host is pingable. Not all hosts that are connectable via ssh can be pinged. If this is the case, check the firewall settings for the icmp protocol.

host.ping?
true

execute_shell

Run a command on the remote host with env variables, remote user and the current directory.

host.su('karen')
host.execute_shell('whoami').to_s
'karen'

execute

Runs a raw command without any shell like features.

host.execute_shell('nproc').to_i
8

disconnect

Disconnects and closes any ssh connections that is open with the remote host.