Git Product home page Git Product logo

vim-dadbod-ssh's Introduction

vim-dadbod-ssh

Project Status: Active - The project has reached a stable, usable state and is being actively developed.

NeoVim plugin that allows vim-dadbod connections to remote servers through ssh.

It's actually a wrapper for existing adapters. It creates ssh tunnel using ssh -L ... and then passing changed connection url to proper adapter.

It was tested with mysql, I'm not sure how well it works with other connections, if you are using it with other db please let me know.

Requirements

  • NeoVim - Plugin is using nvim's jobstart() API to create and keep tunnel, I'm sure it can be done for Vim 8+ as well. I would be grateful for PR ❤️
  • Linux or MacOS - adapter is using ssh command to connect to the remote server. It is also using following commands: netstat (Linux), lsof (MacOS), grep, awk and sed. To be more specific this command is used: netstat -tuplen 2>/dev/null | grep {localhost} | awk '{print $4}' | sed 's/.*://g' (Linux) lsof -nP -iTCP -sTCP:LISTEN 2>/dev/null | grep {localhost} | awk '{print $9}' | sed 's/.*://g' (MacOS)

Installation

Using vim-plug:

Plug 'tpope/vim-dadbod'

Plug 'pbogut/vim-dadbod-ssh'

Or use your favourite method / package manager.

Configuration

Adapter format is quite simple, here is example for mysql connection:

let g:my_db = "ssh://sshremotehost:mysql://user:password@databasehost/db_name"

Do not use DB g:my_db = "ssh://..... especially in your start-up scripts, as this command will create tunnel (which may take couple seconds) and assign modified URL to the g:my_db variable (see how it works section), which will work for some time but will fail if tunnel breaks and new one will have to be established.

As you can see normal connection URL is prepended with ssh://sshremotehost:

How to set up SSH password? Please, use public key. How to set up user and port? You can do this in your $HOME/.ssh/config:

Host mydbhost-name
  User username
  HostName 123.123.123.123
  Port 22222

With that you can use ssh://mydbhost-name: in your connection string.

To work adapter don't need any additional configuration, but there are few things one may want to adjust.

  • g:db_adapter_ssh_localhost - defaults to 127.0.0.1 Why IP and not just localhost? It is used to replace your connection host and localhost is causing issues with mysql (maybe others too?). When host is localhost mysql is trying to connect with socket instead of network.
  • g:db_adapter_ssh_timeout - defaults to 10000 (10 seconds) It's how long adapter will wait for tunnel to be established.
  • g:db_adapter_ssh_port_range - defaults to range(7000, 7100) It's range of local ports that will be used to create tunnels, you can specific different range. Script is checking if port is available before trying to create tunnel, so if some IPs in range are taken that should be fine.

So how it works?

On first connection ssh is used to create tunnel to the remote server. Then in connection URL port and host are changed to use localhost and port that was used to create tunnel. URL modified like that is then passed to the adapter.

With this URL ssh://sshremotehost:mysql://user:password@databasehost/db_name adapter will run: ssh -L 7000:databasehost:3306 -t echo ssh_connected; read. URL is modified to mysql://user:[email protected]:7000/db_name and that is passed to the mysql adapter.

read is used to keep connection alive and echo to confirm when connection is established. -N could be used instead read but then would have to find another method to confirm connection was established. If you have better ideas I accept PR's.

Contributions

Always welcome.

License

MIT License; The software is provided "as is", without warranty of any kind.

vim-dadbod-ssh's People

Contributors

jpagex avatar khoulyy avatar pbogut avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

vim-dadbod-ssh's Issues

Postgresql support

Hi, not sure if I have a configuration issue or work needs to be done to support postgresql.

Running the command:
:DB ssh://my-ssh-profile:postgres://username:pa$$word@rds_cluster_url:5432/db_name

(removed actual values from the above string)

My ssh config:

Host my-ssh-profile
  User ec2-user
  HostName port_of_bastion_host
  Port 22

Getting the following error:
CleanShot 2023-10-16 at 23 01 13@2x

Thanks!

Seems to not play well with Dadbod UI

After your response on #2 I decided to give NeoVim a try. I was able to load the plugin, but now I'm getting another error when using Dadbod UI. It seems that these plugins don't play well together. This is the error I get when trying to open the Dadbod UI.

Error detected while processing function db_ui#open[1]..<SNR>134_init[2]..15[25]..17[2]..20[17]..25[5]..db#resolve[1]..<SNR>135_canonicalize[10]..db#a
dapter#call[5]..db#adapter#ssh#canonicalize:
line    2:
E117: Unknown function: db#adapter#0#canonicalize

I was able to successfully query using the :DB command however.

2 connection to different SSH hosts.

Just a question.
I'm not really good at this, but according to information on the internet, it's possible to have 2 SSH connections to different hosts.

Right now, I have 2 databases: one on the dev host and the other on the prod host.

I can connect to them separately on the first neovim run.
However, if I open a dev connection on the first Neovim run, then I can't open a connection to the prod databases. Is there a way to fix this?

Can you give me a link to study this theme? I would be grateful.

Thanks for your plugin; it really helped me.

Error connecting SSH

Hello!

Can't use ssh connection with dadbod. If i run DBUI i receive error:
sshhost:remotehost:3306 stderr: Welcome to SSH Server 'nostromo'
Error detected while processing function db_ui#open[1]..83_init[2]..4[25]..6[5]..13[8]..14[5]..db#resolve[1]..85_canonicalize[10]..db#adapter#call[5]..db#adapter#ssh#canonicalize:
line 2:
E117: Unknown function: db#adapter#0#canonicalize

Free port detection not working on MacOS

Hello!

Thanks a lot for making vim-dadbod working with SSH!

As mentioned in the README, the open port detection is done for Linux, but not for MacOS.

For people interesting to use it on MacOS, I could make it work by changing the call to netstat with lsof. See the diff here: 3e614a6.

function s:get_free_port()
-  let ports = systemlist("netstat -tuplen 2>/dev/null | grep " . s:localhost
-      \                . " | awk '{print $4}' | sed 's/.*://g'")
+  let ports = systemlist("lsof -nP -iTCP -sTCP:LISTEN | grep " . s:localhost
+      \                . " | awk '{print $9}' | sed 's/.*://g'")

Hope this can help someone.

Maybe we could handle both by detecting the OS?

Unknown Function: wait

I'm getting the following error when attempting to try to run this plugin:

Error detected while processing function db#execute_command[130]..function db#execute_command[30]..db#connect[1]..db#resolve[1]..136_canonicalize
[10]..db#adapter#call[5]..db#adapter#ssh#canonicalize[1]..138_get_tunneled_url:
line 51:
E117: Unknown function: wait

It seems like it can't find the wait function. I tried to see if it was the way my VIM was compiled, but no luck.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.