Advertisement
Mark2020H

Python script for installing running VNC and SSH

May 23rd, 2023 (edited)
1,054
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.89 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import os
  3. import subprocess
  4. import sys
  5. import time
  6.  
  7. #  Python3 console  app to both  run , install vnc server and ssh server , start , stop and  exit
  8. #  futher introduced  was capture  the output and print  to screen I think you will love the way this done
  9. #  MD Harrington date 25-05-2023   time 10:35 AM  this took some  time to work this all out
  10. #  For the benefit of some  friends on face book and instagram
  11. #  This is how its done
  12.  
  13. class Menu:
  14.    
  15.     def __init__(self):
  16.         os.system("clear")
  17.         self.choices = {
  18.             "1": self.check_install_x11vnc_server,
  19.             "2": self.check_install_ssh_server,
  20.             "3": self.start_vnc_server,
  21.             "4": self.stop_vnc_server,
  22.             "0": self.exit_menu
  23.         }
  24.  
  25.     def typewriter_print(self, text, color=""):
  26.         for char in text:
  27.             sys.stdout.write("{}{}".format(color, char))
  28.             sys.stdout.flush()
  29.             time.sleep(0.02)
  30.         print("\033[0m")
  31.  
  32.     def display_menu(self):
  33.         self.typewriter_print("\033[1;36m------ Menu ------")
  34.         self.typewriter_print("1. \033[1;35mCheck and install X11VNC server")
  35.         self.typewriter_print("2. \033[1;34mCheck and install SSH server")
  36.         self.typewriter_print("3. \033[1;33mStart VNC server")
  37.         self.typewriter_print("4. \033[1;32mStop VNC server")
  38.         self.typewriter_print("0. \033[1;31mExit")
  39.  
  40.     def check_install_x11vnc_server(self):
  41.         self.typewriter_print("\033[1;33mChecking X11VNC server...\033[0m")
  42.         result = subprocess.run(["which", "x11vnc"],  stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
  43.         if result.returncode != 0:
  44.             self.typewriter_print("\033[1;31mX11VNC server not found. Installing...\033[0m")
  45.             os.system("sudo apt install -y x11vnc")
  46.         else:
  47.             self.typewriter_print("\033[1;32mX11VNC server is installed.\033[0m")
  48.  
  49.     def check_install_ssh_server(self):
  50.         self.typewriter_print("\033[1;33mChecking SSH server...\033[0m")
  51.         ##########################################################################################################
  52.         # Alteration  made to the line below 15:39hrs 23-05-2023  removed 3rd argument "--quiet" Now detects sshd#
  53.         ##########################################################################################################
  54.         result = subprocess.run(["systemctl", "is-active", "ssh"],  stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
  55.         if result.returncode != 0:
  56.             self.typewriter_print("\033[1;31mSSH server not found. Installing and starting...\033[0m")
  57.             os.system("sudo apt install -y openssh-server")
  58.             os.system("sudo systemctl enable ssh")
  59.             os.system("sudo systemctl start ssh")
  60.         else:
  61.             self.typewriter_print("\033[1;32mSSH server is installed and running.\033[0m")
  62.  
  63.     def start_vnc_server(self):
  64.         self.typewriter_print("\033[1;33mStarting VNC server...\033[0m")
  65.         os.system("x11vnc -auth guess -forever -rfbauth $HOME/.vnc/passwd -rfbport 5900 -shared")
  66.  
  67.     def stop_vnc_server(self):
  68.         self.typewriter_print("\033[1;33mStopping VNC server...\033[0m")
  69.         os.system("x11vnc -R stop")
  70.  
  71.     def exit_menu(self):
  72.         self.typewriter_print("\033[1;33mExiting menu...\033[0m")
  73.         time.sleep(1)
  74.         os.system("clear")
  75.         sys.exit()
  76.  
  77.     def run(self):
  78.         while True:
  79.             self.display_menu()
  80.             choice = input("Enter your choice: ")
  81.  
  82.             action = self.choices.get(choice)
  83.             if action:
  84.                 action()
  85.                 time.sleep(1)
  86.                 input("\nPress Enter to continue...")
  87.                 os.system("clear")
  88.                
  89.     def main():
  90.         menu = Menu()
  91.        
  92.         menu.run()
  93.  
  94. if __name__ == "__main__":
  95.     Menu.main()
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement