diff --git a/extras/pandora_gotty/build_deb.sh b/extras/pandora_gotty/build_deb.sh index 483ab35076..fbcb2a6790 100755 --- a/extras/pandora_gotty/build_deb.sh +++ b/extras/pandora_gotty/build_deb.sh @@ -6,6 +6,8 @@ mkdir -p pandora_gotty/usr/bin mkdir -p pandora_gotty/etc/pandora_gotty cp -a ../src/pandora_gotty pandora_gotty/usr/bin cp -a ../src/pandora_gotty.conf pandora_gotty/etc/pandora_gotty +curl -SsL --output pandora_gotty/usr/bin/pandora_gotty_exec http://192.168.50.31/installers/installers/Linux/x86_64/pandora_gotty_exec +chmod +x pandora_gotty/usr/bin/pandora_gotty_exec dpkg-deb --build pandora_gotty mv pandora_gotty.deb ../ rm -rf pandora_gotty/usr/ diff --git a/extras/pandora_gotty/build_rpm.sh b/extras/pandora_gotty/build_rpm.sh index 64a43fa099..e08fa0e65b 100755 --- a/extras/pandora_gotty/build_rpm.sh +++ b/extras/pandora_gotty/build_rpm.sh @@ -2,7 +2,10 @@ #RPM VERSION=$(grep '%define version' pandora_gotty.spec | awk '{print $3}') mkdir -p pandora_gotty-${VERSION} -cp src/* pandora_gotty-${VERSION}/ +cp src/pandora_gotty pandora_gotty-${VERSION}/ +cp src/pandora_gotty.conf pandora_gotty-${VERSION}/ +curl -SsL --output pandora_gotty-${VERSION}/pandora_gotty_exec http://192.168.50.31/installers/installers/Linux/x86_64/pandora_gotty_exec +chmod +x pandora_gotty-${VERSION}/pandora_gotty_exec tar -cvzf pandora_gotty-${VERSION}.tar.gz pandora_gotty-${VERSION}/* mv pandora_gotty-${VERSION}.tar.gz ${HOME}/rpmbuild/SOURCES/ rm -rf ${HOME}/rpmbuild/RPMS/x86_64/pandora_gotty* diff --git a/extras/pandora_gotty/pandora_gotty.spec b/extras/pandora_gotty/pandora_gotty.spec index 005dfd945f..813c9726c9 100644 --- a/extras/pandora_gotty/pandora_gotty.spec +++ b/extras/pandora_gotty/pandora_gotty.spec @@ -25,6 +25,7 @@ rm -rf $RPM_BUILD_ROOT mkdir -p $RPM_BUILD_ROOT/%{_bindir} mkdir -p %{buildroot}/etc/pandora_gotty/ cp %{name} $RPM_BUILD_ROOT/%{_bindir} +cp pandora_gotty_exec $RPM_BUILD_ROOT/%{_bindir} cp pandora_gotty.conf %{buildroot}/etc/pandora_gotty/ %clean rm -Rf $RPM_BUILD_ROOT @@ -33,6 +34,7 @@ rm -Rf $RPM_BUILD_ROOT %defattr(-,root,root,-) %config(noreplace) /etc/pandora_gotty/pandora_gotty.conf %{_bindir}/%{name} +%{_bindir}/pandora_gotty_exec %changelog * Mon Sep 18 2023 PandoraFMS - 1.0-1 diff --git a/extras/pandora_gotty/src/.gitignore b/extras/pandora_gotty/src/.gitignore index 423058ca29..a2acfc48bf 100644 --- a/extras/pandora_gotty/src/.gitignore +++ b/extras/pandora_gotty/src/.gitignore @@ -1 +1,2 @@ -*rpm \ No newline at end of file +*rpm +bin/* diff --git a/extras/pandora_gotty/src/pandora_gotty.conf b/extras/pandora_gotty/src/pandora_gotty.conf index 01a037dd72..09d54e64c0 100644 --- a/extras/pandora_gotty/src/pandora_gotty.conf +++ b/extras/pandora_gotty/src/pandora_gotty.conf @@ -1,15 +1,8 @@ -//Pandora Gotty +//Pandora Gotty config file // [bool] Permit clients to write to the TTY permit_write = true -// [bool] Enable basic authentication -enable_basic_auth = true - -// [string] Default username and password of basic authentication (user:pass) -// To enable basic authentication, set `true` to `enable_basic_auth` -credential = "pandora:Pandor4!" - // [bool] Permit clients to send command line arguments in URL (e.g. http://example.com:8080/?arg=AAA&arg=BBB) permit_arguments = true diff --git a/extras/pandora_gotty/src/pandora_gotty_exec.py b/extras/pandora_gotty/src/pandora_gotty_exec.py new file mode 100644 index 0000000000..1f7756745f --- /dev/null +++ b/extras/pandora_gotty/src/pandora_gotty_exec.py @@ -0,0 +1,110 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +__author__ = "PandoraFMS Team" +__copyright__ = "Copyright 2023, PandoraFMS" +#__credits__ = ["Rob Knight", "Peter Maxwell", "Gavin Huttley", "Matthew Wakefield"] +__maintainer__ = "Projects/QA department" +__status__ = "Prod" +__version__ = "1.0" + +import sys, argparse, signal, re, datetime, subprocess + +info= f""" +SSH and TELNET helper for pandora_gotty. +Version: {__version__} +""" + +parser = argparse.ArgumentParser(description= info, formatter_class=argparse.RawTextHelpFormatter) +parser.add_argument('exec_cmd', + help='Aplication to be executed, avalibles: ssh or telnet',type=str, choices=['ssh', 'telnet']) +parser.add_argument('address', + help='IP addres or dns name to connect', type=str, default="") +parser.add_argument('port', + help='Port to connect', type=int, default=23) +parser.add_argument('user', + help='Username, only requiered for ssh connection', type=str, default="", nargs='?') + +args = parser.parse_args() + +# Define a function to handle the SIGINT signal +def sigint_handler(signal, frame): + print ('\nInterrupted by user', file=sys.stderr) + sys.exit(0) +signal.signal(signal.SIGINT, sigint_handler) + +# Define a function to handle the SIGTERM signal +def sigterm_handler(signum, frame): + print("Received SIGTERM signal.", file=sys.stderr) + sys.exit(0) +signal.signal(signal.SIGTERM, sigterm_handler) + +# Functions +def is_valid_add(add:str): + # Regular expression to match an IP address + ip_pattern = r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$' + + # Regular expression to match a DNS name (domain name) + dns_pattern = r'^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$' + + if re.match(ip_pattern, add) or re.match(dns_pattern, add): + return True + else: + print(f"Error not valid address: {add}", file=sys.stderr) + return False + +def is_valid_username(username:str): + # Regular expression to match a valid Linux username + username_pattern = r'^[a-zA-Z_][a-zA-Z0-9_]{0,31}$' + if re.match(username_pattern, username) is not None: + return True + else: + print(f"Error not valid username: {username}", file=sys.stderr) + return False + +def exec_ssh (user:str, add:str, port:int): + # Previus checks + if is_valid_username(user) == False: + return False + if is_valid_add(add) == False: + return False + if port == 0 : + return False + + try: + print("> Starting SSH connection...") + ssh_command = f"ssh {user}@{add} -p {port}" + subprocess.run(ssh_command, shell=True) + + except subprocess.CalledProcessError as e: + raise SystemExit(e) + return True + +def exec_telnet (add:str, port:int): + # Previus checks + if is_valid_add(add) == False: + return False + + try: + print("> Starting Telnet connection...") + ssh_command = f"telnet -E {add} {port}" + subprocess.run(ssh_command, shell=True) + + except subprocess.CalledProcessError as e: + raise SystemExit(e) + return True + + +# Main +if __name__ == "__main__": + if args.exec_cmd == "ssh": + exec_ssh(args.user, args.address, args.port) + print ("> ssh session finished") + sys.exit(0) + + if args.exec_cmd == "telnet": + exec_telnet(args.address, args.port) + print ("> telnet session finished") + sys.exit(0) + + sys.exit(0)