Created
April 21, 2025 03:24
-
-
Save huynhbaoan/4cf9a328859e9cbc487477f7ad3f11a6 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env bash | |
# runscript.sh | |
# Usage: ./runscript.sh <ip-address> | |
set -euo pipefail | |
IP_ADDRESS=${1:-} | |
if [[ -z $IP_ADDRESS ]]; then | |
echo "Usage: $0 <ip-address>" | |
exit 1 | |
fi | |
# --- 1. fetch the ENI for this IP (first match only) ------------------------ | |
eni=$(aws ec2 describe-network-interfaces \ | |
--filters "Name=private-ip-address,Values=$IP_ADDRESS" \ | |
--query 'NetworkInterfaces[0]' \ | |
--output json 2>/dev/null) || eni="null" | |
if [[ -z $eni || $eni == "null" ]]; then | |
echo "IP $IP_ADDRESS → not found" | |
exit 0 | |
fi | |
# extract fields | |
interface_type=$(jq -r '.InterfaceType // ""' <<<"$eni") | |
description=$(jq -r '.Description // ""' <<<"$eni") | |
attachment_instance_id=$(jq -r '.Attachment.InstanceId // ""' <<<"$eni") | |
# we’ll refer directly to .TagSet via jq where needed | |
# --- 2. classify by InterfaceType & Description ---------------------------- | |
# 2.1 Lambda | |
if [[ $interface_type == "lambda" ]]; then | |
# desc: "AWS Lambda VPC ENI-<FunctionName>-<UUID>" | |
name_part=${description#AWS Lambda VPC ENI-} | |
lambda_name=${name_part%%-*} | |
echo "Lambda $lambda_name" | |
exit 0 | |
fi | |
# 2.2 NLB | |
if [[ $interface_type == "network_load_balancer" ]]; then | |
# desc: "ELB net/<NLB-name>/<id>" | |
name_part=${description#ELB net/} | |
nlb_name=${name_part%%/*} | |
echo "NLB $nlb_name" | |
exit 0 | |
fi | |
# 2.3 ALB | |
if [[ $description == ELB\ app/* ]]; then | |
# desc: "ELB app/<ALB-name>/<id>" | |
name_part=${description#ELB app/} | |
alb_name=${name_part%%/*} | |
echo "ALB $alb_name" | |
exit 0 | |
fi | |
# 2.4 ECS | |
ecs_arn=$(jq -r '.TagSet[]? | select(.Key=="aws:ecs:taskArn") | .Value' <<<"$eni") | |
if [[ -n $ecs_arn ]]; then | |
echo "ECS $ecs_arn" | |
exit 0 | |
fi | |
# 2.5 EKS branch (trunk vs branch is distinguished by InterfaceType) | |
if [[ $interface_type == "branch" ]]; then | |
echo "EKS branch" | |
exit 0 | |
fi | |
# 2.6 EKS trunk | |
eks_node_id=$(jq -r '.TagSet[]? | select(.Key=="node.k8s.amazonaws.com/instance_id") | .Value' <<<"$eni") | |
if [[ -n $eks_node_id ]]; then | |
node_name=$(aws ec2 describe-instances \ | |
--instance-ids "$eks_node_id" \ | |
--query 'Reservations[0].Instances[0].Tags[?Key==`Name`].Value|[0]' \ | |
--output text 2>/dev/null || echo "") | |
echo "EKS ${node_name:-$eks_node_id}" | |
exit 0 | |
fi | |
# 2.7 plain EC2 | |
if [[ -n $attachment_instance_id ]]; then | |
inst_name=$(aws ec2 describe-instances \ | |
--instance-ids "$attachment_instance_id" \ | |
--query 'Reservations[0].Instances[0].Tags[?Key==`Name`].Value|[0]' \ | |
--output text 2>/dev/null || echo "") | |
echo "EC2 ${inst_name:-$attachment_instance_id}" | |
exit 0 | |
fi | |
# 2.8 anything else | |
echo "Other" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment