#!/usr/bin/perl

use strict;
use warnings;

# Dell's DRAC SM-CLP

use Data::Dump qw/dump/;

use Net::OpenSSH;
use Expect;
use Getopt::Long;

my $debug = 0;
my $timeout = 5;

GetOptions(
	'debug!' => \$debug,
	'timeout=i' => \$timeout,
) or die $!;

my ( $uri, $command, $path ) = @ARGV;
die "usage: $0 login:passwd\@host show /system1\n" unless $uri && $command;

my $ssh = Net::OpenSSH->new( $uri );
my ( $pty, $pid ) = $ssh->open2pty;
my $expect = Expect->init($pty);
$expect->raw_pty(1);
$expect->log_user(1) if $debug;

$expect->expect( $timeout, '$' );
$expect->send( "smclp\n" );

$expect->expect( $timeout, '>' );

my $data;

sub smclp {
	my ( $command, $path ) = @_;

	warn "smclp $command $path\n" if $debug;

	$expect->send( "$command $path\n" );

	$expect->expect( $timeout, '>' );

	my $got = $expect->before;

	my $section;

	foreach ( split(/\r\n/, $got ) ) {
		if ( m{(/.*):} ) {
			$path = $1;
		} elsif ( m{^\s{2}(\w+):} ) {
			$section = $1;
		} elsif ( m{^\s{4}(.+)} ) {
			if ( $section eq 'Properties' ) {
				my ( $k, $v ) = split(/\s+=\s+/,$1,2);
				$data->{$path}->{$section}->{$k} = $v;
			} else {
				push @{ $data->{$path}->{$section} }, $1;
			}
		} else {
			print "# IGNORE [$_]\n" if $debug;
		}
	}

	warn "# data = ",dump $data;
}

smclp $command => $path;

