jojo's blog--快乐忧伤都与你同在
为梦想而来,为自由而生。 性情若水,风起水兴,风息水止,故时而激荡,时又清平……
posts - 11,  comments - 30,  trackbacks - 0
Here is the script in it's
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, "</dev/null");
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
A. Clay Stephenson Expert in this area This member has accumulated 80000 or more points     
May 21, 2004 16:47:45 GMT  10 pts     
In that case, $Line has your input so you can use it to execute whatever you like.

Typically, your client should send a formatted request string of some type. I like to use <tabs> to separate the fields and then your server responds each time it sees a new line of input.



------------------------------------------------------------------------------------------------------

All I am saying in that any client/server architecture, you must establish some sort of protocol.

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<tab>string1<tab>string2<LF>

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.

-----------------------------------------------------------------------------------------------

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

posted on 2009-08-06 12:11 Blog of JoJo 阅读(287) 评论(0)  编辑  收藏 所属分类: 每日一记My Script

只有注册用户登录后才能发表评论。


网站导航:
 

<2024年12月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

常用链接

留言簿(6)

随笔档案

文章分类

文章档案

新闻分类

新闻档案

相册

收藏夹

搜索

  •  

最新评论

阅读排行榜

评论排行榜