Last active
December 29, 2019 20:12
-
-
Save DSdatsme/4794e8923e44240357e091c96e8cab36 to your computer and use it in GitHub Desktop.
inventory utility to quickly ssh to servers just by searching them in a file
This file contains 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 | |
INVENTORY_FILE='/path/to/inventory_file' | |
SSH_USER='darshit' | |
server_ips=$(cat $INVENTORY_FILE | peco | awk '{print $1;}' ) | |
iterm_newtab(){ | |
osascript \ | |
-e 'tell application "iTerm2" to tell current window to set newWindow to (create tab with default profile)'\ | |
-e "tell application \"iTerm2\" to tell current session of newWindow to write text \"${@}\"" | |
} | |
ip_counts=$( echo $server_ips | wc -w ) # counting number of words | |
echo $ip_counts | |
if [ $ip_counts -eq 1 ] ; then | |
ssh $SSH_USER@$server_ips | |
else | |
echo "sessions opened in different tabs" | |
for each_server_ip in $server_ips; do | |
echo "tab opened for -->" $each_server_ip | |
iterm_newtab "ssh $SSH_USER@$each_server_ip" | |
done | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
EASE_SSH
What does this do???
If you have a big inventory file and you are the person who needs to keep sshing the servers now and then, this will make your life easy.
How does this work???
This script runs peco on the inventory file, this way you get an interactive search. After you select your line, it filters out the first word of each line you selected which are IP addresses as per inventory file. It then checks for the number of IP addresses found, if you selected only one line then it will straight forward run ssh command on that one IP address, but if you had selected multiple, then it will iterate through each and open the ssh session for each in a new iTerm tab.
setup
cd /usr/local/bin
inv
. (you can give it any name. This will be your command that you will use to run the script)touch inv
code inv
echo $USER
)chmod +x inv
inv
Sample inventory file might look like
Requirements:
ssh -i /path/to/private_key $SSH_USER@$server_ips