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