5 ms·
BASH autocomplete for SSH
- jack83 15y agoyou can also use ctrl+R
- daniellockard 15y agoYou'd be surprised how little some people know about bash ctrl commands.
- agumonkey 15y agogeneralize to gnu/linux tools.
- xenomachina 15y agogeneralize to things they use every day
- dredmorbius 15y agogeneralize to things
- snprbob86 15y agoHonestly, simply reading `man bash` from start to finish was one of the best things I ever did. I started doing it with all sorts of stuff. May I suggest you start with `man man`? Then someone told me about `apropos` which is summarized in it's man page as "search the whatis database for strings". Sooo useful. Not nearly as useful as simply reading man pages from start to finish, but I've only got so much spare time.
- dmhouse 15y agoI think `man readline' is more appropriate for the keyboard shortcuts, since it is the readline library that provides many of them.
- agumonkey 15y agoReading `man` is the kind of thing you understand after years of hitting hard walls. Even now I still despise it even though I know most of the answers are in it. I think I want something less linear to search into, oh and I'm surely a lazy P*S too.
- 6ren 15y agoI bind this to up-arrow in my .inputrc: "\e[A": history-search-backward "\e[B": history-search-forward OP's method would be helpful when the line differs syntactically prior to the hostname e.g. different options, arguments, a pipe into it. Though that case is rare in my own usage.
- 6ren 15y agoEDIT [missed the edit window] whoops, I see ctrl-r is "reverse-search-history" (not "history-search-backward"), it matches your input to any part of the history (so the prefix issue doesn't arise). So you could just type the hostname, and it will pick out the most recent line in the history that matches - it needn't be at the beginning of the line. You can hit ctrl-r again to go to a matching line further up. ctrl-s goes back down again, but not working for me, apparently because my gnome terminal intercepts it before bash's readline sees it, using it for xon/xoff according to http://stackoverflow.com/questions/791765/unable-to-forward-search-bash-history-similarly-as-with-ctrl-r http://stackoverflow.com/questions/791765/unable-to-forward-... This "fixes" it (by disabling the STOP feature of the terminal), so ctrl-s works to search down. stty -ixon
- dryicerx 15y agoTwo more places to extract hostnames for ssh auto completion are .ssh/known_hosts and the .ssh/config. Or give zsh a try, simply autoload compinit and it will do ssh/sftp auto completions along with a boatload more out of the box.
- xenomachina 15y agoIan MacDonald's bash completions (http://www.caliban.org/bash/ http://www.caliban.org/bash/) do this with known_hosts.
- mustpax 15y agoI usually set up my SSH autocompletes to read from known_hosts. The problem is that on some systems, like say Debian, the hostnames are hashed for security reasons. So you have to add the following line to your .ssh/config to get the hostnames back into a readable form. HashKnownHosts no
- avallark 15y agobash_completion does this for most systems. Its also available in mac ports
- remi 15y agoAlso, `brew install bash-completion` with Homebrew.
- alextingle 15y agoZsh. It absolutely mystifies me when people get all excited about trivial enhancements to Bash, when Zsh has had those features for a decade.
- StavrosK 15y agoI downloaded "oh my zsh", never looked back. Of course, I only made it look exactly like fish, which is what I was using, but it's less buggy than fish.
- vidarh 15y agoBash has had this for ages too. Debian comes with it in the "bash-completion" package, for example. People get all excited about trivial enhancements for Bash for the same reasons they don't know about Zsh: The pain isn't great enough for them to actively seek out alternatives.
- jodrellblank 15y agoSo why aren't they on by default? Why is the latest Ubunbtu (styled as a user friendly Linux not a purist Linux) still making the clueless "HUH?" noise at me during tab completion because it doesn't have completion-ignore-case on by default? Or automatically showing a list of clashes or cycling through clashes instead of sitting there like a lemon? Both also one line of config somewhere.
- vidarh 15y agoBecause not enough people care enough to push hard enough for it to change - presumably most of the people who do care don't find having to do apt-get install bash-completion onerous enough to matter.
- allan_ 15y agocomplete -W "$(awk '/^#|^ $/{next}{print tolower($2)}' /etc/hosts) $(awk '/ ^#|^ $/{next}{gsub(/,/," ",$1); print $1}' $HOME/.ssh/known_hosts)" ssh telnet ping
- denysonique 15y agoRemote host path completion when scp'ing or rsync'ing would be great
- denysonique 15y agoActually 'ssh shared connections' does the trick and enables that feature
- denysonique 15y agohttp://blogs.perl.org/users/smylers/2011/08/ssh-productivity-tips.html http://blogs.perl.org/users/smylers/2011/08/ssh-productivity...
- IgorPartola 15y agoI have been using this trick for a while. It is annoying in two ways: first, it includes misspelled hosts. Second, it does not work for scp and sftp. Otherwise, it may save you time.
- Nick_C 15y ago> it does not work for scp and sftp The .ssh/config tip does work though. I'd rather have them there anyway since I can give the host an alias. Host myproxy HostName xxxxxxx.xxx User xxxx Port xxxx and ssh will autocomplete on my<Tab>
- jrussbowman 15y agoOn the flip side a similar script to use a similar technique in .profile to strip out all SSH lines in your history would be a good security measure. That account gets compromised and you wouldn't have a road map of other hosts to connect to. Could remove known_hosts on systems that don't hash the hostnames too. Maybe I really am turning into a grumpy Sa if these are my first thoughts on reading things like this.