Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use IO::Socket;
- $hostname=`hostname`;
- $hostname=~s/\n//g;
- if ($#ARGV!=0) {
- printf "Synopsis: server.pl <port>\n\n";
- exit(1);
- }
- printf "Please enter the password for this server:\n";
- my $password = <STDIN>;
- #Get rid of \n
- $password=~s/\n//g;
- for(;;) {
- my $sock = new IO::Socket::INET (
- LocalHost => "$hostname",
- LocalPort => $ARGV[0],
- Proto => 'tcp',
- Listen => 1,
- Reuse => 1,
- );
- die "Could not create socket: $!\n" unless $sock;
- my $new_sock = $sock->accept();
- my $authenticated=0;
- while(<$new_sock>) {
- $in=$_;
- #Get rid of \n
- $in=~s/\n//g;
- #If this is a new connection, check if the client knows the password
- if ($authenticated==0) {
- if ($in ne $password) {
- print $new_sock "Access denied... Good Bye ;-)\n";
- close($new_sock);
- }
- else {
- $authenticated=1;
- }
- }
- else {
- if ($in eq "quit") {
- close($new_sock);
- }
- $res="";
- if ($in ne "") {
- $res=`grep -i "$in" phone.txt`;
- }
- if ($res eq "") {
- print $new_sock "No information found\n";
- }
- else {
- print $new_sock "$res";
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment