So our work environment makes use of Vagrant to emulate real environment and the setup is ….annoying LONG but luckily you only need to set it up one…until the box is acting up……in which case you have to destroy and set it up again.
Fine. But here’s where it gets very annoying because you have to regenerate public key for the box again. It dawns on me that since the box lives in my machine, why can’t I just reuse the public key that I already generate to pull the repository in the first place.
I move all the keys to the mount folder( sync folder ) and copy it to ~/.ssh/ and found that it works. Hooray. But this is too much clicking. Can I automate it? This answer posits that you could scp to the box using the IdentityFile, which is what I decided to do.
The gist of the command is to substitute the variable into scp command:
scp -P ${port} -i ${identityFile} file ${User}@${HostName}:${destination}/
vagrant ssh-config actually spits out the useful Info. So we can parse that
#!/bin/sh dest="" #parse dest ssh_config="$(vagrant ssh-config)" interest_keys=("HostName" "User" "Port" "IdentityFile") interest_vals=() current_key_index=0 current_key="${interest_keys[${current_key_index}]}" grab_value=false for line in $ssh_config do if [ "${grab_value}" = true ];then interest_vals+=("${line}") current_key_index="${current_key_index} + 1" current_key="${interest_keys[${current_key_index}]}" grab_value=false elif [ "${line}" = "${current_key}" ]; then grab_value=true fi done cmd="scp -P ${interest_vals[2]} -i ${interest_vals[3]} ${@} ${interest_vals[1]}@${interest_vals[0]}:${dest}/" eval $cmd
And then copy all the public key as well as ssh config to vagrant with this command
#chmod +x script before run ./script ~/.ssh/* --dest=~/.ssh/
Here is the full code along with parsing destination and trying to infer the default destination ( wild card exansion . Not working yet!!!)
#!/bin/sh ssh_config="$(vagrant ssh-config)" default_dest="" prev_dest="" default_dest_inferrable=true dest="" file_names=() interest_keys=("HostName" "User" "Port" "IdentityFile") interest_vals=() current_key_index=0 current_key="${interest_keys[${current_key_index}]}" grab_value=false #try to infer the default dest if parameters having the same dirname for file_name in "$@" do case ${file_name} in --dest=*) dest="${file_name#*=}" shift ;; *) dir_name="$(dirname ${file_name})" if [[ "$prev_dest" = "" ]];then prev_dest="${dir_name}" elif [[ "$prev_dest" != "$dir_name" ]]; then default_dest_inferrable=false fi file_names+="$file_name " ;; esac done if [[ "$dest" = "" ]] && [[ "$default_dest_inferrable" = false ]]; then echo "Cannot parse destination or Cannot infer the destination" exit fi if [[ "$dest" = "" ]]; then dest="${prev_dest}" fi for line in $ssh_config do if [ "${grab_value}" = true ];then interest_vals+=("${line}") current_key_index="${current_key_index} + 1" current_key="${interest_keys[${current_key_index}]}" grab_value=false elif [ "${line}" = "${current_key}" ]; then grab_value=true fi done cmd="scp -P ${interest_vals[2]} -i ${interest_vals[3]} ${@} ${interest_vals[1]}@${interest_vals[0]}:${dest}/" eval $cmd
Beware also. As soon as the known_hosts were copied over, go in there to delete 127.0.0.1 so that Vagrant won’t ask for password the next time. It stucks connects to itself I think…..