To send a command via TCP with Perl, you can use IO::Socket::INET:
use IO::Socket; my $sock = new IO::Socket::INET ( PeerAddr => 'localhost', PeerPort => '1001', Proto => 'tcp', ); die "Error: $!\n" unless $sock; print $sock "commandtoexecute\n"; close($sock); |
This will send the command commandtoexecute to port 1001 on localhost or complain that there is an error creating the socket.
If you want to show the response, simply add these lines after the print statement:
$answer=<$sock>; print $answer; |
The return status from the server will print out:
srv-5:~/nawperl usr4$ perl t.pl OK srv-5:~/nawperl usr4$ cat t.pl use IO::Socket; my $sock = new IO::Socket::INET ( PeerAddr => 'localhost', PeerPort => '1001', Proto => 'tcp', ); die "Error: $!\n" unless $sock; print $sock "commandtoexecute\n"; $answer=<$sock>; print $answer; close($sock); srv-5:~/nawperl usr4$ |
The server replies OK after receiving the command commandtoexecute. Here is how this looks in a telnet session:
srv-5:~/nawperl usr4$ telnet localhost 1001 Connected to localhost. Escape character is '^]'. commandtoexecute OK ^] telnet> quit Connection closed. srv-5:~/nawperl usr4$ |
For more information, see IO::Socket::INET: