loading...

SSH Shortcuts

hoelzro profile image Rob Hoelz ・1 min read

Originally posted at hoelz.ro

Let's say that you have a server you SSH into very often. Let's also say that sshd on that server is running on a non-standard port to avoid annoying scanners (we'll using 53718 in this example). To SSH into this server, you run the following:

  $ ssh -p 53718 rob@example-server.com

What a mouthful! There's got to be a way to avoid typing that much! A technique I've often seen people use is to create a shell alias for this:

  $ alias example='ssh -p 53718 rob@example-server.com' # in bash

So now all I'd have to do is run example. Piece of cake, right?

But what if you're using scp? Or sftp? Or rsync? Or git remote add? Or Vim's netrw plugin? Suddenly your simple shell alias doesn't seem so cool!

However, there is a solution to this! Enter your SSH config file.

Your SSH config file, located at ~/.ssh/config, can do a lot of cool things. To replicate our example above, we'd add this to our configuration:

Host example
  HostName example-server.com
  User rob
  Port 53718

Now all I need to do is ssh example, and voilà! It works! It's four characters longer than the alias, but the shortcut is applied to the following commands as well:

  scp example:that-important-file.txt .
  sftp example
  rsync -ar example:my-project/ .
  git remote add example ssh://example/~/my-project
  vim scp://example/that-important-file.txt

If you're interested in shortening the Git example even further, see my blog post about it.

Your SSH configuration file is very powerful; I recommend checking out man ssh_config for more options to play with!

Posted on Jul 27 '18 by:

hoelzro profile

Rob Hoelz

@hoelzro

Hi, I'm Rob! I like programming, gaming, languages, and birds. Opinions shared are my own.

Discussion

markdown guide
 

Also with wildcards

Host *.yourdomain.com
  User joe
 

Hi. I am having doubts regarding using the ssh login to check the server status with child_process module. Is that something that you can help me with? Thanks.

 

I can try; I'm not sure which child_process module you're referring to, though. Do you have some code I could look at?

 

yeah,
This is a coffescript for hubot.

code :

---> Code Starts here

child_process = require 'child_process'
module.exports = (robot) ->
robot.respond /check status/i, (res) ->

child_process.exec("sshpass -p 'password' ssh username@hostname", (err, stdout, stderr) ->
  if err
    console.log(err)
  else
    console.log(stdout)
)

       ----> code ends here

Actually the problem is i'm not sure whether the syntax for child_process is correct. I am new to coffeescript and hubot. So i don't know how to resolve this

I have no familiarity with CoffeeScript or Node's child_process module, so I don't know how much help I'll be, but I'll try! Are you getting an error message or anything?

yeah. Error : Response not OK: no_text.

I think passing the shell commands within the child_process has some issues. If you come up with any solution or something related to that, please let me know. Thanks.

 
 $ alias example='ssh example'
 $ example 'command parameters'