Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Let's do it transparently

by rustic (Monk)
on Sep 06, 2011 at 19:50 UTC ( #924453=CUFP: print w/replies, xml ) Need Help??

Having to support many different UNIX platforms is a challenge and a pain in the same time. What commands should I run on each of them for a trivial checkup ? What are the options should I use ? etc. So I came up with a script for the abstraction purposes, where the only think I need is the hostname and the <thing> to check. Because originally this was spin around the FS usage I wrote for this functionality. Now I think it may be expanded to something more generic, but till then I wild like to share it, cause - I find it cool to use.
#!/usr/bin/perl -w # # df Act like UNIX df utility # usage: run and follow the text # use Net::Ping; use Net::OpenSSH; use IO::Socket; my ($ssh, $rsh) = qw(22 514); while (1) { print "nom du serveur: "; chomp(my $server = <STDIN>); die "le nom du serveur est obligatoire\n" unless $server; print "nom du FS sans le premier / : "; chomp(my $fs = <STDIN>); die "le nom du FS est obligatoire\n" unless $fs; sub os_port { foreach (@_) { $sock = new IO::Socket::INET(PeerAddr => $server, PeerPort => $_, Proto => 'tcp'); last if $sock; } $sock; } my $p = Net::Ping->new; #new("icmp") if ($p->ping($server)) { my $sock = os_port($ssh, $rsh); die "Aucune possibilite a se connecte au serveur\n" unless $so +ck; while (<$sock>) { $sock_string = $_; last; } close $sock; } $p->close(); if ($sock_string) { my $openssh = Net::OpenSSH->new($server); $openssh->error and die "Couldn't establish SSH connection: ". $openssh->error +; chomp(my $uname = $openssh->capture("uname -s")); $openssh->error and die "remote uname command failed: " . $openssh->err +or; if ($uname =~ /Linux/) { $openssh->system("df -h /$fs") or die "remote command failed: " . $openssh->error; } elsif ($uname =~ /AIX/) { $openssh->system("df -m /$fs") or die "remote command failed: " . $openssh->error; } elsif ($uname =~ /HP-UX/) { $openssh->system("bdf /$fs") or die "remote command failed: " . $openssh->error; } else { $openssh->system("df /$fs") or die "remote command failed: " . $openssh->error; } } else { system "rsh $server bdf /$fs"; } }
Here it is - AIX, HP-UX and Linux platforms via SSH or Rlogin connections acting as expected in each and every case.

Replies are listed 'Best First'.
Re: Let's do it transparently
by jwkrahn (Monsignor) on Sep 06, 2011 at 22:46 UTC
    sub os_port { foreach (@_) { $sock = new IO::Socket::INET(PeerAddr => $server, PeerPort => $_, Proto => 'tcp'); last if $sock; } $sock; }

    Subroutines defined at compile time have package scope so it makes little sense to define them inside a loop.    You will never get a new subroutine for each loop iteration.

    while (<$sock>) { $sock_string = $_; last; }

    Why not just:

    $sock_string = <$sock>;
      I do completely agree with you jwkrahn. Regarding the subroutine invocation I didn't realize this misfunctionality. Regarding the while (<$sock>) {} loop I was looking to grab the result I need from a few lines response I got in my test, so this left like that. Thank you for rectifications indeed.
Re: Let's do it transparently
by Tux (Abbot) on Sep 06, 2011 at 22:50 UTC

    I also run perl and system commands on Linux, HP-UX, AIX and a bit of Windows and got sick of the difference in these basic commands. Eventually I installed di on all systems. Works better than any default and gives predictable (and if wanted parsable) output. Problem solved.

    ps is even worse, but Proc::ProcessTable made me write px, so I now have a ps that works the same on all (but windows).

    Enjoy, Have FUN! H.Merijn
      This comes to my help. I'll try it out. Thanks!
Re: Let's do it transparently
by salva (Abbot) on Sep 07, 2011 at 09:18 UTC
    Later versions of OpenSSH server support the extension for querying file system information:
    my $df = $ssh->sftp->statvfs($fs);

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://924453]
Approved by Argel
Front-paged by Arunbear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2018-06-25 03:01 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.