Skip to content

Instantly share code, notes, and snippets.

@gdjohn4s
Last active November 16, 2021 09:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gdjohn4s/573dd0180bd8d3de1467cc2c9f0a84d4 to your computer and use it in GitHub Desktop.
Save gdjohn4s/573dd0180bd8d3de1467cc2c9f0a84d4 to your computer and use it in GitHub Desktop.
Simple remote command execution in perl using Net::SSH2 module.
#!/usr/bin/perl
=begin
Author: john4s
Scope: Check file system disk usage on remote system
Version: v1.0
DISCLAIMER:
To run this script you need to import your public rsa key on the server.
=cut
use warnings;
use strict;
use Net::SSH2;
# Configuration management
my $server = "YOUR IP";
my $port = 22;
my $user = "USERNAME";
# SSH Connection
my $ssh = Net::SSH2->new();
$ssh->connect($server, $port) or die $!;
$ssh->auth_agent($user);
die "Failed to autenticate: $!\n" unless $ssh->auth_ok;
print("Connected to $server as $user\n");
# Run remote commands
my $channel = $ssh->channel();
$channel->exec("df -h");
print("$_") while <$channel>;
$channel->close;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment