Hey Mate! Welcome to another blog. Here we will discuss about SSH automation.
Tired of manually typing long SSH commands every time? Want to auto-complete your SSH like a pro? Well, you’re in the right place!
Ever find yourself typing out long SSH commands, only to realize you’ve misspelled the hostname? Or worse — forgotten the exact host? 😫 If you’re tired of manually looking up hostnames, it’s time to level up your SSH game with auto-completion using ~/.ssh/config! 🚀
With just a bit of setup, you can:
✅ Auto-complete SSH hostnames with Tab
✅ Avoid remembering long hostnames
✅ Save time and effort while connecting
✅SSH UI for Login
Let’s dive in and make SSH work for you, not against you! 😎
1️⃣ Without Use SSH Auto-Completion?
The Problem
Without SSH auto-completion, you might find yourself doing things like:
ssh 192.168.1.104 -i ~/.ssh/id_rsa -p 2222 -J jumpserverThat’s a lot of typing, and let’s be real — you’re going to mess it up at some point.
Solution:
🛠 Create or Edit Your ~/.ssh/config File
Run this command to open (or create) your SSH config file:
nano ~/.ssh/configNow, add your SSH hosts in this format:
Host my-server
HostName 192.168.1.104
User myuser
IdentityFile ~/.ssh/id_rsa
Port 2222
ProxyJump jumpserver
Host my-webserver
HostName 10.0.0.5
User ubuntu
IdentityFile ~/.ssh/webserver_key
Host jumpserver
HostName 10.11.0.5
User ubuntu
IdentityFile ~/.ssh/webserver_keyThe real problem is here:
But wait if you have 50+ hostname, how can you remember for all the hostnames ? it’s pretty annoying right.
How It feel like, if you can complete with “TAB” while type SSHhostnames ?
Like linux navigation 😋😋
Let’s do SSH Auto Completion…
🛠 2 : Enable Auto-Completion in Bash
Now, let’s make sure your terminal can auto-complete SSH hostnames:
complete -W "$(grep "Host" ~/.ssh/config | grep -vE "HostName|Hostname|no" | awk '{print $2}' | sed -E "/\*/d")" ssh💡 Tip: Add this line to ~/.bashrc or ~/.zshrc to make it permanent:
echo 'complete -W "$(grep "Host" ~/.ssh/config | grep -vE "HostName|Hostname|no" | awk '{print $2}' | sed -E "/\*/d")"' ssh >> ~/.bashrc
source ~/.bashrc3. Testing SSH Auto-Completion:
Now, try typing:
ssh my<tab>
# output
my-server my-webserverYour terminal should auto-complete to myserver. 🎉
4. Let’s Connect via SSH UI:
But wait, yes you heard right it’s SSH UI.
Execute the below script, and you navigate and log in the hosts.
Pre-requisites:
apt install dialogSSH-UI Script:
#!/bin/bash
# Extract hosts from SSH config
HOSTS=$(grep "Host" ~/.ssh/config | grep -vE "HostName|Hostname|no" | awk '{print $2}' | sort -h | sed -E "/\*/d")
# Build dialog menu options dynamically
MENU_OPTIONS=""
COUNT=1
declare -A HOST_MAP # Map for storing numbered options
while read -r HOST; do
MENU_OPTIONS+="$COUNT \"$HOST\" "
HOST_MAP["$COUNT"]="$HOST"
((COUNT++))
done <<< "$HOSTS"
# Show dialog menu
MACHINE=$(dialog --menu "Welcome! Karthick-Dkk\nSelect Machine for Login:" 0 0 0 $MENU_OPTIONS 3>&1 1>&2 2>&3 3>&-)
clear
# If a valid selection was made, SSH into the selected host
if [[ -n "$MACHINE" ]]; then
ssh "${HOST_MAP[$MACHINE]}"
exit
else
echo "No machine selected. Exiting..."
exit 1
fiExecute:
chmod +x
./ssh-ui-script
Add to system-level command: (Optional)
If tired of executing manually, we can make our life easier, will add system command for SSH-UI.
command: ui-ssh
# ln -s >location of script> <command-name>
ln -s ssh-ui-script /usr/bin/ui-sshOnce added you can use the below command to open the SSH-UI.
ui-sshNow you can select your SSH host from a menu instead of typing! 🖥️
️5. Bonus: Convert Salt Roster (/etc/salt/roster) to ~/.ssh/config (Automation)
If you manage many hosts using SaltStack, manually adding entries is a pain. Here’s a script to convert /etc/salt/roster to ~/.ssh/config automatically:
🔧 Roster to SSH Config Script
#!/bin/bash
ROSTER_FILE="/etc/salt/roster"
SSH_CONFIG="$HOME/.ssh/config"
# Backup existing SSH config
cp "$SSH_CONFIG" "$SSH_CONFIG.bak"
> "$SSH_CONFIG"
while read -r line; do
[[ -z "$line" || "$line" =~ ^#.*$ ]] && continue
if [[ "$line" =~ ^[a-zA-Z0-9_-]+: ]]; then
HOST_ALIAS="${line%:}"
elif [[ "$line" =~ "host:" ]]; then
HOST_IP=$(echo "$line" | awk '{print $2}')
elif [[ "$line" =~ "user:" ]]; then
SSH_USER=$(echo "$line" | awk '{print $2}')
elif [[ "$line" =~ "port:" ]]; then
SSH_PORT=$(echo "$line" | awk '{print $2}')
elif [[ "$line" =~ "priv:" ]]; then
SSH_KEY=$(echo "$line" | awk '{print $2}')
elif [[ "$line" =~ "proxy:" ]]; then
PROXY_JUMP=$(echo "$line" | awk '{print $2}')
fi
if [[ -n "$HOST_ALIAS" && -n "$HOST_IP" && -n "$SSH_USER" && -n "$SSH_KEY" ]]; then
{
echo "Host ${HOST_ALIAS}"
echo " HostName $HOST_IP"
echo " User $SSH_USER"
echo " Port ${SSH_PORT:-22}"
echo " IdentityFile $SSH_KEY"
[[ -n "$PROXY_JUMP" ]] && echo " ProxyJump $PROXY_JUMP"
echo ""
} >> "$SSH_CONFIG"
unset HOST_ALIAS HOST_IP SSH_USER SSH_PORT SSH_KEY PROXY_JUMP
fi
done < "$ROSTER_FILE"
echo "SSH config updated!"Run It:
chmod +x roster_to_ssh_config.sh
sudo ./roster_to_ssh_config.shTL;DR — Quick Setup
1️⃣ Edit ~/.ssh/config and add your hosts
2️⃣ Enable auto-completion in Bash/Zsh
3️⃣ Use ssh <tab> to autocomplete hostnames
4️⃣ (Bonus) Convert /etc/salt/roster to ~/.ssh/config
5️⃣ (Bonus) SSH menu selection script
Enjoy your new and improved SSH workflow! 🎉
Why This Rocks?
✅ Saves Time: No more typing long SSH commands
✅ Auto-Completion: Just press TAB and autocomplete!
✅ No More IP Headaches: Human-friendly SSH hostnames
✅ Portable & Reusable: Works across multiple machines
Final Thoughts:
🔹 Before: Manually typing long SSH commands 😩
🔹 After: Tab-completion, menu selection, and automation! 🚀
This setup saves time, reduces errors, and makes SSH easier. No more “What was that server’s IP again?” moments! 😂
If you found this useful, share it with your fellow sysadmins! 💡💻
Try it out and thank me later! 😉 Happy SSH-ing! 🚀🐧
No comments:
Post a Comment