Hey there, fellow developer! Ready to dive into the world of SSH integrations with Ruby? Let's get cracking!
SSH is like the Swiss Army knife of remote server management. It's secure, versatile, and downright essential. Today, we're going to harness its power using Ruby and the awesome net-ssh gem. Buckle up!
Before we start, make sure you've got:
gem install net-ssh
)Got 'em? Great! Let's roll.
First things first, let's create a new Ruby file. Call it whatever you want - I'm going with ssh_integration.rb
. Fancy, right?
Now, let's import our star player:
require 'net/ssh'
Alright, time for the main event. Here's how you connect:
Net::SSH.start('your_server.com', 'username', password: 'your_password') do |ssh| puts "Connected to #{ssh.host}!" end
Easy peasy! But hey, what if something goes wrong? Let's add some error handling:
begin Net::SSH.start('your_server.com', 'username', password: 'your_password') do |ssh| puts "Connected to #{ssh.host}!" end rescue Net::SSH::AuthenticationFailed puts "Oops! Authentication failed. Check your credentials!" end
Now that we're in, let's run some commands:
ssh.exec!('ls -l') do |ch, stream, data| puts data end
Want to run multiple commands? No sweat:
['ls -l', 'pwd', 'whoami'].each do |cmd| ssh.exec!(cmd) do |ch, stream, data| puts "#{cmd}: #{data}" end end
Sometimes you need to capture that output for processing. Here's how:
output = ssh.exec!('ls -l') puts output.lines.count # Count the number of lines
Need to move some files around? We've got you covered:
ssh.scp.upload!('local_file.txt', '/remote/path/file.txt') ssh.scp.download!('/remote/path/file.txt', 'local_file.txt')
Always set a timeout to avoid hanging connections:
Net::SSH.start('your_server.com', 'username', password: 'your_password', timeout: 10) do |ssh| # Your code here end
And remember to close your connections when you're done!
Let's wrap this all up in a neat little class:
class SSHClient def initialize(host, user, password) @host = host @user = user @password = password end def execute(command) Net::SSH.start(@host, @user, password: @password) do |ssh| ssh.exec!(command) end end # Add more methods as needed end client = SSHClient.new('your_server.com', 'username', 'password') puts client.execute('ls -l')
Testing is crucial! Here's a simple test using RSpec:
require 'rspec' require_relative 'ssh_integration' RSpec.describe SSHClient do it 'executes a command successfully' do client = SSHClient.new('test.com', 'user', 'pass') allow(Net::SSH).to receive(:start).and_yield(double('ssh')) expect(client.execute('ls')).to eq('file1\nfile2\n') end end
Never hardcode credentials! Use environment variables or a secure credential store. And if possible, switch to key-based authentication for even better security.
And there you have it! You're now armed with the knowledge to build your very own SSH integration in Ruby. Remember, practice makes perfect, so don't be afraid to experiment and expand on what you've learned here.
Keep coding, keep learning, and most importantly, have fun! 🚀