Created
March 11, 2017 12:46
-
-
Save geek-id/50012773ac5934c535d85e6bc228ccf7 to your computer and use it in GitHub Desktop.
Read and write 2 line different location with module fileinput error can't read first line edit and just edit another line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re, sys, os, fileinput | |
configFile = 'sshd_config' | |
if os.path.isfile(configFile) and os.access(configFile, os.R_OK): | |
# code here | |
try: | |
# config = open(configFile, 'r') | |
with open(configFile, 'r') as searchconfig: | |
for config in searchconfig: | |
if 'Port' in config: | |
old_port = config | |
# for rootlogin in searchconfig: | |
if 'Banner' in config: | |
banner = config | |
# print(root_access) | |
print(old_port) | |
print(banner) | |
with fileinput.FileInput(configFile, inplace=True, backup='.bak') as conf: | |
new_port = input('Set Port SSH(default Port 22): ') | |
default = '22' | |
active = input('Do you want activate Banner Login?(Default: disable)[Y|n] ').lower() | |
banner_disable = '#Banner /etc/issue.net' | |
print(active) | |
for configPort in conf: | |
if not new_port: | |
print(configPort.replace(old_port, ('Port %s\n' % default)), end="") | |
else: | |
print(configPort.replace(old_port, ('Port %s\n' % new_port)), end="") | |
for banner_conf in conf: | |
if active == 'y': | |
# print(active) | |
print(banner_conf.replace(banner, (re.sub('^\#','', banner))), end="") | |
elif not active or active == 'n': | |
# print(active) | |
print(banner_conf.replace(banner, (banner_disable)), end="") | |
conf.close() | |
except IOError: | |
print('Something wrong') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment