NetAdminTools.com
 
SignalQ Sites:
NetAdminTools - Coprolite - NoNIC - SpotBridge - NAW
RoboCoop - AreWeDown - SolarPower - SysAdminTools
Xfig - Gold Loaf - GeekPapa - FixGMC - MCJ - FixRambler
Categories:
GNU/Linux | Homebrew designs | Perl | Administration | Backup/Recovery | Bugs/Fixes | Certification | Database | Email | File/Print | Hardware | Information Grab Bag | Interoperability | GNU/Linux ABCs | Monitoring | Name Resolution | Network Services | Networking | Remote Control | Security | Desktop | Web | BSD | Solaris | GIAGD | REALbasic

Last 30 Days | Last 60 Days | Last 90 Days | All Articles | RSS | Hail Support


Categories:
·GNU/Linux
·Homebrew designs
·Perl
·Administration
·Backup/Recovery
·Bugs/Fixes
·Certification
·Database
·Email
·File/Print
·Hardware
·Information Grab Bag
·Interoperability
·GNU/Linux ABCs
·Monitoring
·Name Resolution
·Network Services
·Networking
·Remote Control
·Security
·Desktop
·Web
·BSD
·Solaris
·GIAGD
·REALbasic
·All Categories


Sending a Command Via TCP With Perl
Topic: Perl   Posted:2006-04-23
Printer Friendly: Print

spacerspacer
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:




Please read our Terms of Use
Microsoft, Windows, Windows XP, Windows 2003, Windows 2000, and NT are either trademarks or registered trademarks of Microsoft Corporation. NetAdminTools.com is not affiliated with Microsoft Corporation. Linux is a registered trademark of Linus Torvalds, and refers to the Linux kernel. The operating system of most distributions that contain the Linux kernel is GNU/Linux. All logos and trademarks in this site are property of their respective owner. Copyright 1997-2008 NetAdminTools.com

Created by:
MCJ
MCJ CMS