in reply to Sockets, And Running Commands.
another more perlish way and perhaps of academic interest could be this using the IO::socket-modul
recip is listening on homer ...
... while sndr ...tos@homer ~/tos/tmp # cat recip #!/usr/bin/perl use strict; use warnings; use IO::Socket; my $socket=new IO::Socket::INET (LocalPort => 1960, Proto => 'tcp', Listen => 5, Reuse => 1, ); die "could'nt create socket: $!" unless $socket; while (my $new_socket = $socket->accept()) { while (defined (my $buf = <$new_socket>)) { print $buf; $buf =~ /^ls/ && do { system ("ls") }; } } close ($socket);
... sends some messages from VRANZ ...tos@VRANZ /cygdrive/t/tos/tmp # cat sndr #!/usr/bin/perl use strict; use warnings; use IO::Socket; my $socket=new IO::Socket::INET (PeerAddr => 'homer', PeerPort => 1960, Proto => 'tcp' ); die "couldn't create socket: $!" unless $socket; foreach (1..3) { print $socket "message $_\n"; $socket->flush(); } print $socket "ls\n"; $socket->flush(); close ($socket);
recip receives the messages, dumps them and proceeds the "ls"-cmdtos@VRANZ /cygdrive/t/tos/tmp # perl sndr tos@VRANZ /cygdrive/t/tos/tmp #
Be aware that this code is merely as secure as ftp and telnet are. For secure connections i can imagine that there is something to find at CPAN. Otherwise use the former recommended ssh-utilities.tos@homer ~/tos/tmp # perl recip message 1 message 2 message 3 ls recip sndr sndr~ x
greetings, tos
|
|---|