Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 7.45 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use Data::Validate::IP qw(is_ipv4);
  3.  
  4. $SIG{'INT'} = \&logout;
  5. $SIG{'QUIT'} = \&logout;
  6. $SIG{'TSTP'} = \&logout;
  7. sub logout {system("clear");
  8.             exit;}
  9.  
  10. $green  = "\033[0;40;32m";
  11. $red    = "\033[0;40;31m";
  12. $normal = "\033[0m";
  13.  
  14. $hostname = `/bin/hostname`;
  15. chomp $hostname;
  16.  
  17. $gateway = `cat /etc/sysconfig/network | grep GATEWAY`;
  18. @gateway_parts = split (/=/, $gateway);
  19. $gateway = $gateway_parts[1];
  20. chomp($gateway);
  21.  
  22. $ip = `cat /etc/sysconfig/network-scripts/ifcfg-eth0 | grep IPADDR`;
  23. @ip_parts = split (/=/, $ip);
  24. $ip = $ip_parts[1];
  25. chomp($ip);
  26.  
  27. $nm = `cat /etc/sysconfig/network-scripts/ifcfg-eth0 | grep NETMASK`;
  28. @nm_parts = split (/=/, $nm);
  29. $nm = $nm_parts[1];
  30. chomp($nm);
  31.  
  32. @ns = `cat /etc/resolv.conf | grep nameserver`;
  33. ($ns1, $ns2, $ns3) = "";
  34. $ctr = 1;
  35.  
  36. foreach(@ns){
  37.     @ns_parts = split (" ", $_);
  38.     if ($ctr == 1){
  39.         $ns1 = $ns_parts[1];
  40.     } elsif ($ctr == 2){
  41.         $ns2 = $ns_parts[1];
  42.     } elsif ($ctr == 3){
  43.         $ns3 = $ns_parts[1];
  44.     }
  45.     $ctr++;
  46. }
  47.  
  48. $shownote = 0;
  49.  
  50. sub show_menu {
  51.     system("clear");
  52.  
  53.     menu('',H);
  54.     menu('  Modify Network Settings  ',H);
  55.     if (-e "/home/support/reboot_required"){
  56.         menu('  REBOOT REQUIRED TO ENABLE CHANGES  ',H);
  57.     } else {
  58.         menu('',H);
  59.     }
  60.     menu('',B);
  61.     menu('',B);
  62.  
  63.  
  64.     menu("1 - IP Address",T);
  65.     menu("$ip",R);
  66.  
  67.     menu("2 - Subnet Mask",T);
  68.     menu("$nm",R);
  69.  
  70.     menu("3 - Default gateway",T);
  71.     menu("$gateway",R);
  72.  
  73.     menu("4 - Name Server 1",T);
  74.     menu("$ns1",R);
  75.  
  76.     menu("5 - Name Server 2",T);
  77.     menu("$ns2",R);
  78.  
  79.     menu("6 - Name Server 3",T);
  80.     menu("$ns3",R);
  81.  
  82.     print "\n";
  83.     menu("1-6 - Modify a setting.                          \n",P);
  84.  
  85.     if ($shownote == 1){
  86.     menu("S   - Save changes and reboot the system.        \n",P);
  87.     }
  88.  
  89.     menu("A   - Abort changes and exit to the main menu.   \n\n",P);
  90.     menu("Please select an option> ",P);
  91. }
  92.  
  93. show_menu();
  94.  
  95. while(<STDIN>){
  96.  
  97.     if ($_ == 1){
  98.         system("clear");
  99.         print "\n" x 4;
  100.         menu("Enter a new IP address and press <ENTER>.\n",P);
  101.         menu("",P);
  102.         $ip = <STDIN>;
  103.         chomp $ip;
  104.         $shownote = 1;
  105.         logger("Entered a new IP address: $ip.");
  106.         show_menu();
  107.     } elsif ($_ == 2){
  108.         system("clear");
  109.         print "\n" x 4;
  110.         menu("Enter a new subnet mask and press <ENTER>.\n",P);
  111.         menu("",P);
  112.         $nm = <STDIN>;
  113.         chomp $nm;
  114.         $shownote = 1;
  115.         logger("Entered a new subnet mask: $nm.");
  116.         show_menu();
  117.     } elsif ($_ == 3){
  118.         system("clear");
  119.         print "\n" x 4;
  120.         menu("Enter a new default gateway and press <ENTER>.\n",P);
  121.         menu("",P);
  122.         $gateway = <STDIN>;
  123.         chomp $gateway;
  124.         $shownote = 1;
  125.         logger("Entered a new default gateway: $gateway.");
  126.         show_menu();
  127.     } elsif ($_ == 4){
  128.         system("clear");
  129.         print "\n" x 4;
  130.         menu("Enter a new primary name sever and press <ENTER>.\n",P);
  131.         menu("",P);
  132.         $ns1 = <STDIN>;
  133.         chomp $ns1;
  134.         $shownote = 1;
  135.         logger("Entered a new primary name server: $ns1.");
  136.         show_menu();
  137.     } elsif ($_ == 5){
  138.         system("clear");
  139.         print "\n" x 4;
  140.         menu("Enter a new secondary name sever and press <ENTER>.\n",P);
  141.         menu("",P);
  142.         $ns2 = <STDIN>;
  143.         chomp $ns2;
  144.         $shownote = 1;
  145.         logger("Entered a new secondary name server: $ns2.");
  146.         show_menu();
  147.     } elsif ($_ == 6){
  148.         system("clear");
  149.         print "\n" x 4;
  150.         menu("Enter a new tertiary name sever and press <ENTER>.\n",P);
  151.         menu("",P);
  152.         $ns3 = <STDIN>;
  153.         chomp $ns3;
  154.         $shownote = 1;
  155.         logger("Entered a new tertiary name server: $ns3.");
  156.         show_menu();
  157.     } elsif ($_ =~ /A/i){
  158.         logger("User aborted all changes.");
  159.         exit;
  160.     } elsif ($_ =~ /S/i){
  161.  
  162.         logger("Saved configuration.");
  163.         `touch /home/support/reboot_required`;
  164.  
  165.         open F, "> /home/support/ifcfg-eth0";
  166.         print F "DEVICE=eth0\n";
  167.         print F "BOOTPROTO=static\n";
  168.         print F "IPADDR=$ip\n";
  169.         print F "NETMASK=$nm\n";
  170.         print F "ONBOOT=yes\n";
  171.         close F;
  172.         `sudo /bin/rm -rf /etc/sysconfig/network-scripts/ifcfg-eth0`;
  173.         `sudo /bin/mv /home/support/ifcfg-eth0 /etc/sysconfig/network-scripts/`;
  174.  
  175.         open F, "> /home/support/network";
  176.         print F "NETWORKING=yes\n";
  177.         print F "HOSTNAME=$hostname\n";
  178.         print F "GATEWAY=$gateway\n";
  179.         close F;
  180.         `sudo /bin/rm -rf /etc/sysconfig/network`;
  181.         `sudo /bin/mv /home/support/network /etc/sysconfig/`;
  182.  
  183.         open F, "> /home/support/resolv.conf";
  184.         if (length($ns1) > 0){
  185.             print F "nameserver $ns1\n";
  186.         }
  187.         if (length($ns2) > 0){
  188.             print F "nameserver $ns2\n";
  189.         }
  190.         if (length($ns3) > 0){
  191.             print F "nameserver $ns3\n";
  192.         }
  193.         close F;
  194.         `sudo /bin/rm -rf /etc/resolv.conf`;
  195.         `sudo /bin/mv /home/support/resolv.conf /etc/`;
  196.  
  197.         menu('  The settings have been saved.  ',M);
  198.         sleep 2;
  199.         menu('  Reboot required to enable changes.  ',M);
  200.         sleep 2;
  201.         menu('  Do you want to reboot now?  ',M);
  202.         logger("Reboot message displayed.");
  203.         menu("y/N>",PC);
  204.         $line = <STDIN>;
  205.         if ($line =~ /y/i){
  206.             logger("System rebooting.");
  207.             system("sudo /usr/bin/reboot");
  208.         } else {
  209.             logger("System NOT rebooted by request.");
  210.         }
  211.  
  212.         exit;
  213.     } else {
  214.         logger("User selected invalid option: $_");
  215.         menu('  Invalid selection - please try again  ',M);
  216.         sleep 2;
  217.         show_menu();
  218.     }
  219. }
  220.  
  221.  
  222. sub menu {
  223.     ($line, $type) = @_;
  224.     chomp ($width = `tput cols`);
  225.     $len = length($line);
  226.  
  227.     # Print alert messages
  228.     if ($type eq "M"){
  229.         system("clear");
  230.         print "\n" x 10;
  231.         $box_width = $width - 20;
  232.         print " " x 10 . "*" x $box_width . "\n";
  233.         $sides = ($box_width - $len) / 2;
  234.         print " " x 10 . "*" x $sides . $line . "*" x $sides . "\n";
  235.         print " " x 10 . "*" x $box_width . "\n";
  236.     }
  237.  
  238.     # Print a test line
  239.     if ($type eq 'T'){
  240.         $dots = $width - $len - 30;
  241.         print "    " . $line . "." x $dots;
  242.     }
  243.  
  244.     # Print the line centered
  245.     if ($type eq 'PC'){
  246.         $sidepad = ($width - $len) / 2;
  247.         print " " x $sidepad . $line;
  248.     }
  249.  
  250.     # Print the line centered
  251.     if ($type eq 'P'){
  252.         $sidepad = ($width - $len) / 2;
  253.         print " " x $sidepad . $line;
  254.     }
  255.  
  256.     # Print a test result
  257.     if ($type eq 'R'){
  258.         print "[ " . $line . " ]    \n";
  259.     }
  260.  
  261.     # Print menu options
  262.     if ($type eq "O"){
  263.         print " " x 23 . $line;
  264.         print "\n";
  265.     }
  266.  
  267.     # Print a blank line
  268.     if ($type eq 'B'){
  269.         print "\n";
  270.     }
  271.  
  272.     # Clear the screen
  273.     if ($type eq 'C'){
  274.         system("clear");
  275.     }
  276.  
  277.     # Print menu headers
  278.     if ($type eq 'H'){
  279.         $sides = ($width - $len) / 2;
  280.         print "*" x $sides;
  281.         print "$line";
  282.         print "*" x $sides;
  283.         print "\n";
  284.     }
  285. }
  286.  
  287. sub logger {
  288.     my ($msg) = @_;
  289.     my $date = `/bin/date`;
  290.     chomp $date;
  291.     open L, ">> /home/support/support.log";
  292.     print L "$date: settings.pl ->  $msg\n";
  293.     close L;
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement