bmclanconf 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #!/bin/bash
  2. #
  3. # Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions
  7. # are met:
  8. #
  9. # Redistribution of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. #
  12. # Redistribution in binary form must reproduce the above copyright
  13. # notice, this list of conditions and the following disclaimer in the
  14. # documentation and/or other materials provided with the distribution.
  15. #
  16. # Neither the name of Sun Microsystems, Inc. or the names of
  17. # contributors may be used to endorse or promote products derived
  18. # from this software without specific prior written permission.
  19. #
  20. # This software is provided "AS IS," without a warranty of any kind.
  21. # ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
  22. # INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
  23. # PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
  24. # SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE
  25. # FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  26. # OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
  27. # SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA,
  28. # OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  29. # PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  30. # LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
  31. # EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  32. # This script is designed for Sun Fire LX50, V60x and V65x systems.
  33. # It may work with other setups as long as you use the correct channel
  34. # and interface settings. It can be used on the V20z as long as you
  35. # specify network settings on the command line.
  36. #
  37. # If the wrong channel is used you could lose network access to the
  38. # box because your BMC will be sending out bad Gratuitous ARP packets
  39. # with the wrong MAC address. You will need to use a console or
  40. # override your local ARP cache with the correct addr.
  41. usage ()
  42. {
  43. echo "
  44. usage: $0 -c <chan> -i <interface> [options]
  45. $0 -c <chan> [-v|w|x|y|z address] [options]
  46. -c channel Which BMC channel to configure [-c help for more info]
  47. -i interface Auto-configure BMC based on interface network settings
  48. -v address Use specified IP address
  49. -w address Use specified Netmask address
  50. -x address Use specified MAC address
  51. -y address Use specified Gateway IP address
  52. -z address Use specified Gateway MAC address
  53. -m interface Which IPMI interface to use [default linux=open solaris=lipmi]
  54. -p password Set BMC channel password
  55. -s string Set SNMP community string
  56. -a interval Set Gratuitous ARP interval, off=0, default=4
  57. -r Reset BMC channel, zero all network settings, disable
  58. -d Debug mode, does not make any changes
  59. "
  60. exit 0;
  61. }
  62. channel_usage ()
  63. {
  64. echo "
  65. Sun Fire V60x and V65x - Linux 2.4
  66. eth0 = channel 6, top
  67. eth1 = channel 7, bottom
  68. Sun Fire V60x and V65x - Linux 2.6
  69. eth0 = channel 7, bottom
  70. eth1 = channel 6, top
  71. Sun Fire V60x and V65x - Solaris x86
  72. e1000g0 = channel 7, bottom
  73. e1000g1 = channel 6, top
  74. Sun LX50 - Linux
  75. eth0 = channel 7, bottom
  76. eth1 = channel 6, top
  77. Sun LX50 - Solaris x86
  78. le0 = channel 7, bottom
  79. le1 = channel 6, top
  80. Sun Fire V20z (-i option does not apply)
  81. channel 1
  82. "
  83. exit 0
  84. }
  85. hex_to_ip ()
  86. {
  87. test $# -ge 1 || return;
  88. local HEX B1 B2 B3 B4;
  89. HEX=`echo $1 | tr '[:lower:]' '[:upper:]'`;
  90. H1=`echo $HEX | cut -c1-2`
  91. H2=`echo $HEX | cut -c3-4`
  92. H3=`echo $HEX | cut -c5-6`
  93. H4=`echo $HEX | cut -c7-8`
  94. B1=`echo 10 o 16 i $H1 p | dc`
  95. B2=`echo 10 o 16 i $H2 p | dc`
  96. B3=`echo 10 o 16 i $H3 p | dc`
  97. B4=`echo 10 o 16 i $H4 p | dc`
  98. echo "$B1.$B2.$B3.$B4"
  99. }
  100. ipmitool_lan_set ()
  101. {
  102. [ $# -lt 1 ] && return
  103. local PARAM=$1
  104. local VALUE=
  105. [ $# -ge 2 ] && VALUE=$2
  106. if [ $DEBUG -gt 0 ]; then
  107. echo "Setting LAN parameter $PARAM $VALUE"
  108. echo "$IPMITOOL -I $IPMIINTF lan set $CHANNEL $PARAM $VALUE"
  109. return
  110. fi
  111. $IPMITOOL -I $IPMIINTF lan set $CHANNEL $PARAM $VALUE
  112. }
  113. ipmitool_lan_reset ()
  114. {
  115. ipmitool_lan_set "ipsrc" "static"
  116. ipmitool_lan_set "ipaddr" "0.0.0.0"
  117. ipmitool_lan_set "netmask" "0.0.0.0"
  118. ipmitool_lan_set "macaddr" "00:00:00:00:00:00"
  119. ipmitool_lan_set "defgw ipaddr" "0.0.0.0"
  120. ipmitool_lan_set "defgw macaddr" "00:00:00:00:00:00"
  121. ipmitool_lan_set "password"
  122. ipmitool_lan_set "snmp" "public"
  123. ipmitool_lan_set "arp generate" "off"
  124. ipmitool_lan_set "access" "off"
  125. exit 0
  126. }
  127. DEBUG=0
  128. LINUX=0
  129. SOLARIS=0
  130. CHANNEL=0
  131. IFACE=
  132. PASSWORD=
  133. SNMP=
  134. GRATARP=8
  135. PATH=$PATH:/bin:/sbin:/usr/bin:/usr/sbin
  136. case `uname -s` in
  137. Linux)
  138. IPMIINTF=open
  139. IPMITOOL=ipmitool
  140. PING=ping
  141. IFCONFIG=ifconfig
  142. ARP=arp
  143. ROUTE=route
  144. ;;
  145. SunOS)
  146. IPMIINTF=lipmi
  147. IPMITOOL=ipmiadm
  148. PING=ping
  149. IFCONFIG=ifconfig
  150. ARP=arp
  151. ROUTE=route
  152. ;;
  153. *)
  154. echo "Invalid OS!"
  155. exit 1
  156. esac
  157. while getopts "dhri:c:m:p:s:a:v:w:x:y:z:" OPTION ; do
  158. case "$OPTION" in
  159. i) IFACE=$OPTARG ;;
  160. c) CHANNEL=$OPTARG ; test X$CHANNEL = Xhelp && channel_usage ;;
  161. m) IPMIINTF=$OPTARG ;;
  162. p) PASSWORD=$OPTARG ;;
  163. s) SNMP=$OPTARG ;;
  164. a) GRATARP=$OPTARG ;;
  165. d) DEBUG=1 ;;
  166. v) IP_ADDRESS=$OPTARG ;;
  167. w) IP_NETMASK=$OPTARG ;;
  168. x) MAC_ADDRESS=$OPTARG ;;
  169. y) GATEWAY_IP=$OPTARG ;;
  170. z) GATEWAY_MAC=$OPTARG ;;
  171. r) ipmitool_lan_reset ;;
  172. h) usage ;;
  173. *) echo "Ignoring invalid option : -$OPTARG" ;;
  174. esac
  175. done
  176. if [ ! -x `which $IPMITOOL` ]; then
  177. echo "Error: unable to find $IPMITOOL"
  178. exit 1
  179. fi
  180. if [ $CHANNEL -eq 0 ]; then
  181. echo
  182. echo "Error: you must specify a channel with -c"
  183. echo
  184. exit 1
  185. fi
  186. if [ "$IFACE" ]; then
  187. if ! $IFCONFIG $IFACE 2>/dev/null | grep "inet " >/dev/null 2>&1 ; then
  188. echo
  189. echo "Error: unable to find interface $IFACE"
  190. echo
  191. exit 1
  192. fi
  193. echo "Auto-configuring $IFACE (channel $CHANNEL)"
  194. fi
  195. case `uname -s` in
  196. SunOS)
  197. if [ X$IFACE != X ]; then
  198. if [ X$IP_ADDRESS = X ]; then
  199. IP_ADDRESS=`$IFCONFIG $IFACE | grep inet | awk '{print $2}'`
  200. fi
  201. if [ X$IP_NETMASK = X ]; then
  202. HEX_NETMASK=`$IFCONFIG $IFACE | grep netmask | awk '{print $4}'`
  203. IP_NETMASK=`hex_to_ip $HEX_NETMASK`
  204. fi
  205. if [ X$MAC_ADDRESS = X ]; then
  206. MAC_ADDRESS=`$IFCONFIG $IFACE | grep ether | awk '{print $2}'`
  207. fi
  208. if [ X$GATEWAY_IP = X ]; then
  209. GATEWAY_IP=`$ROUTE -n get default | grep gateway: | awk '{print $2}'`
  210. fi
  211. if [ X$GATEWAY_MAC = X ]; then
  212. $PING -i $IFACE $GATEWAY_IP 1 >/dev/null 2>&1
  213. GATEWAY_MAC=`$ARP $GATEWAY_IP | awk '{print $4}'`
  214. if [ X$GATEWAY_MAC = Xno ]; then
  215. GATEWAY_MAC=
  216. fi
  217. fi
  218. fi
  219. ;;
  220. Linux)
  221. if [ X$IFACE != X ]; then
  222. if [ X$IP_ADDRESS = X ]; then
  223. IP_ADDRESS=`$IFCONFIG $IFACE | grep "inet addr" | awk -F"[:[:space:]]+" '{print $4}'`
  224. fi
  225. if [ X$IP_NETMASK = X ]; then
  226. IP_NETMASK=`$IFCONFIG $IFACE | grep Bcast | awk -F"[:[:space:]]+" '{print $8}'`
  227. fi
  228. if [ X$MAC_ADDRESS = X ]; then
  229. MAC_ADDRESS=`$IFCONFIG $IFACE | grep HWaddr | awk '{print $5}'`
  230. fi
  231. if [ X$GATEWAY_IP = X ]; then
  232. GATEWAY_IP=`$ROUTE -n | awk '/^0.0.0.0/ {print $2}'`
  233. fi
  234. if [ X$GATEWAY_MAC = X ]; then
  235. $PING -q -c1 $GATEWAY_IP >/dev/null 2>&1
  236. GATEWAY_MAC=`$ARP -an | grep "$GATEWAY_IP[^0-9]" | awk '{print $4}'`
  237. fi
  238. fi
  239. ;;
  240. esac
  241. if [ X$IP_ADDRESS != X ]; then
  242. ipmitool_lan_set "ipsrc" "static"
  243. ipmitool_lan_set "ipaddr" "$IP_ADDRESS"
  244. fi
  245. if [ X$IP_NETMASK != X ]; then
  246. ipmitool_lan_set "netmask" "$IP_NETMASK"
  247. fi
  248. if [ X$MAC_ADDRESS != X ]; then
  249. ipmitool_lan_set "macaddr" "$MAC_ADDRESS"
  250. fi
  251. if [ X$GATEWAY_IP != X ]; then
  252. ipmitool_lan_set "defgw ipaddr" "$GATEWAY_IP"
  253. fi
  254. if [ X$GATEWAY_MAC != X ]; then
  255. ipmitool_lan_set "defgw macaddr" "$GATEWAY_MAC"
  256. fi
  257. if [ X$PASSWORD != X ]; then
  258. ipmitool_lan_set "password" "$PASSWORD"
  259. fi
  260. if [ X$SNMP != X ]; then
  261. ipmitool_lan_set "snmp" "$SNMP"
  262. fi
  263. if [ "$GRATARP" -ne 0 ]; then
  264. ipmitool_lan_set "arp generate" "on"
  265. ipmitool_lan_set "arp interval" "$GRATARP"
  266. else
  267. ipmitool_lan_set "arp generate" "off"
  268. fi
  269. echo "Setting channel authentication capabilities"
  270. ipmitool_lan_set "auth callback,user,operator,admin" "md2,md5"
  271. echo "Enabling channel $CHANNEL"
  272. ipmitool_lan_set "access" "on"
  273. ipmitool_lan_set "user"
  274. exit 0