in reply to Re^2: Defactor this code
in thread Defactor this code
i have one problem the sshd_config sometime looks like thisAfter we modify the file with above code it becomes like this#Port 22 Port 4444Port 4444 Port 4444
Right, that's what your code in the OP does, and that's what I mentioned in my third "bullet" paragraph as probably being a bug.
Now, you seem to be saying that you simply want to delete all commented lines (the ones beginning with "#"), which is easy enough to do:
But some people prefer to leave the comment lines in, because sometimes these lines hold important information that might be useful someday (e.g. documentation, or details about a prior or alternative configuration that might be needed when conditions change).while (<SSHD_CONFIG>) { next if ( /^#/ ); s/^port.+/Port $sshport/i; print SSHD_CONFIG_NEW; }
That's why the version in my first reply used the parens and the "$1" in the s/// statement, to preserve the "#" character while changing the port number. Actually, I think it may be best to retain the comment lines unchanged, and only edit the "uncommented" line -- this also turns out to be the simplest method:
while (<SSHD_CONFIG>) { s/^port.+/Port $sshport/i; print SSHD_CONFIG_NEW; }
|
|---|