Skip to content

Instantly share code, notes, and snippets.

@theriverman
Last active July 7, 2019 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theriverman/a436b2b2052f4fec771ed9fa1e9173a7 to your computer and use it in GitHub Desktop.
Save theriverman/a436b2b2052f4fec771ed9fa1e9173a7 to your computer and use it in GitHub Desktop.
Full Stack Development with VSCode with Remote-SSH

Full Stack Development with VSCode with Remote-SSH

Motivation

With the latest version of Visual Studio Code it's now possible to remotely connect to a Linux host over SSH and gain a full access to it. One can access all files and access the shell within the IDE just like the user were working directly on the remote HOST.

Users can conveniently forward local ports to the remote Host, that is, services running on the remote host can be accessed using the local computer.

In case the user is a full-stack developer, for example, developing with Angular 2/x and Django, the user is probably running the following commands:

  • ng serve (Listens on localhost:4200)
  • python manage.py runserver (Listens on localhost:8000)

Occasionally when the user gets disconnected from the remote host (SSH is pretty sensitive stuff) these processes tend to survive the disconnect and they keep running. However, after reconnecting via SSH again, the user won't be able to easly get back to that shell. The easiest way is killing those two processes and execute them again.

The following files in this GIST automate this process:

  • /opt/on_logon.sh Discovers the PID of processes listening on port [4200, 8000] and kills them.
  • /etc/pam.d/sshd executes on_logon.sh on each new SSH connection, but not on each new shell creation.

Installation

  1. Create a new file called on_logon.sh in your filesystem, for example, in /opt.
    Paste the contents (see later in this gist) and make the file executable chmod +x /opt/on_logon.sh
  2. Add a new line to the end of /etc/pam.d/sshd:
    session optional pam_exec.so seteuid /opt/on_logon.sh
  3. Done
#!/usr/bin/env bash
echo "Killing existing PIDs of Angular [ng serve] and Django [manage.py runserver] ..."
DJANGO=$(lsof -t -i :8000 -s TCP:LISTEN)
ANGULAR=$(lsof -t -i :4200 -s TCP:LISTEN)
kill -9 "$DJANGO" > /dev/null 2>&1 || echo "Django is not running. Skipping..."
kill -9 "$ANGULAR" > /dev/null 2>&1 || echo "Angular is not running. Skipping..."
session optional pam_exec.so seteuid /opt/on_logon.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment