src.setup-client
1# ------------------------------------------------------------------ 2# Description: This script will install the tools necessary for 3# a user to remotely connect from their client to a 4# server using TigerVNC. Run from the client computer 5# that you plan on using. 6# 7# Usage: > python setup-client 8# > vnc-devbox 9# 10# Requirements: Ssh must already be setup between server and client. 11# Works on MacOSX clients. Not tested on any other 12# OS. 13# 14# Author: William Li 15# Date: 12-14-2021 16# ------------------------------------------------------------------ 17import os 18 19# ------------------------------------------------------------------ 20# FILL OUT PRIOR TO RUNNING THE SCRIPT! 21# ------------------------------------------------------------------ 22# setup required 23SERVER_USER = "ubuntu" # default user name 24SERVER_IP = "xxx.xx.xx.xx" # the address to the server box 25SERVER_LOCAL = "127.0.0.1" # leave this as default to use localhost 26CUSTOM_ALIAS = "vnc-devbox" # the custom alias you want for connecting 27 28# ------------------------------------------------------------------ 29# DO NOT MODIFY 30# ------------------------------------------------------------------ 31SERVER_LOGIN = f"{SERVER_USER}@{SERVER_IP}" 32HOME_FOLDER = os.path.expanduser("~") 33TIGERVNC_VER = "1.12.0" 34TIGERVNC_PORT = "5901" 35 36 37def validate(): 38 if SERVER_IP == "xxx.xx.xx.xx": 39 raise ValueError("Invalid server ip address!") 40 41 42def update_file(file: str, custom_line: str) -> None: 43 """Updates the specified file by adding the custom line. 44 45 Args: 46 file (str): the file to edit 47 custom_line (str): The custom line to add to the file 48 """ 49 50 assert file.endswith("rc"), "Error! This is not an *rc file!" 51 52 # create the file if it does not already exist 53 if ~os.path.exists(file): 54 print(f"Generating a new file:\n", f"{file}") 55 open(file, "a").close() 56 57 # print notification 58 print(f"Adding line to your shell config:\n", f"{custom_line}") 59 60 # add an alias to the file 61 new_line_added = False 62 63 with open(file, "r") as f: 64 lines = f.readlines() 65 if custom_line not in lines: 66 out = open(file, "a") 67 out.write(custom_line) 68 out.close() 69 70 new_line_added = True 71 72 if new_line_added is True: 73 print(f"New line: {alias_vnc_devbox} was added to {file}") 74 else: 75 print(f"No changes made to {file}") 76 77 78def print_intro(): 79 os.system("clear") 80 print( 81 f" ---------------------------------------------\n", 82 f"\n", 83 f"This script will install a Virtual Network \n", 84 f"Client (VCN) on your computer for connecting \n", 85 f" with remote servers. \n", 86 f"\n", 87 f"First install the TigerVNC client from here: \n", 88 f"https://github.com/TigerVNC/tigervnc/releases \n", 89 f"Place the app in ~/Applications/ \n", 90 f"\n", 91 f"You will be asked for authorization to change\n", 92 f" system settings!", 93 f"\n", 94 f"---------------------------------------------\n", 95 ) 96 97 print( 98 f" ---------------------------------------------\n", 99 f"USER SETTINGS: \n", 100 f"SERVER USER: {SERVER_USER} \n", 101 f"SERVER IP: {SERVER_IP} \n", 102 f"SERVER LOCAL: {SERVER_LOCAL} \n", 103 f"CUSTOM ALIAS: {CUSTOM_ALIAS} \n", 104 f"---------------------------------------------\n", 105 ) 106 107 108def print_summary(): 109 # print notification 110 print(f"---------------------------------------------\n") 111 print( 112 f"Run the command: \n", 113 f">>> {CUSTOM_ALIAS} \n", 114 f"This will bring up the TigerVNC viewer client...", 115 f"Enter Address: localhost:5901", 116 ), 117 print(f"---------------------------------------------\n") 118 119 120if __name__ == "__main__": 121 validate() 122 print_intro() 123 124 # await user input 125 user_input = input("Enter version of TigerVNC installed [Default==1.12.0] \n") 126 127 if len(user_input) > 0: 128 TIGERVNC_VER = user_input 129 130 # installing ttab (need to approve accessibility settings) 131 # ttab allows automation via terminal tabs: https://www.npmjs.com/package/ttab 132 # the -g option installs the package globally by adding it to your path 133 os.system("brew update") 134 os.system("brew install node") 135 os.system("npm install ttab -g") 136 os.system("ttab") 137 138 # create an alias that will be added to the shell 139 alias_vnc_devbox = f"alias {CUSTOM_ALIAS}='ssh -L {TIGERVNC_PORT}:{SERVER_LOCAL}:{TIGERVNC_PORT} {SERVER_LOGIN}'" 140 141 # define the path to the shell configurations 142 bashrc = os.path.abspath("%s/.bashrc" % HOME_FOLDER) 143 zshrc = os.path.abspath("%s/.zshrc" % HOME_FOLDER) 144 145 # update the files with custom alias 146 update_file(file=bashrc, custom_line=alias_vnc_devbox) 147 update_file(file=zshrc, custom_line=alias_vnc_devbox) 148 149 print_summary()
SERVER_USER =
'ubuntu'
SERVER_IP =
'xxx.xx.xx.xx'
SERVER_LOCAL =
'127.0.0.1'
CUSTOM_ALIAS =
'vnc-devbox'
SERVER_LOGIN =
'ubuntu@xxx.xx.xx.xx'
HOME_FOLDER =
$HOME
TIGERVNC_VER =
'1.12.0'
TIGERVNC_PORT =
'5901'
def
validate():
def
update_file(file: str, custom_line: str) -> None:
43def update_file(file: str, custom_line: str) -> None: 44 """Updates the specified file by adding the custom line. 45 46 Args: 47 file (str): the file to edit 48 custom_line (str): The custom line to add to the file 49 """ 50 51 assert file.endswith("rc"), "Error! This is not an *rc file!" 52 53 # create the file if it does not already exist 54 if ~os.path.exists(file): 55 print(f"Generating a new file:\n", f"{file}") 56 open(file, "a").close() 57 58 # print notification 59 print(f"Adding line to your shell config:\n", f"{custom_line}") 60 61 # add an alias to the file 62 new_line_added = False 63 64 with open(file, "r") as f: 65 lines = f.readlines() 66 if custom_line not in lines: 67 out = open(file, "a") 68 out.write(custom_line) 69 out.close() 70 71 new_line_added = True 72 73 if new_line_added is True: 74 print(f"New line: {alias_vnc_devbox} was added to {file}") 75 else: 76 print(f"No changes made to {file}")
Updates the specified file by adding the custom line.
Arguments:
- file (str): the file to edit
- custom_line (str): The custom line to add to the file
def
print_intro():
79def print_intro(): 80 os.system("clear") 81 print( 82 f" ---------------------------------------------\n", 83 f"\n", 84 f"This script will install a Virtual Network \n", 85 f"Client (VCN) on your computer for connecting \n", 86 f" with remote servers. \n", 87 f"\n", 88 f"First install the TigerVNC client from here: \n", 89 f"https://github.com/TigerVNC/tigervnc/releases \n", 90 f"Place the app in ~/Applications/ \n", 91 f"\n", 92 f"You will be asked for authorization to change\n", 93 f" system settings!", 94 f"\n", 95 f"---------------------------------------------\n", 96 ) 97 98 print( 99 f" ---------------------------------------------\n", 100 f"USER SETTINGS: \n", 101 f"SERVER USER: {SERVER_USER} \n", 102 f"SERVER IP: {SERVER_IP} \n", 103 f"SERVER LOCAL: {SERVER_LOCAL} \n", 104 f"CUSTOM ALIAS: {CUSTOM_ALIAS} \n", 105 f"---------------------------------------------\n", 106 )
def
print_summary():
109def print_summary(): 110 # print notification 111 print(f"---------------------------------------------\n") 112 print( 113 f"Run the command: \n", 114 f">>> {CUSTOM_ALIAS} \n", 115 f"This will bring up the TigerVNC viewer client...", 116 f"Enter Address: localhost:5901", 117 ), 118 print(f"---------------------------------------------\n")