# Copyright (c) 2002 bivio Software, Inc. All Rights Reserved. # # Visit http://www.bivio.biz for more info. # # This library is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2.1 of the # License, or (at your option) any later version. # # This library 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 # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; If not, you may get a copy from: # http://www.opensource.org/licenses/lgpl-license.html # # $Id: Host.pm,v 2.1 2006/10/16 21:08:05 moeller Exp $ package Bivio::Util::Host; use strict; $Bivio::Util::Host::VERSION = sprintf('%d.%02d', q$Revision: 2.1 $ =~ /\d+/g); $_ = $Bivio::Util::Host::VERSION; =head1 NAME Bivio::Util::Host - control commands on this host =head1 RELEASE SCOPE bOP =head1 SYNOPSIS use Bivio::Util::Host; =cut =head1 EXTENDS L =cut use Bivio::ShellUtil; @Bivio::Util::Host::ISA = ('Bivio::ShellUtil'); =head1 DESCRIPTION C =cut =head1 CONSTANTS =cut =for html =head2 USAGE : string Returns usage. =cut sub USAGE { return <<'EOF'; usage: b-host [options] command [args...] commands: exec_if host command... args -- execs command and arguments only on host EOF } #=IMPORTS use Socket (); #=VARIABLES =head1 METHODS =cut =for html =head2 exec_if(string host, string command, string arg, ...) : string Does not return if successful. If B is set, outputs what it would do. Enclose command as a single string if you want shell interpretation (normal Perl exec rules). Tries to bind to the address to a UDP socket. You must have privs to open "any" socket. =cut sub exec_if { my($self, $host, @cmd) = @_; socket(SOCKET, Socket::PF_INET(), Socket::SOCK_DGRAM(), getprotobyname('udp')) or Bivio::Die->die("Cannot create socket: $!"); if (bind(SOCKET, Socket::pack_sockaddr_in(0, (gethostbyname($host))[4] || Bivio::Die->die("$host: gethostbyname error: $!")))) { return "Would have executed: @cmd\n" if $self->unsafe_get('noexecute'); exec(@cmd) || Bivio::Die->die("Exec failed: @cmd: $!"); # DOES NOT RETURN } return $self->unsafe_get('noexecute') ? "Not this host\n" : undef; } #=PRIVATE METHODS =head1 COPYRIGHT Copyright (c) 2002 bivio Software, Inc. All Rights Reserved. Visit http://www.bivio.biz for more info. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; If not, you may get a copy from: http://www.opensource.org/licenses/lgpl-license.html =head1 VERSION $Id: Host.pm,v 2.1 2006/10/16 21:08:05 moeller Exp $ =cut 1;