blob: 9553948494617d3f5301d15faf75ee4a02dfbc51 [file] [log] [blame]
apw3812c032006-12-07 21:01:14 +00001#!/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
17our $P = 'console';
18
apwac655042007-02-24 11:09:16 +000019use POSIX qw(errno_h BUFSIZ);
apw3812c032006-12-07 21:01:14 +000020use IO::Socket;
apw3812c032006-12-07 21:01:14 +000021use Getopt::Long qw(:config no_auto_abbrev);
22
23my $CONMUX = '/usr/local/conmux';
24my $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;
32use lib "/usr/local/conmux/lib";
33use Conmux;
34
35# Basic terminal handling.
36sub termRaw {
37 $termSettings = `stty -g`;
38 system "stty raw -echo opost onlret";
39}
40sub termRestore {
41 system "stty $termSettings";
42}
43
44my $bot;
45my $list;
46my $status;
47GetOptions(
48 'b|bot=s' => \$bot,
49 'l|list' => \$list,
50 's|status' => \$status,
51);
52sub 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
60my $id;
61if ($bot) {
62 $id = 'bot:' . $bot;
63} else {
64 $id = 'user:' . $ENV{'LOGNAME'};
65}
66
67#
68# MODE: registry list.
69#
70if ($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#
86if ($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#
115if ($#ARGV != 0) {
116 usage();
117}
118
apw3812c032006-12-07 21:01:14 +0000119# Connect to the host/port specified on the command line,
120# or localhost:23
121my $sock = Conmux::connect($ARGV[0]);
122
123my %r = Conmux::sendCmd($sock, 'CONNECT', { 'id' => $id, 'to' => 'console' });
124die "$P: $ARGV[0]: connect failed - $r{'status'}\n" if ($r{'status'} ne 'OK');
125
126print "Connected to $r{'title'} (~\$quit to exit)\n";
mbligh9b94f3e2008-02-19 15:51:28 +0000127#display message of the day passed by the server e.g. who is already connected
128print "$r{'motd'}";
apw3812c032006-12-07 21:01:14 +0000129
apwac655042007-02-24 11:09:16 +0000130my $rin = $win = $ein = '';
131vec($rin, fileno(STDIN), 1) = 1;
132vec($rin, fileno($sock), 1) = 1;
133my $ein = $rin | $win;
134
apw3812c032006-12-07 21:01:14 +0000135# We want to buffer output to the terminal. This prevents the program
136# from blocking if the user hits CTRL-S for example.
apwac655042007-02-24 11:09:16 +0000137# XXX ^^^
apw3812c032006-12-07 21:01:14 +0000138
139termRaw();
140
apwac655042007-02-24 11:09:16 +0000141# Loop waiting for input from the user, or from the server.
142while (1) {
143 $nfound = select($rout=$rin, $wout=$win, $eout=$ein, undef);
144 if ($nfound < 0) {
145 if ($! == EINTR || $! == EAGAIN) {
146 next;
147 }
148 die "$P: select failed - $!\n";
149 }
apw3812c032006-12-07 21:01:14 +0000150
apwac655042007-02-24 11:09:16 +0000151 if (vec($rout, fileno(STDIN), 1)) {
152 $len = sysread(STDIN, $data, 4096);
153 if ($len <= 0) {
154 if ($len < 0) {
155 if ($! == EINTR || $! == EAGAIN) {
156 next;
157 }
158 die "$P: STDIN: read failed - $!\n";
159 }
160 print STDERR "Connection Closed (client)\n";
161 last;
162 }
163 print $sock $data;
164 }
165 if (vec($rout, fileno($sock), 1)) {
166 $len = sysread($sock, $data, 4096);
167 if ($len <= 0) {
168 if ($len < 0) {
169 if ($! == EINTR || $! == EAGAIN) {
170 next;
171 }
172 die "$P: server: read failed - $!\n";
173 }
174 print STDERR "Connection Closed (server)\n";
175 last;
176 }
177 print $data;
apw3812c032006-12-07 21:01:14 +0000178 }
179}
180
apwac655042007-02-24 11:09:16 +0000181termRestore();