#!/usr/bin/perl # Usage: # # Start a screen, ssh forward to tov port 3009, run this script. # Here's how to do that: # # wget http://retout.uwcs.co.uk/pogobot/uptime.perl # mv uptime.perl uptime.pl # screen # ssh -L 3009:localhost:3009 user@uwcs.co.uk # [Ctrl-A Ctrl-C] # ./uptime.pl # [Ctrl-A Shift-D Shift-D] # exit # use warnings; use strict; use IO::Socket::INET; $/ = "\r\n"; my $socket = IO::Socket::INET->new( PeerAddr => 'localhost', PeerPort => 3009, Proto => 'tcp', ) or die "Couldn't open socket: $!"; print "Registering tov.\n"; print $socket "REG tov\r\n"; while (<$socket>) { s/\r\n$// || s/\n$//; print "Received: $_\n"; next unless (/^tov/); my $reply = uptime(); # Put the reply after the second field, removing the first field. s/^([^\t]*\t)([^\t]*)/$2 $reply/ || next; print "Replying: ", $_, "\n"; print $socket $_, "\r\n"; } sub uptime { open(UPTIME, "uptime|"); my $uptime = ; close UPTIME; $uptime =~ s/up (\d+ days)/up \x02$1\x02/; return "tov: $uptime"; }