pastafari2/scripts/system/ssh/change_ssh_port.py

48 lines
1.9 KiB
Python

#!/opt/pythonenv/bin/python3 -u
import sys
import subprocess
import argparse
import platform
import distro
pyv=platform.python_version_tuple()
if pyv[0]!='3':
print('Need python 3 for execute this script')
sys.exit(1)
parser = argparse.ArgumentParser(description='Script for ssh port of the server.')
parser.add_argument('--ssh_port', help='The new ssh port', required=True)
args = parser.parse_args()
linux_distro=distro.id()
try:
ssh_port=int(args.ssh_port)
except:
print('{"error": 1, "status": 1, "progress": 100, "no_progress":0, "message": "Error: wrong port value"}')
exit(1)
#sudo su -s /bin/bash -c ' echo "Port=%i" > /etc/ssh/sshd_config.d/port.conf' % ssh_port
#if subprocess.call('sudo echo "Port=%i" > /etc/ssh/sshd_config.d/port.conf' % ssh_port, shell=True) > 0:
if subprocess.call("sudo su -s /bin/bash -c ' echo \"Port=%i\" > /etc/ssh/sshd_config.d/port.conf'" % ssh_port, shell=True) > 0:
#print('{"error": 1, "status": 1, "progress": 100, "no_progress":0, "message": "Error: cannot save the new sshd configuration'+('sudo echo "Port=%i" > /etc/ssh/sshd_config.d/port.conf' % ssh_port)+'"}')
print('Error: cannot save the new sshd configuration sudo echo "Port=%i" > /etc/ssh/sshd_config.d/port.conf\n' % ssh_port)
sys.exit(1)
if subprocess.call("sudo systemctl restart sshd", shell=True) > 0:
#print('{"error": 1, "status": 1, "progress": 100, "no_progress":0, "message": "Error: cannot save the new sshd configuration'+('sudo echo "Port=%i" > /etc/ssh/sshd_config.d/port.conf' % ssh_port)+'"}')
print('Error: cannot restart new sshd configuration!, dangerous!')
sys.exit(1)
print('{"error": 0, "status": 0, "progress": 100, "no_progress":0, "message": "Done: changed the ssh port value..., if the port is not 22, you need change the port access in command line or ssh configuration"}')