Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use Data::Validate::IP qw(is_ipv4);
- $SIG{'INT'} = \&logout;
- $SIG{'QUIT'} = \&logout;
- $SIG{'TSTP'} = \&logout;
- sub logout {system("clear");
- exit;}
- $green = "\033[0;40;32m";
- $red = "\033[0;40;31m";
- $normal = "\033[0m";
- $hostname = `/bin/hostname`;
- chomp $hostname;
- $gateway = `cat /etc/sysconfig/network | grep GATEWAY`;
- @gateway_parts = split (/=/, $gateway);
- $gateway = $gateway_parts[1];
- chomp($gateway);
- $ip = `cat /etc/sysconfig/network-scripts/ifcfg-eth0 | grep IPADDR`;
- @ip_parts = split (/=/, $ip);
- $ip = $ip_parts[1];
- chomp($ip);
- $nm = `cat /etc/sysconfig/network-scripts/ifcfg-eth0 | grep NETMASK`;
- @nm_parts = split (/=/, $nm);
- $nm = $nm_parts[1];
- chomp($nm);
- @ns = `cat /etc/resolv.conf | grep nameserver`;
- ($ns1, $ns2, $ns3) = "";
- $ctr = 1;
- foreach(@ns){
- @ns_parts = split (" ", $_);
- if ($ctr == 1){
- $ns1 = $ns_parts[1];
- } elsif ($ctr == 2){
- $ns2 = $ns_parts[1];
- } elsif ($ctr == 3){
- $ns3 = $ns_parts[1];
- }
- $ctr++;
- }
- $shownote = 0;
- sub show_menu {
- system("clear");
- menu('',H);
- menu(' Modify Network Settings ',H);
- if (-e "/home/support/reboot_required"){
- menu(' REBOOT REQUIRED TO ENABLE CHANGES ',H);
- } else {
- menu('',H);
- }
- menu('',B);
- menu('',B);
- menu("1 - IP Address",T);
- menu("$ip",R);
- menu("2 - Subnet Mask",T);
- menu("$nm",R);
- menu("3 - Default gateway",T);
- menu("$gateway",R);
- menu("4 - Name Server 1",T);
- menu("$ns1",R);
- menu("5 - Name Server 2",T);
- menu("$ns2",R);
- menu("6 - Name Server 3",T);
- menu("$ns3",R);
- print "\n";
- menu("1-6 - Modify a setting. \n",P);
- if ($shownote == 1){
- menu("S - Save changes and reboot the system. \n",P);
- }
- menu("A - Abort changes and exit to the main menu. \n\n",P);
- menu("Please select an option> ",P);
- }
- show_menu();
- while(<STDIN>){
- if ($_ == 1){
- system("clear");
- print "\n" x 4;
- menu("Enter a new IP address and press <ENTER>.\n",P);
- menu("",P);
- $ip = <STDIN>;
- chomp $ip;
- $shownote = 1;
- logger("Entered a new IP address: $ip.");
- show_menu();
- } elsif ($_ == 2){
- system("clear");
- print "\n" x 4;
- menu("Enter a new subnet mask and press <ENTER>.\n",P);
- menu("",P);
- $nm = <STDIN>;
- chomp $nm;
- $shownote = 1;
- logger("Entered a new subnet mask: $nm.");
- show_menu();
- } elsif ($_ == 3){
- system("clear");
- print "\n" x 4;
- menu("Enter a new default gateway and press <ENTER>.\n",P);
- menu("",P);
- $gateway = <STDIN>;
- chomp $gateway;
- $shownote = 1;
- logger("Entered a new default gateway: $gateway.");
- show_menu();
- } elsif ($_ == 4){
- system("clear");
- print "\n" x 4;
- menu("Enter a new primary name sever and press <ENTER>.\n",P);
- menu("",P);
- $ns1 = <STDIN>;
- chomp $ns1;
- $shownote = 1;
- logger("Entered a new primary name server: $ns1.");
- show_menu();
- } elsif ($_ == 5){
- system("clear");
- print "\n" x 4;
- menu("Enter a new secondary name sever and press <ENTER>.\n",P);
- menu("",P);
- $ns2 = <STDIN>;
- chomp $ns2;
- $shownote = 1;
- logger("Entered a new secondary name server: $ns2.");
- show_menu();
- } elsif ($_ == 6){
- system("clear");
- print "\n" x 4;
- menu("Enter a new tertiary name sever and press <ENTER>.\n",P);
- menu("",P);
- $ns3 = <STDIN>;
- chomp $ns3;
- $shownote = 1;
- logger("Entered a new tertiary name server: $ns3.");
- show_menu();
- } elsif ($_ =~ /A/i){
- logger("User aborted all changes.");
- exit;
- } elsif ($_ =~ /S/i){
- logger("Saved configuration.");
- `touch /home/support/reboot_required`;
- open F, "> /home/support/ifcfg-eth0";
- print F "DEVICE=eth0\n";
- print F "BOOTPROTO=static\n";
- print F "IPADDR=$ip\n";
- print F "NETMASK=$nm\n";
- print F "ONBOOT=yes\n";
- close F;
- `sudo /bin/rm -rf /etc/sysconfig/network-scripts/ifcfg-eth0`;
- `sudo /bin/mv /home/support/ifcfg-eth0 /etc/sysconfig/network-scripts/`;
- open F, "> /home/support/network";
- print F "NETWORKING=yes\n";
- print F "HOSTNAME=$hostname\n";
- print F "GATEWAY=$gateway\n";
- close F;
- `sudo /bin/rm -rf /etc/sysconfig/network`;
- `sudo /bin/mv /home/support/network /etc/sysconfig/`;
- open F, "> /home/support/resolv.conf";
- if (length($ns1) > 0){
- print F "nameserver $ns1\n";
- }
- if (length($ns2) > 0){
- print F "nameserver $ns2\n";
- }
- if (length($ns3) > 0){
- print F "nameserver $ns3\n";
- }
- close F;
- `sudo /bin/rm -rf /etc/resolv.conf`;
- `sudo /bin/mv /home/support/resolv.conf /etc/`;
- menu(' The settings have been saved. ',M);
- sleep 2;
- menu(' Reboot required to enable changes. ',M);
- sleep 2;
- menu(' Do you want to reboot now? ',M);
- logger("Reboot message displayed.");
- menu("y/N>",PC);
- $line = <STDIN>;
- if ($line =~ /y/i){
- logger("System rebooting.");
- system("sudo /usr/bin/reboot");
- } else {
- logger("System NOT rebooted by request.");
- }
- exit;
- } else {
- logger("User selected invalid option: $_");
- menu(' Invalid selection - please try again ',M);
- sleep 2;
- show_menu();
- }
- }
- sub menu {
- ($line, $type) = @_;
- chomp ($width = `tput cols`);
- $len = length($line);
- # Print alert messages
- if ($type eq "M"){
- system("clear");
- print "\n" x 10;
- $box_width = $width - 20;
- print " " x 10 . "*" x $box_width . "\n";
- $sides = ($box_width - $len) / 2;
- print " " x 10 . "*" x $sides . $line . "*" x $sides . "\n";
- print " " x 10 . "*" x $box_width . "\n";
- }
- # Print a test line
- if ($type eq 'T'){
- $dots = $width - $len - 30;
- print " " . $line . "." x $dots;
- }
- # Print the line centered
- if ($type eq 'PC'){
- $sidepad = ($width - $len) / 2;
- print " " x $sidepad . $line;
- }
- # Print the line centered
- if ($type eq 'P'){
- $sidepad = ($width - $len) / 2;
- print " " x $sidepad . $line;
- }
- # Print a test result
- if ($type eq 'R'){
- print "[ " . $line . " ] \n";
- }
- # Print menu options
- if ($type eq "O"){
- print " " x 23 . $line;
- print "\n";
- }
- # Print a blank line
- if ($type eq 'B'){
- print "\n";
- }
- # Clear the screen
- if ($type eq 'C'){
- system("clear");
- }
- # Print menu headers
- if ($type eq 'H'){
- $sides = ($width - $len) / 2;
- print "*" x $sides;
- print "$line";
- print "*" x $sides;
- print "\n";
- }
- }
- sub logger {
- my ($msg) = @_;
- my $date = `/bin/date`;
- chomp $date;
- open L, ">> /home/support/support.log";
- print L "$date: settings.pl -> $msg\n";
- close L;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement