my $sshport; my $errorstate = 0; print "\n Please Enter Custom port for SSH server:\n"; chomp($sshport = ); while (!$errorstate) { if ($sshport =~ /^[1-9]\d{1,9}$/ ) { $errorstate = 1; } else { print " Please Enter valid ssh port number:\n"; chomp($sshport = ); } } open (SSHD_CONFIG,'sshd_config') or die ("Could not able to open the file $!"); open (SSHD_CONFIG_NEW,">",'sshd_config.new') or die ("Could not create the new file $!"); while (){ if (/^port/i) { s/Port.+/Port $sshport/; } elsif (/^#port\s/i) { s/#Port.+/Port $sshport/; } print SSHD_CONFIG_NEW $_; } rename('sshd_config','sshd_config.old'); rename('sshd_config.new','sshd_config');