Hello again techman2006,

I was thinking about your question and I think I did not answer it correctly before. So I finally got some time today and I came up with this answer which is what I think you expect to see.

I am reading and printing the written file on the terminal, so you can remove this part. I just used it for demonstration purposes.

Working code, and output printed.

#!/usr/bin/perl use Expect; use strict; use warnings; use Data::Dumper; use Net::OpenSSH; use Config::IniFiles; use Fcntl qw(:flock); $| = 1; # To see the complete debuging process #$Net::OpenSSH::debug = ~0; my $path = 'conf.ini'; my $timeout = 20; my $debug = 0; sub devices { open my $fh , '<' , "".$path."" or die "Could not open file: ".$path." - $!\n"; flock($fh, LOCK_SH) or die "Could not lock '".$fh."' - $!\n"; tie my %ini, 'Config::IniFiles', ( -file => "".$path."" ) or die "Error: IniFiles->new: @Config::IniFiles::errors"; close ($fh) or die "Could not close '".$fh."' - $!\n"; my @keys = keys (%ini); my %data = (); my %final = (); foreach my $hash (@keys) { %data = clk_sync( $ini{$hash}{host}, $ini{$hash}{user}, $ini{$hash}{psw}, $ini{$hash}{port}, $hash ); @final{keys %data} = values %data; } return %final; } # end sub complex my %results = devices(); print Dumper(\%results); sub clk_sync { # alternative of shift my ($host,$user,$passwd,$port,$device) = (@_); my $host = shift; my $user = shift; my $passwd = shift; my $port = shift; my $device = shift; my %opts = ( passwd => $passwd, port => $port, user => $user ); my $ssh = Net::OpenSSH->new( $host, %opts ); $ssh->error and die "Couldn't establish SSH connection: ". $ssh->error; my @commands = ( "ls /tmp" , "vmstat" ); foreach my $command ( @commands ) { my ($rout, $pid) = $ssh->pipe_out( $command ) or die "pipe_out method failed: " . $ssh->error; open my $write, ">>", "sample.txt" or die $!; print $write "" . $device . " Command executed: " . $command . "\n +"; while (<$rout>) { print $write $_; } print $write "\n"; close $rout; close $write; } # Not needed just for demonstration purposes open my $read, "<", "sample.txt" or die $!; while (my $row = <$read>) { chomp $row; print "$row\n"; } close $read; my %hash_out = ( $device => [ @commands ] ); return %hash_out; } __END__ DEVICE 1 Command executed: ls /tmp foo.txt orbit-tiny qtsingleapp-Viber-0-3e8 qtsingleapp-Viber-0-3e8-lockfile ssh-QEhD246wugPL unity_support_test.0 DEVICE 1 Command executed: vmstat procs -----------memory---------- ---swap-- -----io---- -system-- ---- +--cpu----- r b swpd free buff cache si so bi bo in cs us s +y id wa st 5 0 152888 584588 22596 1091568 1 8 83 125 771 491 40 + 6 53 1 0 DEVICE 1 Command executed: ls /tmp foo.txt orbit-tiny qtsingleapp-Viber-0-3e8 qtsingleapp-Viber-0-3e8-lockfile ssh-QEhD246wugPL unity_support_test.0 DEVICE 1 Command executed: vmstat procs -----------memory---------- ---swap-- -----io---- -system-- ---- +--cpu----- r b swpd free buff cache si so bi bo in cs us s +y id wa st 5 0 152888 584588 22596 1091568 1 8 83 125 771 491 40 + 6 53 1 0 DEVICE 2 Command executed: ls /tmp foo.txt orbit-tiny qtsingleapp-Viber-0-3e8 qtsingleapp-Viber-0-3e8-lockfile ssh-QEhD246wugPL unity_support_test.0 DEVICE 2 Command executed: vmstat procs -----------memory---------- ---swap-- -----io---- -system-- ---- +--cpu----- r b swpd free buff cache si so bi bo in cs us s +y id wa st 1 0 152888 584596 22596 1091568 1 8 83 125 771 492 40 + 6 53 1 0 DEVICE 1 Command executed: ls /tmp foo.txt orbit-tiny qtsingleapp-Viber-0-3e8 qtsingleapp-Viber-0-3e8-lockfile ssh-QEhD246wugPL unity_support_test.0 DEVICE 1 Command executed: vmstat procs -----------memory---------- ---swap-- -----io---- -system-- ---- +--cpu----- r b swpd free buff cache si so bi bo in cs us s +y id wa st 5 0 152888 584588 22596 1091568 1 8 83 125 771 491 40 + 6 53 1 0 DEVICE 2 Command executed: ls /tmp foo.txt orbit-tiny qtsingleapp-Viber-0-3e8 qtsingleapp-Viber-0-3e8-lockfile ssh-QEhD246wugPL unity_support_test.0 DEVICE 2 Command executed: vmstat procs -----------memory---------- ---swap-- -----io---- -system-- ---- +--cpu----- r b swpd free buff cache si so bi bo in cs us s +y id wa st 1 0 152888 584596 22596 1091568 1 8 83 125 771 492 40 + 6 53 1 0 DEVICE 3 Command executed: ls /tmp foo.txt orbit-tiny qtsingleapp-Viber-0-3e8 qtsingleapp-Viber-0-3e8-lockfile ssh-QEhD246wugPL unity_support_test.0 DEVICE 3 Command executed: vmstat procs -----------memory---------- ---swap-- -----io---- -system-- ---- +--cpu----- r b swpd free buff cache si so bi bo in cs us s +y id wa st 1 0 152888 584720 22596 1091568 1 8 83 125 771 492 40 + 6 53 1 0 $VAR1 = { 'DEVICE 2' => [ 'ls /tmp', 'vmstat' ], 'DEVICE 1' => [ 'ls /tmp', 'vmstat' ], 'DEVICE 3' => [ 'ls /tmp', 'vmstat' ] };

The conf.ini file is attached in the previous answer.

Hope this is what you where looking for, let me know how it worked for you.

Seeking for Perl wisdom...on the process of learning...not there...yet!

In reply to Re: Simple SSH based chat client by thanos1983
in thread Simple SSH based chat client by techman2006

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.