Last active
June 10, 2023 07:27
-
-
Save frizzby/78553f939638660aa1b9c2317aaf6e84 to your computer and use it in GitHub Desktop.
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 | |
: ' | |
This script lists all running containers on a Proxmox host along with their | |
Container ID (CTID), IP addresses, and hostnames. If an IP | |
address is provided as a command-line argument, the script will highlight (in red) | |
the IP in the output. | |
The loopback interface and IPv6 link-local addresses are omitted. | |
Usage: | |
./lxc_by_ip.sh [IP address] | |
Parameters: | |
[IP address] - An optional parameter. If provided, the script will highlight | |
this IP address in the output. | |
' | |
# ANSI escape code for highlighted (red) text | |
highlight="\e[31m" | |
# ANSI escape code for resetting text color | |
reset="\e[0m" | |
# Loop over all running containers and print their IDs, IP addresses, and hostnames | |
for i in $(pct list | awk '/running/{print $1}'); do | |
ips=$(pct exec $i -- hostname -I) | |
ips="${ips%"${ips##*[![:space:]]}"}" # Remove trailing spaces | |
hostname=$(pct exec $i -- hostname) | |
if [ -n "$1" ]; then | |
# If this container's IP address matches the highlight IP, print it in red | |
if [[ $ips =~ $1 ]]; then | |
ips=${ips//$1/${highlight}$1${reset}} # Replace the matching IP with a highlighted version | |
echo -e "CTID: $i, IPs: ${ips}, Hostname: $hostname" | |
else | |
echo "CTID: $i, IPs: ${ips}, Hostname: $hostname" | |
fi | |
else | |
echo "CTID: $i, IPs: ${ips}, Hostname: $hostname" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Helps you find which running container (LXC) on a Proxmox host has a certain IP address.