use strict; die "Usage: $0 port#\n (where port# is a number > 9 and < 65536\n" if ( @ARGV != 1 or $ARGV[0] !~ /^[1-9]\d+$/ or $ARGV[0] >= 65536 ); my $sshport = shift; my $filename = "sshd_config"; open (SSHD_CONFIG,$filename) or die ("can't open $filename for reading: $!"); open (SSHD_CONFIG_NEW,">","$filename.new") or die ("can't open $filename.new for writing: $!"); while (){ s/^(#?)port.+/$1Port $sshport/i; print SSHD_CONFIG_NEW; } ( rename $filename, "$filename.old" and rename "$filename.new", $filename ) or die "rename failed: $!"