Skip to content

Instantly share code, notes, and snippets.

@DSdatsme
Last active December 29, 2019 20:12
Show Gist options
  • Save DSdatsme/4794e8923e44240357e091c96e8cab36 to your computer and use it in GitHub Desktop.
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
#!/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
@DSdatsme
Copy link
Author

DSdatsme commented Dec 29, 2019

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

  • go to bin directory
    cd /usr/local/bin
  • now create a file called inv. (you can give it any name. This will be your command that you will use to run the script)
    touch inv
  • Now you have to open the newly created file and paste the script and save it. You can use any text editor nano, vi, vscode, etc.. (I am using vscode here)
    code inv
  • You have to change 2 variables before you have your script.
    • INVENTORY_FILE --> specify the file location of the inventory file
    • SSH_USER --> your ssh user name. (if you don't know the value over here, generally it can be your current Linux/mac username. It can be found by running echo $USER )
  • The command won’t run until you give executable permissions.
    chmod +x inv
  • That’s it. Try it by running:
    inv

Sample inventory file might look like

[domain1]
1.2.3.4 nickname=a.com

[domain2]
10.0.0.1
10.0.0.2 name=idontknow.com

Requirements:

  • This script used peco, install it if not installed.
  • you should have already your private key set up in ~/.ssh/id_rsa file. (this is because ssh command takes that as the input private key if not explicitly specified.)
  • if you have a key stored at a different location you can change the ssh command to something like this:
    ssh -i /path/to/private_key $SSH_USER@$server_ips
  • if you are not able to connect, this script has nothing to do with it, that's because of your servers issue.
  • you need iTerm to use multiple ssh feature or it might just fail. (not tested, but sure)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment