Guest User

Untitled

a guest
Aug 7th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.10 KB | None | 0 0
  1. //lab 5 client
  2.  
  3.  
  4. #!/usr/bin/perl
  5. #UDP client
  6. use IO::Socket::INET;
  7. use strict;
  8.  
  9. my ($MySocket, $def_msg, $msg, $text, $host, $port);
  10. #takes in host and port as command line arguments
  11. $host = $ARGV[0];
  12. $port = $ARGV[1];
  13. # Create a new socket
  14. $MySocket=new IO::Socket::INET->new(PeerPort=>$port, Proto=>'udp',PeerAddr=>$host);
  15. # Send messages
  16. $def_msg="Enter message to send to server : ";
  17. print "\n",$def_msg;
  18.  
  19. while($msg=<STDIN>)
  20.   {
  21.   chomp $msg;
  22.   if($msg ne '') #if there is a message
  23.     {
  24.       if($MySocket->send($msg))
  25.       {
  26.         print ".....<done>","\n"; #message successfully sent
  27.         if($msg eq "quit"){
  28.           print "Exited\n";    #if client sends quit, exit
  29.           exit 1;
  30.         }
  31.       }
  32.       if($MySocket->recv($text, 128)){  #if a message is received, print it
  33.         print "$text\n";
  34.       }
  35.  
  36.     }
  37.   else
  38.     {
  39.     # Send an empty message to server and exit
  40.       $MySocket->send('');
  41.       exit 1;
  42.     }
  43.   }
  44.  
  45. //lab5 server
  46.  
  47. #!/usr/bin/perl
  48. #UDP Server
  49.  
  50.  
  51. use IO::Socket::INET;
  52. use strict;
  53.  
  54. my ($MySocket, $def_msg, $text, $response, $port);
  55. #takes in port as a command line argument
  56. $port = $ARGV[0];
  57.  
  58. # Create a new socket
  59. $MySocket=new IO::Socket::INET->new(LocalPort=>$port,Proto=>'udp');
  60. # Keep receiving messages from client
  61. $def_msg="\nReceiving message from client.....\n";
  62.  
  63.  
  64. while(1)
  65.  {
  66.  $MySocket->recv($text,128);
  67.  if($text ne '')
  68.  {
  69.      print "\nReceived message '", $text,"'\n";
  70.      $response = "server:".reverse $text;     #responds to client, reverses
  71.      print "$response\n";       #prints server reponse server-side
  72.      $MySocket->send($response);    #prints server reponse client-side
  73.    }
  74.    # If client message is empty, they have exited
  75.  else
  76.   {
  77.    print "Client has exited!";
  78.   }
  79. }
  80.  
  81. //lab 5 step 4
  82.  
  83. #!/usr/bin/perl
  84. use Nmap::Scanner;
  85. use strict;
  86.  
  87. my @results;
  88. my $ipaddr = $ARGV[0]; #File containing list of IPs to scan
  89. my $num_scan = $ARGV[1]; #Number of Scans to run
  90. my $sleep = $ARGV[2];  #How long (seconds) to wait between scans
  91. my $email = $ARGV[3];  #Email to send results to
  92. my $scanner = new Nmap::Scanner;  #new scanner object
  93.  
  94. open FH, "$ipaddr" || die $!;  #open the ip addresses file
  95. my @ip = <FH>;
  96. close(FH);
  97.  
  98. foreach my $L(@ip){
  99.     chomp $L;
  100.     $scanner->add_target($L); #adds each IP to list of scans
  101. }
  102.  
  103. for(my $i=0; $i < $num_scan; $i++){
  104.   push (@results, ($scanner->scan())->as_xml()); #scans and converts results
  105.   sleep $sleep; #waits a predetermined amount of time between scans
  106. }
  107.  
  108. if($email =~  m/(\w[-._\w]*\w@\w[-._\w]*\w\.\w{2,3})/g){
  109.   sendemail($email, @results); #sends results to email
  110. }
  111. else{
  112.   print "Invalid email, results not sent\n";
  113. }
  114.  
  115. #sendmail subroutine, takes in
  116. #an email and a message
  117. sub sendemail{
  118.   my $to = $_[0];
  119.   my $msg = $_[1];
  120.   my $from = "bob_saget\@afv.com";
  121.   my $subject = "Output from Nmap::Scanner";
  122.  
  123.   open(MAIL, "|/usr/sbin/sendmail -t");
  124.   print MAIL "To: $to\n";
  125.   print MAIL "From: $from\n";
  126.   print MAIL "Subject: $subject\n\n";
  127.   print MAIL "Output: $msg";
  128.   close MAIL;
  129. }
  130.  
  131. //
Add Comment
Please, Sign In to add comment