Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Another handy trick: put this script on your path somewhere, and name it "ssh-argv0":

    #!/bin/sh
    exec ssh "${0##*/}" "$@"
Then create symlinks to ssh-argv0 for common hostnames you ssh to, shortened using host aliases as suggested in the article. You now have a command for each host, which you can use as a prefix like sudo to run a single command on that host. For instance, "myth sudo reboot".

If you're used to using "ssh user@box foo", and not always for the same user (in which case you could use "User" in .ssh/config), you can do the same thing via "box -l user foo"



Huh. ssh used to offer this feature natively -- just symlink any hostname to the ssh binary -- but it turns out that they removed this about ten years ago.

(I feel old...)


A feature which was copied from rlogin and rsh, I seem to remember.

Me too.


${0##*/} ... are you looking for the basename(1) command? Posix requires support for this usage, but it does not work with many traditional shells, e.g., Solaris 10 /bin/sh.

Here's how to work around it:

    #!/bin/sh
    exec ssh "$(basename "$0")" "$@"
FWIW it's a little easier to read, too.


Sun was so into keeping backward compatibility intact they would not update the default /bin/sh because of some compatibility issues that could affect customers. So they included the newer XPG4 sh in a different location (it is a POSIX-compliant ksh88): /usr/xpg4/bin/sh. So you can write:

  #!/usr/bin/env sh
So that users who have setup PATH=/usr/xpg4/bin:$PATH get the correct shell and everything works fine.


No, I'm not looking to fork and exec a second program, and short of busybox most shells don't have basename built in.

Common idiom, works in POSIX /bin/sh, bash, dash, ash, and every other sh I've encountered. I make a point of avoiding bashisms in /bin/sh scripts, but I have no problem counting on POSIX sh. :)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: