Posted By

jsinix on 12/01/14


Tagged

perl management inventory


Versions (?)

Perl inventory management script


 / Published in: Perl
 

URL: www.jsinix.com

Author: jsinix([email protected]

This script can be used to create, delete and update device inventory. This is a basic script that can be modified and improved upon to make it serve better .

  1. #!/usr/bin/perl -w
  2. use CGI qw(:standard);
  3. use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
  4. use strict;
  5. use Switch;
  6.  
  7. print header;
  8. print start_html("Device Status DB");
  9.  
  10. my @fieldnames = param();
  11.  
  12. my $device = param('devicename');
  13. chomp($device);
  14. my $devicelen = length($device);
  15.  
  16. if($devicelen > 15) {
  17. print "Device name should be less than 15 characters.";
  18. exit();
  19. }
  20.  
  21. my $description = param('description');
  22. my $descriptionlen = length($description);
  23.  
  24. if($descriptionlen > 150) {
  25. print "Description should be less than 150 characters.";
  26. exit();
  27. }
  28.  
  29. my $password = param('pass');
  30. chomp($password);
  31. #print $password;
  32. #my $pwdbit0 = pass_bit($password);
  33. #print $pwdbit0;
  34.  
  35.  
  36. my $replacebit = param('replace');
  37. chomp($replacebit);
  38.  
  39. my $selection = param('select');
  40. chomp($selection);
  41.  
  42. switch ($selection) {
  43. case "add" {add_entry($device, $description);}
  44.  
  45. case "query" {query_entry($device);}
  46.  
  47. case "remove" {remove_entry($device);}
  48.  
  49. else {print "No valid option\n";}
  50. }
  51.  
  52. sub add_entry {
  53. my ($dev1, $des1) = @_;
  54. my $pbit = is_present($dev1);
  55. my $pwdbit1 = pass_bit($password);
  56.  
  57. if($pwdbit1 != 1) {
  58. print "Incorrect password";
  59. exit();
  60. }
  61.  
  62. if($pbit eq "1") {
  63. if($replacebit == 1) {
  64. create_device($dev1, $des1);
  65. print "Updated";
  66. } else {
  67. print "Device already present. If you want to replace select the 'Replace existing entry' and try again.";
  68. }
  69. } else {
  70. create_device($dev1, $des1);
  71. print "Created";
  72. }
  73.  
  74. }
  75.  
  76. sub query_entry {
  77. my ($dev2) = @_;
  78. my $qqbit = is_present($dev2);
  79. my $file_location = "DeviceDB/$dev2";
  80. if($qqbit == 1) {
  81. my $result1 = `cat $file_location`;
  82. print $result1;
  83. } else {
  84. print "Entry not present";
  85. }
  86. }
  87.  
  88. sub remove_entry {
  89. my ($dev5) = @_;
  90. my $ppbit = is_present($dev5);
  91. my $file_location = "DeviceDB/$dev5";
  92.  
  93. my $pwdbit2 = pass_bit($password);
  94.  
  95. if($pwdbit2 != 1) {
  96. print "Incorrect password";
  97. exit();
  98. }
  99.  
  100. if($ppbit == 1) {
  101. `rm $file_location`;
  102. print "Entry removed";
  103. } else {
  104. print "Entry not present already";
  105. }
  106. }
  107.  
  108. sub is_present {
  109. my ($dev3) = @_;
  110. my $file_location = "DeviceDB/";
  111. my $res = `ls -1 $file_location | grep $dev3`;
  112.  
  113. if($res) {
  114. return 1;
  115. } else {
  116. return 0;
  117. }
  118. }
  119.  
  120. sub create_device {
  121. my ($dev4,$des4) = @_;
  122. my $file_location = "DeviceDB/$dev4";
  123. open my $fh, '>', $file_location or die $!;
  124. print $fh $des4;
  125. close $fh;
  126. }
  127.  
  128. sub pass_bit {
  129. my ($pass1) = @_;
  130. if($pass1 eq "12345") {
  131. return 1;
  132. } else {
  133. return 0;
  134. }
  135. }
  136.  
  137. #my $link = "#";
  138. #my $text = "Homepage";
  139. #print "<br><br><br>";
  140. #print "<a href=\"$link\">$text</a>"."\t";
  141.  
  142. print end_html;

Report this snippet  

You need to login to post a comment.