$ cat > lab9c2.pl #!/usr/bin/perl -w open(FILE,$ARGV[0]) or die(\"Could not open
ID: 3824502 • Letter: #
Question
$ cat > lab9c2.pl
#!/usr/bin/perl -w
open(FILE,$ARGV[0]) or die("Could not open the file $ARGV[0]");
while ( $line = <FILE> ) {
($loginID, $term, $ip) = split(' ', $line);
($IPclass) = split('.', $ip);
print "IP Class of $IPclass ";
}
close(FILE);
$
$ ./lab9c2.pl loginfile4lab9
IP Class of 162
IP Class of 10
IP Class of 10
IP Class of 50
IP Class of 107
IP Class of 10
IP Class of 73
IP Class of 73
IP Class of 10
IP Class of 50
$
Create a file signinhw10 as below:
last -wi | grep "Feb 22" | head -10 >signinhw10
Question:
Use lab9c2.pl introduced in Lab 9 and the code fragment that follows it as the basis to write a perl script, hw10a.pl, to compute and print the number of the class
B IP addresses
• Here is how the program would be executed. The output from my program is also given. To improve clarity, I have deleted a couple of warnings I got.
$ ./hw10a.pl signinhw10
Number of Class B addresses is 2
Explanation / Answer
#!/usr/bin/perl -w
open(FILE,$ARGV[0]) or die("Could not open the file $ARGV[0]");
my $count = 0;
while ( $line = <FILE> ) {
(undef, undef, $ip) = split(' ', $line);
($IPclass) = split('.', $ip);
if ($IPclass >= 128 && $IPclass <= 191)
{
$count += 1;
}
}
close(FILE);
print "Number of Class B addresses is $count ";
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.