2

I keep destroying and provisioning VMs, so ssh frequently complains that I'm being NASTY. Mmmm...

Anyways, I wrote a fish function to banish the offending key on the line it tells me with sed:

function forgethost
   sed -i.bak {$argv}d ~/.ssh/known_hosts
end

but I'd like to keep the command quoted to protect me against (injecting) myself a little bit. When I wrap it in '', it then doesn't replace $argv. Also, I'd prefer to just grab the first argument [0], but when I add that it also doesn't expand.

2 Answers 2

2

Create a ~/.ssh/config entry for the VMs you're continually recreating. Example below. Means you don't have to keep hacking your ~/.ssh/known_hosts file.

Host test-vm-1
    StrictHostKeyChecking no
4
  • It's across a range of IPs, can I say Host 10.11.12.13/24 or the like?
    – Nick T
    Nov 3, 2016 at 14:50
  • Yes : can say Host 10.11.12.?. See man page for ssh_config, section PATTERNS.
    – steve
    Nov 3, 2016 at 18:15
  • Online man for convienence. /24 may have been a bit too easy, mine is actually a /22 and a /23 so might need to cobble together multiple imprecise patterns.
    – Nick T
    Nov 3, 2016 at 19:39
  • Could always cobble together some command to drop the explicit IPs into the config file. e.g. in the case of /24 : echo {1..255}|xargs -n 1 printf "Host 1.2.3.%d\n StrictHostKeyChecking no\n"
    – steve
    Nov 3, 2016 at 20:04
1

When I wrap it in '', it then doesn't replace $argv.

What you want is double-quotes, i.e. "$argv". This is the same as in other shells.

Also, I'd prefer to just grab the first argument [0], but when I add that it also doesn't expand.

In fish, the first element of a list has the index 1, so you want $argv[1].

Together that means

function forgethost
   sed -i.bak "$argv[1]"d ~/.ssh/known_hosts
end

Of course you could also add error handling if more than one argument has been given with

if set -q argv[2] # yes, no $
    echo "This only accepts one argument" >&2
    return 1
endt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.