Perl and sending file to server


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl and sending file to server
# 1  
Old 06-27-2009
Perl and sending file to server

Hi,

I am trying to write some test code for bigger project where my perl script will send file over to server. This is the current SERVER and CLIENT code I have so far. When I type "hi" from client I get hello back from server and like wise I send send command I get respond back the problem comes when I type "put <filename>" on client, the file transfer finishes but I get ERROR in client "Can't use an undefined value as a symbol reference at ./client.pl line 43."

Any help in debugging this issue would be highly appreciated.


SERVER CODE:

HTML Code:
#!/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 CODE:

HTML Code:
#!/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;
}
# 2  
Old 07-02-2009
While this is a lot of work, and lots of good code, it smells like homework.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Web Development

Sending file to WebDav Server

Hi All, I am using a webdav server host name : abc.xyz.com.ak username : user123 password : password123 port : 80 I need to send files stored in my windows system to the server, any idea how to do it. I dont know how to create a url in linux for webdav server using details. (2 Replies)
Discussion started by: prakhar_dubey
2 Replies

2. Shell Programming and Scripting

Sending zip file as attachments in perl

Hi All, i have a code, where that script is sending mail to the users, but i want to enhance it, i.e i need to add attachment(zip file) to that, i dont want to use MIME:LITE module or any other module, with this simple code, i need to enhance. below is my code my %mail_params = (HTML ... (13 Replies)
Discussion started by: asak
13 Replies

3. Shell Programming and Scripting

Perl: Sending file from UNIX server to Windows server

I'm trying to write a Perl script where a file from a UNIX server box connects to a Windows server box and copies that file into the Window box. The main problem I have right now is that whenever I try to connect to the Windows box, the connection is refused. The error message that always pops... (2 Replies)
Discussion started by: kooshi
2 Replies

4. Shell Programming and Scripting

extract string and sending it into a new file in perl program

Hi, I have a input file with following values (test.out) I would like to grep all lines with word 'PANIC' and sent it another file using perl program with grep command. I have been trying different ways and not working. Pls advice. Thanks a lot for the help. --example--... (3 Replies)
Discussion started by: hudson03051nh
3 Replies

5. Shell Programming and Scripting

Sending a file to 24 Server locations parallely

Hi All, I have to send a processed file to 24 different server locations. I feel, if this job can be done parallel in the background - Time will come down. I found the script to FTP the file for a single server location through past Unix posts as below: #!/usr/bin/ksh ftp -v -n... (16 Replies)
Discussion started by: vsmeruga
16 Replies

6. Shell Programming and Scripting

Sending email and attachment file using Perl

I am trying to send an email body of information and also attachment using perl script, but I am only able to send the body but not an attachment. is there around it without using "use MIME::Lite;" module. $user = "bataf\@xyz.com"; $subjectt = "mail from perl"; open(MAIL, "| mailx -s... (1 Reply)
Discussion started by: bataf
1 Replies

7. Solaris

Sending a file to a server

Dear Experts, please help i am very much confused here and not getting the right path, actually i want to send a file from client to a server, initially i used mmap() which is failing most of the time so i want to use general IO streams to do that, i can open a file in "rb" mode and read... (1 Reply)
Discussion started by: vin_pll
1 Replies

8. Shell Programming and Scripting

Reading File from Server - perl

Hey, I'm trying to read a file from a server. Simple file with some numbers. Here is the code i'm running. use Net::SSH::Perl::SSH1 ; $scon = Net::SSH::Perl->new ("com123.sever.mydomain.com",(protocol=>'2',port=>'22',debug=>'true')); $scon->login("user123","pass123"); open(FILE,... (9 Replies)
Discussion started by: Phi01
9 Replies

9. Programming

sending file from server to client

hi dear i m very new to socket programing . i need the source code which sends file from server to client . i mean both server n client programe which sends file . can u do this for me please my email id is email id removed regards bilal (1 Reply)
Discussion started by: bilal
1 Replies

10. Shell Programming and Scripting

FTP script for sending a file from one unix directory to another unix server director

Hi, My local server is :/usr/abcd/ Remote server is :/Usr/host/test/ I want to send files from local unix directory(All files starting with O_999) to remote host unix directory. Can any body give me the Unix Shell script to do this. One more doubt: Shall we need to change the file... (1 Reply)
Discussion started by: raja_1234
1 Replies
Login or Register to Ask a Question