Thursday, November 27, 2008

Controlling netgear ( D834G ) router from shell


You can perform the following operations from the shell
  • Disconnect
  • Connect
  • Reconnect (Disconnect, then connect again)
  • Show status
  • reboot router



1 #!/bin/bash
2
3 router_ip=192.168.0.1
4 username="admin"
5 password="CHANGE-ME"
6
7 debug_url="http://$router_ip/setup.cgi?todo=debug"
8 status_url="http://$router_ip/setup.cgi?next_file=s_status.htm"
9 connection_url="http://$router_ip/setup.cgi?next_file=st_poe.htm"
10 disconnect_url="http://$router_ip/setup.cgi?todo=disconnect&next_file=st_poe.htm"
11 connect_url="http://$router_ip/setup.cgi?todo=connect&next_file=st_poe.htm"
12
13
14 RED='\e[1;31m'
15 CYAN='\e[1;36m'
16 NC='\e[0m' # No Color
17
18 tmp_file="/tmp/babil-router.info"
19 sleep_time=3
20
21
22 function clean_up()
23 {
24 rm -f $tmp_file
25 }
26
27 function reboot_router()
28 {
29 report=`wget -q --user $username --password $password $debug_url -O /dev/null`
30 echo "[+] $report"
31 sleep 2
32 (sleep 2; echo "reboot"; sleep 2) | telnet $router_ip
33 }
34
35
36 function ip()
37 {
38 #curl --basic --user $username:$password $status_url 2>/dev/null | sed -e :a -e 's/<[^>]*>//g;/<;/N;//ba' > $tmp_file
39 wget -q --user $username --password $password $status_url -O - | sed -e :a -e 's/<[^>]*>//g;/<;/N;//ba' > $tmp_file
40
41 c=1
42 while read line
43 do
44 #echo "$c $line"
45 if [[ $c == 71 ]];then
46 echo -e "[+] outbound ip : ${RED} $line ${NC}"
47
48 elif [[ $c == 83 ]];then
49 echo -e "[+] gateway ip : ${RED} $line ${NC}"
50
51 fi
52
53 c=$((c+1))
54
55 done < $tmp_file
56 clean_up
57 }
58
59
60 function stat()
61 {
62
63 ip
64
65 #curl --basic --user $username:$password $connection_url 2>/dev/null | sed -e :a -e 's/<[^>]*>//g;/<;/N;//ba' > $tmp_file
66 wget -q --user $username --password $password $connection_url -O - | sed -e :a -e 's/<[^>]*>//g;/<;/N;//ba' > $tmp_file
67
68 c=1
69 while read line
70 do
71 #echo "$c $line"
72 if [[ $c == 61 ]];then
73 echo -e "[+] duration : ${RED} $line ${NC}"
74
75 elif [[ $c == 65 ]];then
76 echo -e "[+] status : ${RED} $line ${NC}"
77
78 fi
79
80 c=$((c+1))
81
82 done < $tmp_file
83
84 clean_up
85
86 }
87
88
89 function connect()
90 {
91 echo -n "[+] connecting "
92 #curl --basic --user $username:$password $connect_url 2>/dev/null | sed -e :a -e 's/<[^>]*>//g;/<;/N;//ba' > $tmp_file
93 wget -q --user $username --password $password $connect_url -O - | sed -e :a -e 's/<[^>]*>//g;/<;/N;//ba' > $tmp_file
94 for ((i=0;i<$sleep_time;i++))
95 do
96 echo -n "."
97 sleep 1
98 done
99
100 echo
101 stat
102 }
103
104
105 function disconnect()
106 {
107 echo -n "[+] disconnecting "
108 #curl --basic --user $username:$password $disconnect_url 2>/dev/null | sed -e :a -e 's/<[^>]*>//g;/<;/N;//ba' > $tmp_file
109 wget -q --user $username --password $password $disconnect_url -O - | sed -e :a -e 's/<[^>]*>//g;/<;/N;//ba' > $tmp_file
110 for ((i=0;i<$sleep_time;i++))
111 do
112 echo -n "."
113 sleep 1
114 done
115
116 echo
117 stat
118 }
119
120 case "$1" in
121
122 connect)
123 echo
124 connect
125 echo
126 ;;
127
128 disconnect)
129 echo
130 disconnect
131 echo
132 ;;
133
134 stat)
135 echo
136 stat
137 echo
138 ;;
139 reboot)
140 echo
141 reboot_router
142 echo
143 ;;
144 reconnect)
145 echo
146 connect
147 echo
148 disconnect
149 echo
150 ;;
151 *)
152 echo
153 echo -e "[+] usage: `basename $0` { ${RED}connect | disconnect | reconnect | stat | reboot${NC} }"
154 echo
155 exit 1
156 ;;
157
158 esac
159
160