Skip to content

Instantly share code, notes, and snippets.

@jnaskali
Created June 14, 2019 10:19
Show Gist options
  • Save jnaskali/104cc00d8e9dfc63cac1d9534ebc00a2 to your computer and use it in GitHub Desktop.
Save jnaskali/104cc00d8e9dfc63cac1d9534ebc00a2 to your computer and use it in GitHub Desktop.
Script to handle SSH links from Firefox in Linux
#!/bin/sh
# Script that opens ssh:// links from Firefox - CC0 by Juhani, www.naskali.fi
# extract the protocol
proto="$(echo $1 | grep -oP '^[a-z]*')" # first letters
# extract the user (if any)
user="$(echo $1 | grep -oP '(?<=:\/\/)(.*)(?=@)')" # text in between :// and @
# extract the host
host="$(echo $1 | grep -oP '(?<=:\/\/|@)([a-z0-9\.]+)(?!.*@)')" # alphanumerals preceded by :// or @, not followed by text@
# extract the port
port="$(echo $1 | grep -oP '(?<=:)\d*')" # numbers after :
command="$proto "
if [ -n "$port" ]
then
command="${command}-p $port "
fi
if [ -n "$user" ]
then
command="${command}${user}@"
fi
command="${command}$host"
/usr/bin/gnome-terminal -q -- zsh -c "$command"
# You also need the boolean 'network.protocol-handler.expose.ssh':false in Firefox's about:config
@mwobst
Copy link

mwobst commented Mar 13, 2023

Line 9 should be
host="$(echo $1 | grep -oP '(?<=:\/\/|@)([a-z0-9\.\-]+)(?!.*@)')" […]
Its regex includes the "-" sign to allow stuff like
ssh://my-supreme-host.my-domain.de, otherwise the resulting host would be, for example, only "my".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment