{{tag>desktop bash scripts}}
====== Create custom URI/URL handlers ======
If you want to be able to open URIs like ssh://user@host from Firefox in Terminal application.
===== Creating the Handlers =====
Make a .desktop file in ''~/.local/share/applications''. Example ssh-handler.desktop:
[Desktop Entry]
Type=Application
Name=SSH Handler
Exec=ssh-handler.sh %u
Icon=utilities-terminal
StartupNotify=false
MimeType=x-scheme-handler/ssh;
===== Create handler script itself =====
#!/bin/bash
# Debug option
set -o xtrace
(
echo "Parsing URI..."
# If there is an @ sign in uri parse it, uri is in the form of ssh://user@host or ssh://user@host:port
if [[ "${1}" =~ "@" ]];then
echo "In if statement"
user=$(echo ${1} | awk -F// '{ print $2 }' - | awk -F@ '{ print $1 }' -)
host=$(echo ${1} | awk -F// '{ print $2 }' - | awk -F@ '{ print $2 }' -| awk -F: '{ print $1 }' -)
host=${host%/} # Remove trailing slash (seems to be passed in URI)
port=$(echo ${1} | awk -F: '{ print $3 }' -)
[ -z "$port" ] && port="22"
gnome-terminal -- ssh $user@$host -p $port
# terminator -e "ssh $user@$host -p $port"
# else $1 is in the form of ssh://host which uses our .ssh/config aliases
else
echo "In else"
host=$(echo ${1} | awk -F// '{ print $2 }')
host=${host%/} # Remove trailing slash (seems to be passed in URI)
gnome-terminal -- ssh $user@$host -p $port
# terminator -e "ssh $host"
fi
) >> /tmp/ssh-handler 2>&1
Save it in ''~/.local/bin'', make it executable. Add this to you PATH bash variable.
===== Registering Custom URI Handler =====
Once this file is created, the last step is to tell the system that the SSH links should be handled by default by this desktop entry. Before we do that, it can be a good idea to check that nothing is currently handling the SSH protocol, or if something is handling it make sure that we can replace it safely. To check that, run the following command:
xdg-mime query default x-scheme-handler/ssh
If the output is blank, we’re good ! It not, use your best judgement to decide if you want to replace it or not.
Now we can run the following command to define our ssh-handler.desktop file as the default handler for ssh protocol:
xdg-mime default ssh-handler.desktop x-scheme-handler/ssh
And that’s it ! Clicking on ssh://... links in your browser should now directly open a terminal window and connect to the specified host.
===== GVFS Over-Rides =====
The gvfs system may take precident over what you have specified here, specifically the ssh:// handler is aliased to sftp://. Looking in the directory: ''/usr/share/gvfs/mounts/'' we can find the handlers. Edit sftp.desktop and remove the SchemeAlias, like the following example.
[Mount]
Type=sftp
Exec=/usr/libexec/gvfsd-sftp
AutoMount=false
Scheme=sftp
# SchemeAliases=ssh
DefaultPort=22
HostnameIsInetAddress=true
====== Tested on ======
* Xubuntu 20.04.1
====== See also ======
====== References ======
* https://edoceo.com/sys/xfce-custom-uri-handler
* https://web.archive.org/web/20170923120108/http://nknu.net/ubuntu-14-04-how-to-open-ssh-links-in-a-terminal/