From 12116ce3c52641f58aa1ed8a9abb8f1eadfd6af3 Mon Sep 17 00:00:00 2001 From: absurdo Date: Fri, 1 Sep 2023 00:52:51 +0200 Subject: [PATCH] Fixes in tasks --- scripts/system/ssh/change_ssh_port.py | 38 ++++++++++++++++++++++++--- servers/scheduler.py | 2 +- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/scripts/system/ssh/change_ssh_port.py b/scripts/system/ssh/change_ssh_port.py index 3ec6528..3ee13c3 100644 --- a/scripts/system/ssh/change_ssh_port.py +++ b/scripts/system/ssh/change_ssh_port.py @@ -5,6 +5,7 @@ import subprocess import argparse import platform import distro +import os pyv=platform.python_version_tuple() @@ -30,19 +31,50 @@ except: exit(1) +print('Changing ssh port...') + #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 os.path.isfile('/etc/sshd_config.d/port.conf'): + if subprocess.call("sudo cp /etc/sshd_config.d/port.conf /etc/sshd_config.d/port.conf.bak", shell=True) > 0: + print('Error: cannot save the old sshd configuration') + sys.exit(1) + 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 linux_distro=='fedora' or linux_distro=='centos' or linux_distro=='almalinux' or linux_distro=='rocky': + if subprocess.call("sudo getenforce", shell=True)==0: + if subprocess.call("sudo semanage port -a -t ssh_port_t -p tcp %s" % ssh_port, shell=True) > 0: + print('WARNING: probably the port is open for ssh in selinux\n') + #sys.exit(1) + +if subprocess.call("sudo systemctl status firewalld", shell=True)==0: + if subprocess.call("sudo firewall-cmd --add-port=%s/tcp --permanent && sudo firewall-cmd --reload" % ssh_port, shell=True) > 0: + print('Error, cannot set firewall services for sshd...\n') + exit(1) + if subprocess.call("sudo systemctl restart sshd", shell=True) > 0: + #Fallout to #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: cannot restart new sshd configuration!, dangerous!\n') + print('Restarting sshd with old value...') + + if os.path.isfile('/etc/sshd_config.d/port.conf.bak'): + if subprocess.call("sudo cp /etc/sshd_config.d/port.conf.bak /etc/sshd_config.d/port.conf", shell=True) > 0: + print('Error: cannot get the old sshd configuration for restaure old port!, deleting old configuration and opening port 22!\n') + sys.exit(1) + + if subprocess.call("sudo rm -f /etc/sshd_config.d/port.conf && sudo systemctl restart sshd", shell=True) > 0: + print('Error: cannot delete configuration and restart sshd!!\n') + 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"}') +print('{"error": 0, "status": 0, "progress": 100, "no_progress":0, "message": "Done: changed the ssh port value..."}') diff --git a/servers/scheduler.py b/servers/scheduler.py index 85874f5..c5a1732 100644 --- a/servers/scheduler.py +++ b/servers/scheduler.py @@ -51,7 +51,7 @@ def execute_script(task_id, executable='launcher.py'): return_value=proc.returncode - if return_value>0: + if return_value!=0: connection=WebModel.connection() if executable=='launcher.py':