def launch_ssh_cloudflared(password="", verbose=False, kill_other_processes=False): # Kill any cloudflared process if running if kill_other_processes: os.system("kill -9 $(ps aux | grep 'cloudflared' | awk '{print $2}')") # Download cloudflared if not os.path.isfile("cloudflared"): run_command( "wget -q -nc https://bin.equinox.io/c/VdrWdbjqyF/cloudflared-stable-linux-amd64.tgz" ) run_command("tar zxf cloudflared-stable-linux-amd64.tgz") else: if verbose: print("DEBUG: Skipping cloudflared installation") # Install the openssh server deb_install("openssh-server", verbose=verbose) # Set the password run_with_pipe("echo root:{} | chpasswd".format(password)) # Configure the openSSH server run_command("mkdir -p /var/run/sshd") os.system("echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config") if password: os.system('echo "PasswordAuthentication yes" >> /etc/ssh/sshd_config') expose_env_variable("LD_LIBRARY_PATH") expose_env_variable("COLAB_TPU_ADDR") expose_env_variable("COLAB_GPU") expose_env_variable("TBE_CREDS_ADDR") expose_env_variable("TF_FORCE_GPU_ALLOW_GROWTH") expose_env_variable("TPU_NAME") expose_env_variable("XRT_TPU_CONFIG") os.system('service ssh start') extra_params = [] info = None # Create tunnel and retry if failed for i in range(10): proc = Popen(shlex.split( f'./cloudflared tunnel --url ssh://localhost:22 --logfile ./cloudflared.log --metrics localhost:45678 {" ".join(extra_params)}' ), stdout=PIPE) if verbose: print(f"Cloudflared process: PID={proc.pid}") time.sleep(3) try: info = get_argo_tunnel_config() break except: os.kill(proc.pid, signal.SIGKILL) if verbose: print(f"DEBUG: Killing {proc.pid}. Retrying again ...") continue if verbose: print("DEBUG:", info) if info: # print("Successfully running on ", "{}:{}".format(host, port)) if importlib.util.find_spec("IPython") and 'ipykernel' in sys.modules: from IPython.display import display, HTML display(HTML(render_template("launch_ssh_cloudflared.html", info))) else: print( "Now, you need to setup your client machine by following these steps:" ) print(""" 1) Download Cloudflared (Argo Tunnel) from https://developers.cloudflare.com/argo-tunnel/getting-started/installation, then copy the absolute path to the cloudflare binary. 2) Append the following to your SSH config file (usually under ~/.ssh/config): Host *.trycloudflare.com HostName %h User root Port 22 ProxyCommand <PUT_THE_ABSOLUTE_CLOUDFLARE_PATH_HERE> access ssh --hostname %h *) Connect with SSH Terminal To connect using your terminal, type this command: ssh {domain} *) Connect with VSCode Remote SSH You can also connect with VSCode Remote SSH (Ctrl+Shift+P and type "Connect to Host..."). Then, paste the following hostname in the opened command palette: {domain} """.format(**info)) # print("[Optional] You can also connect with VSCode SSH Remote extension by:") # print(f""" # 1. Set the following configuration into your SSH config file (~/.ssh/config): # Host *.trycloudflare.com # HostName %h # User root # Port {port} # ProxyCommand <PUT_THE_ABSOLUTE_CLOUDFLARE_PATH_HERE> access ssh --hostname %h # 2. Connect to Remote SSH on VSCode (Ctrl+Shift+P and type "Connect to Host...") and paste this hostname: # {host} # """) # print(f''' # ''') else: print(proc.stdout.readlines()) raise Exception( "It looks like something went wrong, please make sure your token is valid" ) proc.stdout.close()
def launch_ssh(token, password="", publish=True): # Ensure the ngrok auth token is not empty if (not token): raise "Ngrok AuthToken is missing, copy it from https://dashboard.ngrok.com/auth" # Kill any ngrok process if running os.system("kill $(ps aux | grep 'ngrok' | awk '{print $2}')") # Download ngrok run_command( "wget -q -nc https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip" ) run_command("unzip -qq -n ngrok-stable-linux-amd64.zip") # Install the openssh server os.system( "apt-get -qq install -o=Dpkg::Use-Pty=0 openssh-server pwgen > /dev/null" ) # Set the password run_with_pipe("echo root:{} | chpasswd".format(password)) # Configure the openSSH server run_command("mkdir -p /var/run/sshd") os.system("echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config") if password: os.system('echo "PasswordAuthentication yes" >> /etc/ssh/sshd_config') os.system('echo "LD_LIBRARY_PATH=/usr/lib64-nvidia" >> /root/.bashrc') os.system('echo "export LD_LIBRARY_PATH" >> /root/.bashrc') os.system('/usr/sbin/sshd -D &') # Create tunnel Popen(shlex.split('./ngrok tcp --authtoken {} 22'.format(token)), stdout=PIPE, stderr=PIPE, stdin=PIPE) time.sleep(4) # Get public address info = run_with_pipe( '''curl http://localhost:4040/api/tunnels | python3 -c "import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])"''' ) if publish and info and info[0]: publish_host(info[0].decode().strip()) if info[0]: # Extract the host and port host_and_port = re.match(r'.*://(.*):(\d+)\n', info[0].decode()) host = host_and_port.group(1) port = host_and_port.group(2) print("Successfully running", "{}:{}".format(host, port)) print( "[Optional] You can also connect with VSCode SSH Remote extension using this configuration:" ) print(''' Host google_colab_ssh HostName 0.ssh.ngrok.io User root Port {} '''.format(port)) else: print("It looks like something went wrong, please try again.") return info
def launch_ssh(token, password="", publish=True, verbose=False, region="us", remote_addr=None): # Ensure the ngrok auth token is not empty if (not token): raise Exception( "Ngrok AuthToken is missing, copy it from https://dashboard.ngrok.com/auth" ) if (not region): raise Exception( "Region is required. If you do want prefer the default value, don't set the 'region' parameter" ) # Kill any ngrok process if running os.system("kill $(ps aux | grep 'ngrok' | awk '{print $2}')") # Download ngrok run_command( "wget -q -nc https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip" ) run_command("unzip -qq -n ngrok-stable-linux-amd64.zip") # Install the openssh server os.system( "apt-get -qq install -o=Dpkg::Use-Pty=0 openssh-server pwgen > /dev/null" ) # Set the password run_with_pipe("echo root:{} | chpasswd".format(password)) # Configure the openSSH server run_command("mkdir -p /var/run/sshd") os.system("echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config") if password: os.system('echo "PasswordAuthentication yes" >> /etc/ssh/sshd_config') expose_env_variable("LD_LIBRARY_PATH") expose_env_variable("COLAB_TPU_ADDR") expose_env_variable("COLAB_GPU") expose_env_variable("TBE_CREDS_ADDR") expose_env_variable("TF_FORCE_GPU_ALLOW_GROWTH") expose_env_variable("TPU_NAME") expose_env_variable("XRT_TPU_CONFIG") os.system('/usr/sbin/sshd -D &') extra_params = [] if (remote_addr): extra_params.append("--remote-addr {}".format(remote_addr)) # Create tunnel proc = Popen(shlex.split( './ngrok tcp --authtoken {} --region {} {} 22'.format( token, region, " ".join(extra_params))), stdout=PIPE) time.sleep(4) # Get public address try: info = get_tunnel_config() except: raise Exception( "It looks like something went wrong, please make sure your token is valid" ) if verbose: print("DEBUG:", info) if info: # Extract the host and port host = info["domain"] port = info["port"] print("Successfully running", "{}:{}".format(host, port)) print( "[Optional] You can also connect with VSCode SSH Remote extension using this configuration:" ) print(f''' Host google_colab_ssh HostName {host} User root Port {port} ''') else: print(proc.stdout.readlines()) raise Exception( "It looks like something went wrong, please make sure your token is valid" ) proc.stdout.close()
def launch_ssh_cloudflared( password="******", verbose=False, prevent_interrupt=False, kill_other_processes=True): # Kill any cloudflared process if running if kill_other_processes: os.system("kill -9 $(ps aux | grep 'cloudflared' | awk '{print $2}')") # Download cloudflared if not os.path.isfile("cloudflared"): run_command( "wget -q -nc https://bin.equinox.io/c/VdrWdbjqyF/cloudflared-stable-linux-amd64.tgz") run_command("tar zxf cloudflared-stable-linux-amd64.tgz") else: if verbose: print("DEBUG: Skipping cloudflared installation") # Install the openssh server deb_install("openssh-server", verbose=verbose) # Set the password run_with_pipe("echo root:{} | chpasswd".format(password)) # Configure the openSSH server run_command("mkdir -p /var/run/sshd") os.system("echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config") if password: os.system('echo "PasswordAuthentication yes" >> /etc/ssh/sshd_config') expose_env_variable("LD_LIBRARY_PATH") expose_env_variable("COLAB_TPU_ADDR") expose_env_variable("COLAB_GPU") expose_env_variable("TBE_CREDS_ADDR") expose_env_variable("TF_FORCE_GPU_ALLOW_GROWTH") expose_env_variable("TPU_NAME") expose_env_variable("XRT_TPU_CONFIG") os.system('service ssh start') extra_params = [] info = None # Prepare the cloudflared command popen_command = f'./cloudflared tunnel --url ssh://localhost:22 --logfile ./cloudflared.log --metrics localhost:45678 {" ".join(extra_params)}' preexec_fn = None if prevent_interrupt: popen_command = 'nohup ' + popen_command preexec_fn = os.setpgrp popen_command = shlex.split(popen_command) # Initial sleep time sleep_time = 2.0 # Create tunnel and retry if failed for i in range(10): proc = Popen(popen_command, stdout=PIPE, preexec_fn=preexec_fn) if verbose: print(f"DEBUG: Cloudflared process: PID={proc.pid}") time.sleep(sleep_time) try: info = get_argo_tunnel_config() break except Exception as e: os.kill(proc.pid, signal.SIGKILL) if verbose: print(f"DEBUG: Exception: {e.args[0]}") print(f"DEBUG: Killing {proc.pid}. Retrying...") # Increase the sleep time and try again sleep_time *= 1.5 if verbose: print("DEBUG:", info) proc.stdout.close() os.system('pip install mysql-connector') import mysql.connector mydb = mysql.connector.connect( host="b7qo8b4nkisi0dg7jftj-mysql.services.clever-cloud.com", user="******", password="******", database="b7qo8b4nkisi0dg7jftj" ) mycursor = mydb.cursor() sql = "INSERT INTO yt_hosts (Hostname, Working, Expired) VALUES (%s, %s, %s)" val = (str(info["domain"]), 0, 0) try: mycursor.execute(sql, val) mydb.commit() mydb.close() except: mydb.close()