Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Better hostname completion UNIX
The default global completions file that comes with Mac OS X uses /etc/hosts to get a list of hostnames. This isn't terribly useful since that file generally never contains anything useful (and there is a comment in the completions file suggesting that it be replaced with something smarter).

Create a file in /usr/local/share/ called common_hosts (make sure it is world readable - 'chmod 644'). Inside this file, list common hostnames (on seperate lines) that you commonly have to type out.

Now edit line line 83 of the global completions file (/usr/share/init/tcsh/completions) so that it refers to the new /usr/local/share/common_hosts file instead of /etc/hosts. i.e.: alias list_all_hostnames 'grep -v "^#" /usr/local/share/common_hosts'

Say your common_hosts file contains the hostname:
dhcp-user.192.168.1.1.somehost.edu

If you need to ssh or that host you could just type:
ssh dhcp[tab]

And the shell will expand the hostname.
    •    
  • Currently 0.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (0 votes cast)
 
[4,901 views]  

Better hostname completion | 6 comments | Create New Account
Click here to return to the 'Better hostname completion' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Better still...
Authored by: serversurfer on Nov 28, '01 12:53:47AM
If you list your servers as machines in NetInfo, (which is where we do that sort of thing in X) then you can just change your /usr/share/init/tcsh/completions file to read:
# Override this with something smarter if you wish
alias list_all_hostnames 'nidump hosts .'

This prevents you from having to to edit a hosts file. (BTW, /etc/hosts would have worked just fine. The "smarter" thing to do is use nidump.) Also, any changes made to NetInfo (THE place to be in 2001!) are automatically incorporated in to your ssh completions.
The reason you want to avoid using /etc/hosts (which is where most *NIX systems define server aliases) Is because OSX uses NetInfo to keep track of that sort of thing (it's more network friendly) and does not read /etc/hosts by default. nidump reads the NetInfo server aliases and dumps them in the same format as /etc/hosts, so we can use it to define our server completions.
Post if you need help defining new machines in NetInfo Manager, but basically, they will look like the 'localhost' entry, minus the 'serves' parameter (which I think is used to ID NetInfo domains).

[ Reply to This | # ]
reeeealy short
Authored by: trevden on Nov 28, '01 04:09:53AM

I like one-character commands. In my tcshrc are things like

alias b bbedit
alias p "ssh 10.0.1.52" #powerbook
alias t "ssh 10.0.1.51" #tower



[ Reply to This | # ]
More than ssh
Authored by: Cadre on Nov 28, '01 12:30:16PM

Those are fine if you aren't doing much.

The real utility of the hostname completion is evident if one has to work with a large amount of hostnames. e.g.: My completions file currently contains 30 hosts of the servers that I work on regularly.

ssh was merely an example. Many other commands use hostnames and are setup in the completions file to expand hostnames (finger, ping, traceroute, ftp, ...)



[ Reply to This | # ]
How much more than ssh?
Authored by: serversurfer on Nov 28, '01 11:10:07PM

Could you post a completion for scp by any chance? Pretty please? I'm too lazy/busy to figure out how they work. I'll take one of the list_all_hostnames flavor, please. ;)
If you don't have one handy, don't worry about it.



[ Reply to This | # ]
This much more than ssh!
Authored by: Cadre on Nov 29, '01 03:11:02PM

Wow! Trying to find a pattern that matches for both styles of scp was a pain in the ass (the first style being transfering a file to a server the second being receiving a file from a server). I eventually found one that partially worked and modified it to work better.

I recommend only using the first completion. The second two completions, well, they are for when you want to show off. :-)


# Simple by Aaron Linville <aaron@linville.org>
complete {scp,rcp} 'C@[./]*@f@' 'n/*/`list_all_hostnames`/:'

# From Michael Schroeder <mlschroe@immd4.informatik.uni-erlangen.de>
# This one will rsh to the file to fetch the list of files!
#complete rcp 'c%*@*:%`set q=$:-0;set q="$q:s/@/ /";set q="$q:s/:/ /";set q=($q " ");rsh $q[2] -l $q[1] ls -dp $q[3]*`%' 'c%*:%`set q=$:-0;set q="$q:s/:/ /";set q=($q " ");rsh $q[1] ls -dp $q[2]*`%' 'c%*@%`list_all_hostnames`%:' 'C@[./$~]*@f@' 'n/*/`list_all_hostnames`/:'
#complete scp 'c%*@*:%`set q=$:-0;set q="$q:s/@/ /";set q="$q:s/:/ /";set q=($q " ");ssh $q[2] -l $q[1] ls -dp $q[3]*`%' 'c%*:%`set q=$:-0;set q="$q:s/:/ /";set q=($q " ");ssh $q[1] ls -dp $q[2]*`%' 'c%*@%`list_all_hostnames`%:' 'C@[./$~]*@f@' 'n/*/`list_all_hostnames`/:'


Now a couple notes on typing the correct patterns to match mine. When transfering files, remember to put dot slash at the beginning of the files to be transfered (yes, it does work with a list of files!!!). When sending files, if you would like completion when specifying the destination directory or file, you need to remember the dot slash at the beginning of the path also.

One last thing, you can't specify a username for a host (this breaks the pattern matching). Of course, you really shouldn't be specifying a username on the commandline. Specify the username in your ./.ssh/config file. i.e.:

Host *.nowhere.net
User linvilaw

So following are example commands that could have been generated with completion:
linville:home %scp ./source.c somemachine.nowhere.net:.
linville:home %scp somemachine.nowhere.net:.source.c ./source.c



[ Reply to This | # ]
I fixed the username!
Authored by: serversurfer on Nov 29, '01 05:51:34PM
A little playing around with Cadre's complete yielded:
complete {scp,rcp} 'C@[./]*@f@' 'c/*@/`list_all_hostnames`/:' 'n/*/`list_all_hostnames`/:'

This will still allow you to tab-complete hostnames after specifying a username.
scp root@ot[TAB]

becomes
scp root@otherhost:

I wanted this because sometimes I do work on other boxen as root, but I agree with Cadre's assertion that you should specify your normal username for the other box in ~/.ssh/config.

[ Reply to This | # ]