| apw | 3812c03 | 2006-12-07 21:01:14 +0000 | [diff] [blame] | 1 | #!/usr/bin/perl | 
 | 2 | # | 
 | 3 | # conmux-attach -- attach commands to the console | 
 | 4 | # | 
 | 5 | # Attach stand alone commands to a console stream.  You can specify | 
 | 6 | # which of the commands file descriptors are connected to which of the | 
 | 7 | # 'steams' in the multiplexor; the console input/output and the user | 
 | 8 | # output. | 
 | 9 | # | 
 | 10 | # (C) Copyright IBM Corp. 2004, 2005, 2006 | 
 | 11 | # Author: Andy Whitcroft <andyw@uk.ibm.com> | 
 | 12 | # | 
 | 13 | # The Console Multiplexor is released under the GNU Public License V2 | 
 | 14 | # | 
 | 15 | use IO::Socket; | 
 | 16 | use Getopt::Long qw(:config no_auto_abbrev); | 
 | 17 |  | 
 | 18 | # Find our internal libraries. | 
 | 19 | ###my $lib = $0; $lib =~ s@/[^/]+$@@; | 
 | 20 | ###push(@INC, $lib); | 
 | 21 | ###require Conmux; | 
 | 22 | ##use lib "."; | 
 | 23 | ##use Conmux; | 
 | 24 | use lib "/usr/local/conmux/lib"; | 
 | 25 | use Conmux; | 
 | 26 |  | 
 | 27 | my $P = 'conmux-attach'; | 
 | 28 | my ($in, $out, $err); | 
 | 29 |  | 
 | 30 | # Usage. | 
 | 31 | GetOptions( | 
 | 32 | 	'i|stdout=s'	=> \$in, | 
 | 33 | 	'o|stdin=s'	=> \$out, | 
 | 34 | 	'e|stderr=s'	=> \$err, | 
 | 35 | 	'b|bot=s'	=> \$bot, | 
 | 36 | ) or exit; | 
 | 37 |  | 
 | 38 | if ($in eq '' && $out eq '' && $err eq '') { | 
 | 39 | 	$in = 'client'; | 
 | 40 | 	$out = 'client'; | 
 | 41 | } | 
 | 42 |  | 
 | 43 | if ($#ARGV < 1) { | 
| mbligh | ff48fca | 2008-01-16 18:28:27 +0000 | [diff] [blame] | 44 | 	print STDERR "$P: <host:port> <program> ...\n"; | 
| apw | 3812c03 | 2006-12-07 21:01:14 +0000 | [diff] [blame] | 45 | 	exit 1; | 
 | 46 | } | 
 | 47 | my ($mux, $app) = @ARGV; | 
 | 48 | shift; | 
 | 49 | $app =~ s@.*/@@; $app =~ s@\s.*$@@; | 
 | 50 | $app = $bot if ($bot); | 
 | 51 |  | 
 | 52 | # | 
 | 53 | # Connect to the client channel. | 
 | 54 | # | 
 | 55 | sub conmux_connect { | 
 | 56 | 	my ($mux, $type) = @_; | 
 | 57 |  | 
 | 58 | 	if (!$cache{$mux, $type}) { | 
 | 59 | 		my $con = Conmux::connect($mux); | 
 | 60 | 		my %r = Conmux::sendCmd($con, 'CONNECT', { | 
 | 61 | 				'id' => 'bot:' . $app,  | 
 | 62 | 				'to' => 'console', | 
 | 63 | 				'type' => $type } ); | 
 | 64 | 		die "$P: $mux: connect failed - $r{'status'}\n" | 
 | 65 | 			if ($r{'status'} ne "OK"); | 
 | 66 |  | 
 | 67 | 		$cache{$mux, $type} = $con; | 
 | 68 | 	} | 
 | 69 |  | 
 | 70 | 	$cache{$mux, $type}; | 
 | 71 | } | 
 | 72 |  | 
 | 73 | if ($in) { | 
 | 74 | 	my $to = conmux_connect($mux, $in); | 
 | 75 | 	open(STDIN, "<&", $to) || | 
 | 76 | 		die "$P: unable to hand off socket to stdin - $!\n"; | 
 | 77 | } | 
 | 78 | if ($out) { | 
 | 79 | 	my $to = conmux_connect($mux, $out); | 
 | 80 | 	open(STDOUT, ">&", $to) || | 
 | 81 | 		die "$P: unable to hand off socket to stdout - $!\n"; | 
 | 82 | } | 
 | 83 | if ($err) { | 
 | 84 | 	my $to = conmux_connect($mux, $err); | 
 | 85 | 	open(STDERR, ">&", $to) || | 
 | 86 | 		die "$P: unable to hand off socket to stderr - $!\n"; | 
 | 87 | } | 
 | 88 | exec @ARGV; |