Skip to content
Snippets Groups Projects
awit-ssh 26.9 KiB
Newer Older
Nigel Kukard's avatar
Nigel Kukard committed
#!/usr/bin/perl
# awit-ssh - SSH initiator which searches LDAP for host details
Nigel Kukard's avatar
Nigel Kukard committed
# Copyright (c) 2016-2017, AllWorldIT
Nigel Kukard's avatar
Nigel Kukard committed
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

Nigel Kukard's avatar
Nigel Kukard committed


=encoding utf8

=head1 NAME

awit-ssh - LDAP lookup utility for SSH hosts.

=head1 SYNOPSIS

	awit-ssh < HOST[:PORT]] [--libvirt-vnc HOST:PORT] [--knock HOST:PORT] | --rsync -- remote://[HOST[:PORT]]/full/path DEST >

=cut

=head1 DESCRIPTION

awit-ssh perl script that automates connecting to a server via ssh by looking up the user and port information from a LDAP
database.

=cut



Nigel Kukard's avatar
Nigel Kukard committed
use strict;
use warnings;

Nigel Kukard's avatar
Nigel Kukard committed

use Term::ANSIColor;
Nigel Kukard's avatar
Nigel Kukard committed
use Getopt::Long;
Robert Spencer's avatar
Robert Spencer committed
use Net::DBus qw(:typing);
# Check Config::IniFiles
if (!eval {require Config::IniFiles; 1;}) {
	print STDERR "You're missing Config::IniFiles, try 'apt-get install libconfig-inifiles-perl'\n";
	exit 1;
}
Nigel Kukard's avatar
Nigel Kukard committed
# Check IO::Socket::INET6
if (!eval {require IO::Socket::INET6; 1;}) {
	print STDERR "You're missing IO::Socket::INET6, try 'apt-get install libio-socket-inet6-perl'\n";
	exit 1;
}
# Check Net::LDAP
if (!eval {require Net::LDAP; 1;}) {
	print STDERR "You're missing Net::LDAP, try 'apt-get install libnet-ldap-perl'\n";
	exit 1;
}
# Check IO::Prompt
if (!eval {require IO::Prompt; 1;}) {
	print STDERR "You're missing IO::Prompt, try 'apt-get install libio-prompt-perl'\n";
Nigel Kukard's avatar
Nigel Kukard committed
	exit 1;
}
## no critic (BuiltinFunctions::ProhibitStringyEval)
eval qq(
	use IO::Prompt qw(prompt);
);
## use critic
use Digest::SHA qw( sha1_hex );
use User::pwent;
Nigel Kukard's avatar
Nigel Kukard committed


Robert Spencer's avatar
Robert Spencer committed
my $NAME = "AWIT-SSH-Client";
Nigel Kukard's avatar
Nigel Kukard committed
our $VERSION = "0.8.6";
Nigel Kukard's avatar
Nigel Kukard committed

Nigel Kukard's avatar
Nigel Kukard committed
print(STDERR "$NAME v$VERSION - Copyright (c) 2016-2017, AllWorldIT\n\n");
Nigel Kukard's avatar
Nigel Kukard committed


Nigel Kukard's avatar
Nigel Kukard committed
=head1 OPTIONS

C<awit-ssh> provides the below commandline options...

=cut
Nigel Kukard's avatar
Nigel Kukard committed

Nigel Kukard's avatar
Nigel Kukard committed
=head2 --help|?

	Display this help information.

=cut

=head2 --version

	Display version information.

=cut
Nigel Kukard's avatar
Nigel Kukard committed
=head2 --knock <HOST:PORT>

	Knock on HOST:PORT to gain access.

=cut
Nigel Kukard's avatar
Nigel Kukard committed
=head2 --rsync remote://<HOST[:PORT]>/full/path DEST

	Use rsync to rsync data from remote server to DEST. This can be specified either way around.

=cut
Nigel Kukard's avatar
Nigel Kukard committed
=head2 --libvirt-vnc <HOST:PORT>

	Connect to remote VNC server HOST:PORT.

=cut
Nigel Kukard's avatar
Nigel Kukard committed

# Grab options
my %optctl = ();
GetOptions(\%optctl,
	"help|?",
	"version",

	# TODO: debug is not implemented, make sure displayHelp is updated
	"debug",

	# TODO: Improve globbing before adding it to displayHelp
	"globbing",

	"knock=s",

	"rsync",

	"libvirt-vnc=s",
Nigel Kukard's avatar
Nigel Kukard committed
) or exit 1;

# Check for help
if (defined($optctl{'help'})) {
	displayHelp();
	exit 0;
}

# Check for version
if (defined($optctl{'version'})) {
	displayVersion();
	exit 0;
}

Nigel Kukard's avatar
Nigel Kukard committed
# Check if we using rsync instead of SSH
my $useRsync;
Nigel Kukard's avatar
Nigel Kukard committed
my @rsyncParams;
if (defined(my $rsyncHost = $optctl{'rsync'})) {
	$useRsync = $rsyncHost;
}

# Check if we using libvirt vnc instead of SSH
my $libvirtVNC;
if (defined(my $vmName = $optctl{'libvirt-vnc'})) {
	if (! -x '/usr/bin/ssvncviewer') {
		logger('ERROR',color('magenta')."To use --libvirt-vnc you need to install ssvncviewer. Hint: apt-get install ssvnc".
				color('reset'));
		exit 1;
	}
	$libvirtVNC = $vmName;
}

# Check if we should be doing port knocking
my ($knockHost,$knockPort);
if (defined(my $knock = $optctl{'knock'})) {
	# If so, split off the host and the port
	($knockHost,$knockPort) = split(':',$knock);
	if (!defined($knockPort)) {
		logger('ERROR',color('magenta')."Port knock specifications should be in the format of HOST:PORT".color('reset'));
		exit 1;
	}
}


# Check for option combinations
if (defined($useRsync) && defined($libvirtVNC)) {
	logger('ERROR',color('magenta')."Options --rsync and --libvirt-monitor cannot be used together".color('reset'));
	exit 1;
}


# Variables we may set below
my $loginUsername;

Nigel Kukard's avatar
Nigel Kukard committed
my $hostSpec;
if (defined($useRsync)) {
Nigel Kukard's avatar
Nigel Kukard committed
	foreach my $param (@ARGV) {
		# Look for the remote:// param
		if ($param =~ /remote:\/\//) {
			# Remove it and set the hostSpec
			my $removedTag = substr($param,9);
			# Assing hostSpec to the first part of the tag
Loading
Loading full blame...