Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Syntactic Confectionery Delight
 
PerlMonks  

Let's do it transparently

by rustic (Scribe)
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 df.pl 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.

Comment on Let's do it transparently
Download Code
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 (Monsignor) 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 (Monsignor) on Sep 07, 2011 at 09:18 UTC
    Later versions of OpenSSH server support the statvfs@openssh.com extension for querying file system information:
    my $df = $ssh->sftp->statvfs($fs);

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://924453]
Approved by Argel
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2014-04-17 02:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (437 votes), past polls