Skip to content

Instantly share code, notes, and snippets.

@moozd
Created March 31, 2026 14:42
Show Gist options
  • Select an option

  • Save moozd/1cc0c1704a77fb6ca0a004ba7f21d0bf to your computer and use it in GitHub Desktop.

Select an option

Save moozd/1cc0c1704a77fb6ca0a004ba7f21d0bf to your computer and use it in GitHub Desktop.
watch vless connections
#!/bin/bash
# ─── Colors ───────────────────────────────────────────────────────────────────
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m'
# ─── Init: clear once, hide cursor ────────────────────────────────────────────
clear
tput civis
trap 'tput cnorm; echo ""; exit' INT TERM
while true; do
# ─── Collect all data first ───────────────────────────────────────────────
NOW=$(date '+%Y-%m-%d %H:%M:%S')
SERVER_IP=$(hostname -I | awk '{print $1}')
SNI=$(grep -o '"dest": "[^"]*"' /usr/local/etc/xray/config.json 2>/dev/null | head -1 | cut -d'"' -f4 | cut -d: -f1)
if systemctl is-active --quiet xray; then
XRAY_STATUS="${GREEN}running${NC}"
else
XRAY_STATUS="${RED}STOPPED${NC}"
fi
CLIENTS=$(ss -tnp 2>/dev/null \
| grep "xray" \
| awk '$4 ~ /:443$/' \
| awk '{print $5}' \
| sed 's/::ffff://g' \
| cut -d: -f1 \
| sort | uniq -c | sort -rn)
TOTAL=$(echo "$CLIENTS" | grep -c '[0-9]' 2>/dev/null || echo 0)
OUT_COUNT=$(ss -tnp 2>/dev/null | grep "xray" | awk '$4 !~ /:443$/' | wc -l)
RX1=$(cat /sys/class/net/eth0/statistics/rx_bytes 2>/dev/null)
TX1=$(cat /sys/class/net/eth0/statistics/tx_bytes 2>/dev/null)
sleep 1
RX2=$(cat /sys/class/net/eth0/statistics/rx_bytes 2>/dev/null)
TX2=$(cat /sys/class/net/eth0/statistics/tx_bytes 2>/dev/null)
RX_RATE=$(( (RX2 - RX1) / 1024 ))
TX_RATE=$(( (TX2 - TX1) / 1024 ))
# ─── Build output buffer ──────────────────────────────────────────────────
BUF=""
BUF+="\n"
BUF+=" ${BOLD}${CYAN}╔══════════════════════════════════════════════════════╗${NC}\n"
BUF+=" ${BOLD}${CYAN}║ XRAY REALITY - LIVE MONITOR ║${NC}\n"
BUF+=" ${BOLD}${CYAN}╚══════════════════════════════════════════════════════╝${NC}\n"
BUF+=" ${BOLD}Time :${NC} ${NOW}\n"
BUF+=" ${BOLD}Server :${NC} ${SERVER_IP}\n"
BUF+=" ${BOLD}Xray :${NC} ${XRAY_STATUS}\n"
BUF+=" ${BOLD}SNI :${NC} ${SNI:-unknown}\n"
BUF+="\n"
BUF+=" ${BOLD}${YELLOW} Connected Clients (${TOTAL} unique IPs)${NC}\n"
BUF+=" ──────────────────────────────────────────────────────\n"
BUF+="$(printf " ${BOLD}%-6s %-22s %-20s${NC}" "CONNS" "CLIENT IP" "HOSTNAME")\n"
BUF+=" ──────────────────────────────────────────────────────\n"
if [[ -z "$CLIENTS" || "$TOTAL" -eq 0 ]]; then
BUF+=" ${RED} No active clients${NC}\n"
else
while read -r count ip; do
[[ -z "$ip" ]] && continue
HOST=$(timeout 1 dig +short -x "$ip" 2>/dev/null | head -1 | sed 's/\.$//')
[[ -z "$HOST" ]] && HOST="-"
BUF+="$(printf " ${GREEN}%-6s${NC} %-22s %-20s" "$count" "$ip" "$HOST")\n"
done <<< "$CLIENTS"
fi
BUF+="\n"
BUF+=" ${BOLD}${YELLOW} Outbound Proxy Connections${NC}\n"
BUF+=" ──────────────────────────────────────────────────────\n"
BUF+=" Active outbound streams : ${CYAN}${OUT_COUNT}${NC}\n"
BUF+="\n"
BUF+=" ${BOLD}${YELLOW} Bandwidth (eth0)${NC}\n"
BUF+=" ──────────────────────────────────────────────────────\n"
BUF+=" ${GREEN}↓ IN ${NC} : ${RX_RATE} KB/s\n"
BUF+=" ${CYAN}↑ OUT${NC} : ${TX_RATE} KB/s\n"
BUF+="\n"
BUF+=" ${BOLD}Ctrl+C to exit${NC}\n"
# ─── Print buffer in one shot from top ────────────────────────────────────
tput cup 0 0
echo -ne "$BUF"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment