PDA

Просмотр полной версии : perl передать файл через socket


malik555
04.07.2009, 14:22
Всем привет !

Подскажите как передать файл через по сокету на
сервер ?

Искал в нете что-то не нашел инфы !

Kaimi
04.07.2009, 14:28
Что есть сервер в данном случае, форма для аплоада файлов или скажем демон самописный принимающий файлы?

ph1l1ster
04.07.2009, 14:31
server:

#!/usr/bin/perl

use strict;
use IO::Socket;

my $filename;

my @command;

my $server = IO::Socket::INET->new(
Listen => 5,
LocalAddr => '127.0.0.1',
LocalPort => 5050,
Proto => 'tcp'
) or die "Can't create server socket: $!";
$server->autoflush(1);

while(my $client = $server->accept)
{
while (<$client>)
{
print $_ ;
@command = split(/ /, $_);

if(lc(trim($command[0])) eq 'time') { print $client &getTimeStamp()."\r\n"; }
elsif(lc(trim($command[0])) eq 'hi') { print $client "Hello!\r\n"; }

## if honeypot has some data to send
elsif(lc(trim($command[0])) eq 'send')
{
print $client "You said to send some data\r\n";
}

## if we see put command then save the file

elsif(lc(trim($command[0])) eq 'put')
{
if(defined($command[1]) && -e trim($command[1]))
{
$filename = trim($command[1]);
print $client "GOAHEAD\r\n";

open FILE, ">./filez_t/$filename";
binmode FILE;
while (<$client>)
{
if (trim($_) eq 'DONE')
{
close FILE;
last;
}

print FILE $_;
}
close FILE;
print $client "Thanks for sending the file\n"
}
}
else { print $client "Unknown Command\r\n"; }
}
}

sub getTimeStamp
{
# Get the all the values for current time
(my $Second, my $Minute, my $Hour, my $Day, my $Month, my $Year, my $WeekDay, my $DayOfYear, my $IsDST) = localtime(time);

# adding 1900 will create a four digit year value
$Year = $Year - 100;

# Adding 1 to month because months don't start at 0
$Month++;

#format the current time into the correct format and return it
return sprintf("%02d%02d%02d%02d%02d%02d",$Year,$Month,$Day,$Hour,$Minute,$Second);
}


sub trim($)
{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
$string =~ s/[\r]+//;
$string =~ s/[\n]+//;
return $string;
}

client:

#!/usr/bin/perl

use strict;
use IO::Socket;

my $server = IO::Socket::INET->new(
PeerAddr => '127.0.0.1',
PeerPort => 5050,
Proto => 'tcp'
) or die "Can't create client socket: $!";
$server->autoflush(1);

my $i = 0;
my $msg;
my @command;
my $filename="who_connected.pl";

while(1)
{
print "ASAP> ";
$msg = <STDIN>;
print $server $msg."\r";

while(<$server>)
{
@command = split(/ /, $msg);

if(lc(trim($command[0])) eq 'put')
{
$filename = trim($command[1]);

open FILE, "$filename" or die "Can't open: $!";
binmode FILE;
while (<FILE>)
{
print $server $_;
print "*";
}
close FILE;
print $server "\nDONE\n";
}
print $_ ."\n";
last if eof($_);
}

}



sub trim($)
{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
$string =~ s/[\r]+//;
$string =~ s/[\n]+//;
return $string;
}

И ещё, http://www.perlmonks.org/?node_id=613739
http://b23.ru/btb

malik555
04.07.2009, 14:31
Что есть сервер в данном случае, форма для аплоада файлов или скажем демон самописный принимающий файлы?


В качестве сервера Демон самописный !

В качестве клиента - тоже обычный скрипт
и передать обычный файл который лежит у клиента на компе !