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.
- [R] host, username, password, port, keys
Public Class Methods
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
Gets the remote user. This is set to the username in the initialize method by default.
Returns the hostname set on the remote host.
Access the os manager. Checkout the os docs for more details.
host.os.kernel 'linux' host.os.release 'ubuntu'
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"
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"
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
host.pwd.path '/home/ubuntu/' host.cd '../' host.pwd.path '/home/'
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
Access the memory manager. Checkout the memory docs for more details.
host.memory.total :gb 16.39
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'
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
The underlying ssh connection that the host uses to run commands with.
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.
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'
Runs a raw command without any shell like features.
Disconnects and closes any ssh connections that is open with the remote host.