Skip to content

Instantly share code, notes, and snippets.

@m0rff
Last active April 27, 2016 13:22
Show Gist options
  • Save m0rff/d8b5c809ec5be28b818ecb94d61a7c23 to your computer and use it in GitHub Desktop.
Save m0rff/d8b5c809ec5be28b818ecb94d61a7c23 to your computer and use it in GitHub Desktop.
a simple script when you often have to ssh into the same ip ranges with changing ssh keys (e.g. often reinstalled vms)

When you often have to SSH into VMs with changing SSH Keys and you dont want to edit your known_hosts everytime ...

Usage:

sshvm.sh 2

Looks for 10.2.3.2 in your known_hosts. Deletes any matching entries. Copies your ssh key. SSHs into the machine.

You need the expect package.

#!/bin/bash
IP=10.2.3.$1
SSHUSER=youruser
PW=yourpassword
sed -i "/^$IP/d" /home/$USER/.ssh/known_hosts
expect << EOF
spawn ssh-copy-id $SSHUSER@$IP
expect "Are you sure you want to continue connecting (yes/no)?"
send "yes\n"
expect "password:"
send "$PW\n"
expect eof
EOF
ssh -o "StrictHostKeyChecking no" $SSHUSER@$IP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment