Guest User

Untitled

a guest
Apr 20th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.23 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use IO::Socket;
  4.  
  5. $hostname=`hostname`;
  6. $hostname=~s/\n//g;
  7.  
  8. if ($#ARGV!=0) {
  9.     printf "Synopsis: server.pl <port>\n\n";
  10.     exit(1);
  11. }
  12.  
  13. printf "Please enter the password for this server:\n";
  14.  
  15. my $password = <STDIN>;
  16.  
  17. #Get rid of \n
  18. $password=~s/\n//g;
  19.  
  20. for(;;) {
  21.  
  22.     my $sock = new IO::Socket::INET (
  23.                      LocalHost => "$hostname",
  24.                      LocalPort => $ARGV[0],
  25.                      Proto => 'tcp',
  26.                      Listen => 1,
  27.                      Reuse => 1,
  28.                      );
  29.     die "Could not create socket: $!\n" unless $sock;
  30.    
  31.     my $new_sock = $sock->accept();
  32.    
  33.     my $authenticated=0;
  34.    
  35.     while(<$new_sock>) {
  36.    
  37.     $in=$_;
  38.     #Get rid of \n
  39.     $in=~s/\n//g;
  40.    
  41.     #If this is a new connection, check if the client knows the password
  42.     if ($authenticated==0) {
  43.         if ($in ne $password) {
  44.         print $new_sock "Access denied... Good Bye ;-)\n";
  45.         close($new_sock);
  46.         }
  47.         else {
  48.         $authenticated=1;
  49.         }
  50.     }
  51.     else {
  52.    
  53.         if ($in eq "quit") {
  54.         close($new_sock);
  55.         }
  56.  
  57.         $res="";
  58.  
  59.         if ($in ne "") {
  60.             $res=`grep -i "$in" phone.txt`;
  61.         }
  62.  
  63.         if ($res eq "") {
  64.         print $new_sock "No information found\n";
  65.         }
  66.         else {
  67.         print $new_sock "$res";
  68.         }
  69.     }
  70.     }
  71. }
Add Comment
Please, Sign In to add comment