in reply to Defactor this code

You can write your input loop so that you don't repeat yourself. Also, I think you probably do not want to allow port numbers ten digits long. It's probably best to test whether the number is in an acceptable range.

my $sshport; while ( $sshport !~ /^[1-9]\d+$/ || $sshport > 65535 ) { print " Please Enter valid ssh port number:\n"; chomp( $sshport = <STDIN> ); }

It might be even better to use something like IO::Prompt.

It's probably not necessary to test whether the line matches the pattern you want to replace before doing the replacement. If the pattern in the replacement doesn't match, nothing will be done anyway. That said, maybe you really did intend for the replacements to be done in circumstances slightly different than when the replacement matches. The code below behaves a little differently than yours, but I think it's closer to what you meant.

while (<SSHD_CONFIG>){ s/^Port.+/Port $sshport/i; s/^#Port\s.+/Port $sshport/i; print SSHD_CONFIG_NEW $_; }

You could even make those two replacements into one (s/^(?:Port|#Port\s).+/Port $sshport/i).

You should check whether rename worked:

rename('sshd_config','sshd_config.old') or die "Can't rename sshd_config: $!"; rename('sshd_config.new','sshd_config') or die "Can't rename sshd_config.new: $!";