OpenGnsys  1.1.1
Net.lib
Ir a la documentación de este archivo.
1 #!/bin/bash
2 #
12 #
22 function ogChangeRepo ()
23 {
24 local SRCIMG NEWREPO REPO OGUNIT
25 
26 if [ "$*" == "help" ]; then
27  ogHelp "$FUNCNAME" "$FUNCNAME IPREPO [ OgUnit ]" \
28  "$FUNCNAME 10.1.120.3" \
29  "$FUNCNAME 10.1.120.3 cdc"
30  return
31 fi
32 
33 
34 if [ $# -lt 1 ]; then
35  ogRaiseError $OG_ERR_FORMAT "$MSG_FORMAT: $FUNCNAME IPREPO [ OgUnit ]"
36  return $?
37 fi
38 
39 
40 # Opciones de montaje: lectura o escritura
41 mount |grep "ogimages.*rw," &>/dev/null && RW=",rw" || RW=",ro"
42 
43 # Si REPO tomamos el repositorio y la unidad organizativa actual
44 REPO=$(ogGetRepoIp)
45 OGUNIT="$(df | awk -F " " '/ogimages/ {sub("//.*/ogimages","",$1); sub("/","",$1); print $1}')"
46 
47 # Parametros de entrada. Si $1 = "REPO" dejo el repositorio actual
48 [ "${1^^}" == "REPO" ] && NEWREPO="$REPO" || NEWREPO="${1}"
49 
50 # Si $1 y $2 son el repositorio y la OU actual me salgo
51 [ "$NEWREPO" == "$REPO" ] && [ "$2" == "$OGUNIT" ] && return 0
52 
53 source /scripts/functions
54 source /scripts/ogfunctions
55 umount $OGIMG
56 [ "$2" == "" ] && SRCIMG="ogimages" || SRCIMG="ogimages/$2"
57 eval $(grep "OPTIONS=" /scripts/ogfunctions)
58 
59 ogEcho session log "$MSG_HELP_ogChangeRepo $NEWREPO ${2%/}"
60 ogConnect $NEWREPO $ogprotocol $SRCIMG $OGIMG $RW
61 
62 # Si da error volvemos a montar el inicial
63 if [ $? -ne 0 ]; then
64  ogConnect $REPO $ogprotocol $SRCIMG $OGIMG $RW
65  ogRaiseError session $OG_ERR_REPO "$NEWREPO"
66  return $?
67 fi
68 
69 }
70 
71 
72 #
83 function ogGetGroupDir ()
84 {
85 local REPO DIR GROUP
86 if [ "$*" == "help" ]; then
87  ogHelp "$FUNCNAME" "$FUNCNAME str_repo" \
88  "$FUNCNAME REPO ==> /opt/opengnsys/images/groups/Grupo1"
89  return
90 fi
91 # Error si se recibe más de 1 parámetro.
92 case $# in
93  0) REPO="REPO" ;;
94  1) REPO="$1" ;;
95  *) ogRaiseError $OG_ERR_FORMAT "$*"
96  return $? ;;
97 esac
98 
99 GROUP="$(ogGetGroupName)"
100 if [ -n "$GROUP" ]; then
101  DIR=$(ogGetPath "$REPO" "/groups/$GROUP" 2>/dev/null)
102  [ -d "$DIR" ] && echo "$DIR"
103 fi
104 # Para que no haya error al fallar la condición anterior
105 return 0
106 }
107 
108 
109 #
117 function ogGetGroupName ()
118 {
119 if [ "$*" == "help" ]; then
120  ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME => Grupo1"
121  return
122 fi
123 [ -n "$group" ] && echo "$group"
124 }
125 
126 
127 # ##
135 function ogGetHostname ()
136 {
137 local HOST
138 if [ "$*" == "help" ]; then
139  ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME => pc1"
140  return
141 fi
142 # Tomar nombre de la variable HOSTNAME
143 HOST="$HOSTNAME"
144 # Si no, tomar del DHCP, opción host-name /* (comentario para Doxygen)
145 [ -z "$HOST" ] && HOST=$(awk -F\" '/option host-name/ {gsub(/;/,""); host=$2}
146  END {print host}
147  ' /var/lib/dhcp3/dhclient.leases)
148 # Si no, leer el parámetro del kernel hostname (comentario para Doxygen) */
149 [ -z "$HOST" ] && HOST=$(awk 'BEGIN {RS=""; FS="="}
150  $1~/hostname/ {print $2}' /proc/cmdline)
151 [ "$HOSTNAME" != "$HOST" ] && export HOSTNAME="$HOST"
152 [ -n "$HOST" ] && echo $HOST
153 }
154 
155 
156 # ##
171 function ogGetIpAddress ()
172 {
173 local IP
174 if [ "$*" == "help" ]; then
175  ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME => 192.168.0.10"
176  return
177 fi
178 if [ -n "$IPV4ADDR" ]; then
179  IP="$IPV4ADDR"
180 else
181  # Obtener direcciones IP.
182  if [ -n "$DEVICE" ]; then
183  IP=$(ip -o address show up dev "$DEVICE" 2>/dev/null | awk '{if ($3~/inet$/) {printf ("%s ", $4)}}')
184  else
185  IP=$(ip -o address show up | awk '$2!~/lo/ {if ($3~/inet$/) {printf ("%s ", $4)}}')
186  fi
187 fi
188 # Mostrar solo la primera.
189 echo "${IP%%/*}" # (comentario para Doxygen) */
190 }
191 
192 
193 # ##
204 function ogGetMacAddress ()
205 {
206 local MAC
207 if [ "$*" == "help" ]; then
208  ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME => 00:11:22:33:44:55"
209  return
210 fi
211 # Obtener direcciones Ethernet.
212 if [ -n "$DEVICE" ]; then
213  MAC=$(ip -o link show up dev "$DEVICE" 2>/dev/null | awk '{sub (/.*\\/, ""); if ($1~/ether/) printf ("%s ", toupper($2));}')
214 else
215  MAC=$(ip -o link show up | awk '$2!~/lo/ {sub (/.*\\/, ""); if ($1~/ether/) printf ("%s ", toupper($2));}')
216 fi
217 # Mostrar sólo la primera.
218 echo ${MAC%% *}
219 }
220 
221 
222 # ##
231 function ogGetNetInterface ()
232 {
233 if [ "$*" == "help" ]; then
234  ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME => eth0"
235  return
236 fi
237 [ -n "$DEVICE" ] && echo "$DEVICE"
238 }
239 
240 
241 # ##
256 function ogGetRepoIp ()
257 {
258 # Variables locales.
259 local SOURCE FSTYPE
260 
261 # Mostrar ayuda.
262 if [ "$*" == "help" ]; then
263  ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME => 192.168.0.2"
264  return
265 fi
266 
267 # Obtener direcciones IP, según el tipo de montaje.
268 eval $(findmnt -P -o SOURCE,FSTYPE $OGIMG)
269 case "$FSTYPE" in
270  nfs) echo "$SOURCE" | cut -f1 -d: ;;
271  cifs) echo "$SOURCE" | cut -f3 -d/ ;;
272 esac
273 }
274 
275 
276 # ##
291 function ogGetServerIp ()
292 {
293 # Variables locales.
294 local SOURCE FSTYPE
295 
296 # Mostrar ayuda.
297 if [ "$*" == "help" ]; then
298  ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME => 192.168.0.2"
299  return
300 fi
301 
302 # Obtener direcciones IP, según el tipo de montaje.
303 eval $(findmnt -P -o SOURCE,FSTYPE $OPENGNSYS)
304 case "$FSTYPE" in
305  nfs) echo "$SOURCE" | cut -f1 -d: ;;
306  cifs) echo "$SOURCE" | cut -f3 -d/ ;;
307 esac
308 }
309 
310 
311 #
322 function ogMakeGroupDir ()
323 {
324 local REPO DIR GROUP
325 if [ "$*" == "help" ]; then
326  ogHelp "$FUNCNAME" "$FUNCNAME str_repo" \
327  "$FUNCNAME" "$FUNCNAME REPO"
328  return
329 fi
330 # Error si se recibe más de 1 parámetro.
331 case $# in
332  0) REPO="REPO" ;;
333  1) REPO="$1" ;;
334  *) ogRaiseError $OG_ERR_FORMAT "$*"
335  return $? ;;
336 esac
337 # Comprobar tipo de repositorio.
338 DIR=$(ogGetPath "$REPO" / 2>/dev/null)
339 [ -n "$DIR" ] || ogRaiseError $OG_ERR_FORMAT "$1"
340 GROUP="$(ogGetGroupName)"
341 if [ -n "$GROUP" ]; then
342  mkdir -p "$DIR/groups/$GROUP" 2>/dev/null
343 fi
344 }
345 
function ogGetHostname()
Muestra el nombre del cliente. #.
Definition: Net.lib:135
function ogRaiseError()
Devuelve el mensaje y el código de error correspondiente. #.
Definition: System.lib:188
function mount()
Definition: ToolsGNU.c:121
function ogHelp()
Muestra mensaje de ayuda para una función determinda. #.
Definition: System.lib:153
function ogGetPath()
Inicia el proceso de arranque de un sistema de archivos. #.
Definition: File.lib:237
function ogGetGroupName()
Devuelve el nombre del grupo al que pertenece el cliente. #.
Definition: Net.lib:117
function ogGetNetInterface()
Muestra la interfaz de red del sistema #.
Definition: Net.lib:231
function umount()
Definition: ToolsGNU.c:143
function ogGetMacAddress()
Muestra la dirección Ethernet del cliente. #.
Definition: Net.lib:204
function ogGetServerIp()
Muestra la dirección IP del Servidor de OpenGnSys. #.
Definition: Net.lib:291
function ogGetRepoIp()
Muestra la dirección IP del repositorio de datos. #.
Definition: Net.lib:256
function ogEcho()
Muestra mensajes en consola y lo registra en fichero de incidencias. #.
Definition: System.lib:34
function awk()
Definition: ToolsGNU.c:89
function ogGetIpAddress()
Muestra la dirección IP del sistema #.
Definition: Net.lib:171