Created
November 28, 2017 09:30
-
-
Save mikechernev/1964dd2c7174358019944ca1394a3232 to your computer and use it in GitHub Desktop.
Auto generate your i2cssh configuration based on your ~/.ssh/know_hosts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
known_hosts="${HOME}/.ssh/known_hosts" | |
i2csshrc="${HOME}/.i2csshrc" | |
tmpfile="/tmp/i2csshrc" | |
envs="tst acc xpr pro adm shd dev" | |
echo -e "---\nversion: 2\nclusters:" > ${tmpfile} | |
sorted_unique_clusters=$(grep -e "^[a-z][a-z][a-z]-.*-[0-9][0-9][0-9]" ${known_hosts} | sed -e 's/-[0-9][0-9][0-9].*//g' -e 's/^[a-z][a-z][a-z]-//g' | sort | uniq) | |
for cluster in ${sorted_unique_clusters}; do | |
cluster_hosts="" | |
for env in ${envs} | |
do | |
hosts=$(grep "^${env}-${cluster}-" ${known_hosts} | sed -e 's/[\., ].*//g' | sort | uniq) | |
if [[ ! -z ${hosts} ]]; then | |
echo -e " ${env}-${cluster}:\n rows: 2\n hosts:" >> ${tmpfile} | |
cluster_hosts+="$hosts " | |
for host in ${hosts} | |
do | |
echo -e " - ${host}" >> ${tmpfile} | |
done | |
fi | |
done | |
if [[ ! -z $cluster_hosts ]]; then | |
echo -e " ${cluster}:\n rows: 2\n hosts:" >> ${tmpfile} | |
for host in $cluster_hosts; do | |
echo -e " - ${host}" >> ${tmpfile} | |
done | |
fi | |
done | |
mv ${tmpfile} ${i2csshrc} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment