| 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 <[email protected]> |
| 12 |
# |
| 13 |
# The Console Multiplexor is released under the GNU Public License V2 |
| 14 |
# |
| 15 |
$| = 1; |
| 16 |
|
| 17 |
our $P = 'console'; |
| 18 |
|
| 19 |
use FindBin; |
| 20 |
use POSIX qw(errno_h BUFSIZ); |
| 21 |
use IO::Socket; |
| 22 |
use Getopt::Long qw(:config no_auto_abbrev); |
| 23 |
|
| 24 |
my $CONMUX = $FindBin::Bin; |
| 25 |
my $CONMUX = $ENV{'CONMUX_ROOT'} if ($ENV{'CONMUX_ROOT'}); |
| 26 |
|
| 27 |
# Find our internal libraries. |
| 28 |
use lib $FindBin::Bin; |
| 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 |
|
| 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"; |
| 123 |
#display message of the day passed by the server e.g. who is already connected |
| 124 |
print "$r{'motd'}"; |
| 125 |
|
| 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 |
|
| 131 |
# We want to buffer output to the terminal. This prevents the program |
| 132 |
# from blocking if the user hits CTRL-S for example. |
| 133 |
# XXX ^^^ |
| 134 |
|
| 135 |
termRaw(); |
| 136 |
|
| 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 |
} |
| 146 |
|
| 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; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
termRestore(); |