apw | 3812c03 | 2006-12-07 21:01:14 +0000 | [diff] [blame] | 1 | #!/usr/bin/perl |
| 2 | # |
| 3 | # console <host>/<machine> -- interactive client interface |
| 4 | # |
| 5 | # The main interactive client interace to conmux. Allows direct |
| 6 | # interaction with the payload, as well as allowing break out |
| 7 | # to the conmux menu to envoke defined commands; for example |
| 8 | # hardreset. |
| 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 | $| = 1; |
| 16 | |
| 17 | our $P = 'console'; |
| 18 | |
apw | ac65504 | 2007-02-24 11:09:16 +0000 | [diff] [blame^] | 19 | use POSIX qw(errno_h BUFSIZ); |
apw | 3812c03 | 2006-12-07 21:01:14 +0000 | [diff] [blame] | 20 | use IO::Socket; |
apw | 3812c03 | 2006-12-07 21:01:14 +0000 | [diff] [blame] | 21 | use Getopt::Long qw(:config no_auto_abbrev); |
| 22 | |
| 23 | my $CONMUX = '/usr/local/conmux'; |
| 24 | my $CONMUX = $ENV{'CONMUX_ROOT'} if ($ENV{'CONMUX_ROOT'}); |
| 25 | |
| 26 | # Find our internal libraries. |
| 27 | ###my $lib = $0; $lib =~ s@/[^/]+$@@; |
| 28 | ###push(@INC, $lib); |
| 29 | ###require Conmux; |
| 30 | ##use lib "."; |
| 31 | ##use Conmux; |
| 32 | use lib "/usr/local/conmux/lib"; |
| 33 | use Conmux; |
| 34 | |
| 35 | # Basic terminal handling. |
| 36 | sub termRaw { |
| 37 | $termSettings = `stty -g`; |
| 38 | system "stty raw -echo opost onlret"; |
| 39 | } |
| 40 | sub termRestore { |
| 41 | system "stty $termSettings"; |
| 42 | } |
| 43 | |
| 44 | my $bot; |
| 45 | my $list; |
| 46 | my $status; |
| 47 | GetOptions( |
| 48 | 'b|bot=s' => \$bot, |
| 49 | 'l|list' => \$list, |
| 50 | 's|status' => \$status, |
| 51 | ); |
| 52 | sub usage { |
| 53 | warn "Usage: $P <service>\n"; |
| 54 | warn " $P <registry>/<service>\n"; |
| 55 | warn " $P <host>:<port>\n"; |
| 56 | warn " $P --status <service>\n"; |
| 57 | die " $P --list [<registry>]\n"; |
| 58 | } |
| 59 | |
| 60 | my $id; |
| 61 | if ($bot) { |
| 62 | $id = 'bot:' . $bot; |
| 63 | } else { |
| 64 | $id = 'user:' . $ENV{'LOGNAME'}; |
| 65 | } |
| 66 | |
| 67 | # |
| 68 | # MODE: registry list. |
| 69 | # |
| 70 | if ($list) { |
| 71 | if ($#ARGV == -1) { |
| 72 | print Conmux::Registry::list('-'); |
| 73 | |
| 74 | } elsif ($#ARGV == 0) { |
| 75 | print Conmux::Registry::list($ARGV[0]); |
| 76 | |
| 77 | } else { |
| 78 | usage(); |
| 79 | } |
| 80 | exit 0 |
| 81 | } |
| 82 | |
| 83 | # |
| 84 | # COMMAND: payload status command |
| 85 | # |
| 86 | if ($status) { |
| 87 | usage() if ($#ARGV != 0); |
| 88 | |
| 89 | my $sock; |
| 90 | eval { |
| 91 | $sock = Conmux::connect($ARGV[0]); |
| 92 | }; |
| 93 | if ($@) { |
| 94 | print "unavailable\n"; |
| 95 | exit 0 |
| 96 | } |
| 97 | my %r = Conmux::sendCmd($sock, 'CONNECT', { 'id' => $id, |
| 98 | 'to' => 'console', 'hide' => 1 }); |
| 99 | if ($r{'status'} ne 'OK') { |
| 100 | print "unavailable\n"; |
| 101 | |
| 102 | } elsif ($r{'state'}) { |
| 103 | print "$r{'state'}\n"; |
| 104 | |
| 105 | } else { |
| 106 | print "unknown\n"; |
| 107 | } |
| 108 | exit 0; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | # |
| 113 | # COMMAND: general payload connect. |
| 114 | # |
| 115 | if ($#ARGV != 0) { |
| 116 | usage(); |
| 117 | } |
| 118 | |
apw | 3812c03 | 2006-12-07 21:01:14 +0000 | [diff] [blame] | 119 | # Connect to the host/port specified on the command line, |
| 120 | # or localhost:23 |
| 121 | my $sock = Conmux::connect($ARGV[0]); |
| 122 | |
| 123 | my %r = Conmux::sendCmd($sock, 'CONNECT', { 'id' => $id, 'to' => 'console' }); |
| 124 | die "$P: $ARGV[0]: connect failed - $r{'status'}\n" if ($r{'status'} ne 'OK'); |
| 125 | |
| 126 | print "Connected to $r{'title'} (~\$quit to exit)\n"; |
| 127 | |
apw | ac65504 | 2007-02-24 11:09:16 +0000 | [diff] [blame^] | 128 | my $rin = $win = $ein = ''; |
| 129 | vec($rin, fileno(STDIN), 1) = 1; |
| 130 | vec($rin, fileno($sock), 1) = 1; |
| 131 | my $ein = $rin | $win; |
| 132 | |
apw | 3812c03 | 2006-12-07 21:01:14 +0000 | [diff] [blame] | 133 | # We want to buffer output to the terminal. This prevents the program |
| 134 | # from blocking if the user hits CTRL-S for example. |
apw | ac65504 | 2007-02-24 11:09:16 +0000 | [diff] [blame^] | 135 | # XXX ^^^ |
apw | 3812c03 | 2006-12-07 21:01:14 +0000 | [diff] [blame] | 136 | |
| 137 | termRaw(); |
| 138 | |
apw | ac65504 | 2007-02-24 11:09:16 +0000 | [diff] [blame^] | 139 | # Loop waiting for input from the user, or from the server. |
| 140 | while (1) { |
| 141 | $nfound = select($rout=$rin, $wout=$win, $eout=$ein, undef); |
| 142 | if ($nfound < 0) { |
| 143 | if ($! == EINTR || $! == EAGAIN) { |
| 144 | next; |
| 145 | } |
| 146 | die "$P: select failed - $!\n"; |
| 147 | } |
apw | 3812c03 | 2006-12-07 21:01:14 +0000 | [diff] [blame] | 148 | |
apw | ac65504 | 2007-02-24 11:09:16 +0000 | [diff] [blame^] | 149 | if (vec($rout, fileno(STDIN), 1)) { |
| 150 | $len = sysread(STDIN, $data, 4096); |
| 151 | if ($len <= 0) { |
| 152 | if ($len < 0) { |
| 153 | if ($! == EINTR || $! == EAGAIN) { |
| 154 | next; |
| 155 | } |
| 156 | die "$P: STDIN: read failed - $!\n"; |
| 157 | } |
| 158 | print STDERR "Connection Closed (client)\n"; |
| 159 | last; |
| 160 | } |
| 161 | print $sock $data; |
| 162 | } |
| 163 | if (vec($rout, fileno($sock), 1)) { |
| 164 | $len = sysread($sock, $data, 4096); |
| 165 | if ($len <= 0) { |
| 166 | if ($len < 0) { |
| 167 | if ($! == EINTR || $! == EAGAIN) { |
| 168 | next; |
| 169 | } |
| 170 | die "$P: server: read failed - $!\n"; |
| 171 | } |
| 172 | print STDERR "Connection Closed (server)\n"; |
| 173 | last; |
| 174 | } |
| 175 | print $data; |
apw | 3812c03 | 2006-12-07 21:01:14 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
apw | ac65504 | 2007-02-24 11:09:16 +0000 | [diff] [blame^] | 179 | termRestore(); |