Skip to content

Instantly share code, notes, and snippets.

@geek-id
Created March 7, 2017 09:20
Show Gist options
  • Save geek-id/41124c9d64578418586223ab33d87762 to your computer and use it in GitHub Desktop.
Save geek-id/41124c9d64578418586223ab33d87762 to your computer and use it in GitHub Desktop.
Configuration SSH Server with python scripting
import socket, os, re, sys, fileinput
sshFile = 'sshd_config'
bannerFile = 'issue.net'
if os.path.isfile(sshFile) and os.access(sshFile, os.R_OK):
# code here
try:
with open(sshFile, 'r') as searchconfig:
for config in searchconfig:
if 'Port' in config:
old_port = config
if 'Banner' in config:
banner = config
with fileinput.FileInput(sshFile, inplace=True, backup='.bak') as conf:
new_port = input('Set Port SSH([Press Enter] to default Port 22): ')
default_port = '22'
active = input('Enable Banner SSH([Y/n])? ').lower()
banner_disable = '#Banner /etc/issue.net'
for configPort in conf:
if not new_port:
print(configPort.replace(old_port, ('Port %s\n' % default_port)), end="")
else:
print(configPort.replace(old_port, ('Port %s\n' % new_port)), end="")
for banner_enable in conf:
if active == 'y':
print(banner_enable.replace(banner, (re.sub('^\#','',banner))), end="")
# print(banner)
elif not active or active == 'n':
print(banner_enable.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