Skip to content

Instantly share code, notes, and snippets.

@oglops
Last active August 26, 2024 22:01
Show Gist options
  • Save oglops/ee041821b315fcd62a5e8d2fb5932255 to your computer and use it in GitHub Desktop.
Save oglops/ee041821b315fcd62a5e8d2fb5932255 to your computer and use it in GitHub Desktop.
Reboot aimesh nodes one by one, then reboot main router
#!/bin/env bash
# this returns all aimesh routers
# nvram get cfg_device_list
router="192.168.0.1"
user="admin"
port=1024
pwd=password
# query aimesh nodes
# intentionally escape \EOF so that all $ symbols are literal
output=$(
ssh -T -o StrictHostKeyChecking=no $user@$router -p $port << \EOF
#echo $PATH
ipv4_regex="([0-9]{1,3}\.){3}[0-9]{1,3}"
# echo ${ipv4_regex}
ipv4_addresses=$(/bin/nvram get cfg_device_list | grep -E -o "${ipv4_regex}")
# echo $ipv4_addresses
# Print the extracted IPv4 addresses
for ip in $ipv4_addresses; do
# echo "Found IPv4 address: $ip"
echo $ip
done
EOF
2>&1
)
# This time without escaping EOF so that $ symbols are subsituted
for ip in ${output}; do
if [ "$ip" != "$router" ]; then
if ping -c 2 $ip >/dev/null 2>&1; then
echo rebooting aimesh node "$ip"
/usr/bin/expect <<- EOF
spawn ssh -T -oStrictHostKeyChecking=no -oCheckHostIP=no $user@$ip -p $port
expect "password"
send -- "$pwd\r"
expect "$user@"
send -- "/sbin/reboot\r"
expect "Connection to $ip closed"
EOF
fi
fi
done
# reboot main router
ssh -T -o StrictHostKeyChecking=no $user@$router -p $port -t reboot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment