- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- perl socket script
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-21-2004 07:59 AM
тАО05-21-2004 07:59 AM
socket.server.perl
socket.client.perl
All it does is send a text string from
client to server and puts it in a file.
Can someone tell me how I can use this
example script to send and run a command on the
remote server instead of just sending the
string to a file on the remote server? See
code below. Is there is perl execute command
if recieved on the server side?
socket.client.perl
#!/usr/bin/perl
#===============================================
# Client -- using object interface
# Support Windows and UNIX
#===============================================
use IO::Socket;
my $sock = new IO::Socket::INET (
PeerAddr => '128.166.11.13',
PeerPort => '9999',
Proto => 'tcp',
);
die "Could not create socket: $!\n" unless $sock;
print $sock "Hi there!\n";
$sock->send("Hi again!\n");
close($sock);
socket.server.perl
#!/usr/bin/perl
#===============================================
# Server -- using object interface
# Support Windows and UNIX
#===============================================
#use Proc::Daemon;
use IO::Socket;
#Proc::Daemon::Init();
use POSIX qw(setsid);
sub daemonize {
die "Can't fork" unless defined (my $child = fork());
exit 0 if $child;
setsid();
open(STDIN, "open(STDOUT, ">/dev/null");
open(STDERR, ">&STDOUT");
chdir '/';
umask(0);
#$ENV{PATH} = '/bin:/sbin:/usr/bin:/usr/sbin';
return $$;
};
&daemonize;
while() {
Solved! Go to Solution.
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-21-2004 08:15 AM
тАО05-21-2004 08:15 AM
Solutionmy $dir = '/etc';
my $cmd = sprintf("ls -l %s",$dir);
my $cc = system($cmd);
This is just to illustrate what is possible. I would be rather careful in how you craft this because this could be a huge security hole. Normally it's not a good idea to build a general purpose remote command executor like this because it would be rather easy to do something rm -r * from /. The safer approach is to build a server that can only perform very limited actions.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-21-2004 08:42 AM
тАО05-21-2004 08:42 AM
Re: perl socket script
entirety.
It would be taylored to only execute one
command not an open perl script. Better
than having root .rhosts everywhere.
I think where it does the:
print F $Line;
at the bottom is where I could put some
kind of execute statement.
Take the string and run it not print it
to a file.
socket.client.perl
#!/usr/bin/perl
#===============================================
# Client -- using object interface
# Support Windows and UNIX
#===============================================
use IO::Socket;
my $sock = new IO::Socket::INET (
PeerAddr => '128.166.11.13',
PeerPort => '9999',
Proto => 'tcp',
);
die "Could not create socket: $!\n" unless $sock;
print $sock "Hi there!\n";
$sock->send("Hi again!\n");
close($sock);
socket.server.perl
#!/usr/bin/perl
#===============================================
# Server -- using object interface
# Support Windows and UNIX
#===============================================
#use Proc::Daemon;
use IO::Socket;
#Proc::Daemon::Init();
use POSIX qw(setsid);
sub daemonize {
die "Can't fork" unless defined (my $child = fork());
exit 0 if $child;
setsid();
open(STDIN, "open(STDOUT, ">/dev/null");
open(STDERR, ">&STDOUT");
chdir '/';
umask(0);
#$ENV{PATH} = '/bin:/sbin:/usr/bin:/usr/sbin';
return $$;
};
&daemonize;
while() {
my $new_sock = $sock->accept();
####################################
# Put a message to file. #
####################################
while(defined($Line = <$new_sock>)) {
open (F,"+>>/lhome/root/testfile.txt");
print F $Line;
close F;
};
close($sock);
}; #End of while cycle
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-21-2004 08:47 AM
тАО05-21-2004 08:47 AM
Re: perl socket script
Typically, your client should send a formatted request string of some type. I like to use
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-21-2004 09:03 AM
тАО05-21-2004 09:03 AM
Re: perl socket script
Thanks Clay. This solves many problems here.
No more .rhosts to remote shutdown dbs for
backups. No more buying expensive agents
to do remote shutdowns etc...
Now I have to have some kind of check on
the server side for valid commands.
I don't understand your tab format??
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-21-2004 11:14 AM
тАО05-21-2004 11:14 AM
Re: perl socket script
Let's suppose that your server does 4 things:
1) lists files in a dedicated directories
2) removes 1 file
3) add's a line to an existing file
4) kill the server
Your create client requests that adhere to this, for example, you formant your protocol like this
request_code
Now every line sent to the server expects these 3 arguments -- although some may simply be dummy args. Tabs are nice because unlike spaces or colons or commas they are not typically part of strings. Typically the request_code is something very easy to parse; e.g 1 - list; 2 - remove, 4 - kill,kill,kill
If you wanrt to see some examples of this along with a little explanation, go to www.sysadminmag.com and look under the April '03 issue. I wrote an article that used Perl a Perl client/server pair to implement multi-host semaphores. Their web page also has a source code link where you will find the client and server pieces.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-24-2004 01:56 AM
тАО05-24-2004 01:56 AM
Re: perl socket script
above me at this point.
For now though. I just wanted something
simple to get around the root .rhosts problem. I was thinking as a security measure
of passing a code to the server.pl but don't
know the perl syntax at this time.
Could you show me the simple syntax for
passing two args.
e.g.
From client.pl send:
1234 shutdown.sh
On server.pl check code then run command:
1234 shutdown.sh
On my client.perl I currently have:
print $sock "shutdown.sh"
On my server.perl I have:
while(defined($Line = <$new_sock>)) {
system($Line);
close($sock);
}; #End
I don't know how to separate the first arg
"1234" and test if it is a match before
running shutdown.sh. I assume this is what
you mean by tab delimits and I assume it
would be in your code I looked at on:
http://www.samag.com/documents/s=7898/sam0304a/0304a.htm