source: client/engine/Net.lib @ b584da5

918-git-images-111dconfigfileconfigure-oglivegit-imageslgromero-new-oglivemainmaint-cronmount-efivarfsmultivmmultivm-ogboot-installerogClonningEngineogboot-installer-jenkinsoglive-ipv6test-python-scriptsticket-301ticket-50ticket-50-oldticket-577ticket-585ticket-611ticket-612ticket-693ticket-700ubu24tplunification2use-local-agent-oglivevarios-instalacionwebconsole3
Last change on this file since b584da5 was dc157ae, checked in by irina <irinagomez@…>, 9 years ago

#678 Unidades organizativas separadas: script de restauración

git-svn-id: https://opengnsys.es/svn/branches/version1.1@4755 a21b9725-9963-47de-94b9-378ad31fedc9

  • Property mode set to 100755
File size: 9.4 KB
RevLine 
[e0f3634]1#!/bin/bash
2#/**
3#@file     Net.lib
4#@brief    Librería o clase Net
5#@class    Net
6#@brief    Funciones básicas de red.
[aab6c6e]7#@version  1.0.6
[e0f3634]8#@warning  License: GNU GPLv3+
9#*/
10
11
12#/**
[fe6843b]13# ogChangeRepo  IPREPO [ OgUnit ]
14#@brief   Cambia el repositorio para el recurso remoto images.
15#@param 1 Ip Repositorio
16#@param 2 Abreviatura Unidad Organizativa
17#@return  Cambio recurso remoto en OGIMG.
18#@version 1.1 - Primera versión para OpenGnSys.
19#@author  Irina Gomez, ETSII Universidad de Sevilla
20#@date    2015-06-16
21#*/
22function ogChangeRepo {
23local SRCIMG
24
25if [ "$*" == "help" ]; then
26    ogHelp "$FUNCNAME" "$FUNCNAME IPREPO [ OgUnit ]" \
27           "$FUNCNAME 10.1.120.3" \
28           "$FUNCNAME 10.1.120.3 cdc"
29    return
30fi
31
32
33if [ $# -lt 1 ]; then
34    ogRaiseError $OG_ERR_FORMAT "$MSG_FORMAT: $FUNCNAME IPREPO [ OgUnit ]"
35    return $?
36fi
37
38REPO="${1^^}"
39# Si REPO tomamos el valor por defecto.
40[ "$REPO" == "REPO" ] && REPO=$ogrepo
41
[cead9e3]42OLDREPO=$(ogGetRepoIp)
43# Si $1 y $2 son el repositorio y la OU actual me salgo
44[ "$REPO" == "$OLDREPO" ] && [ "$2" == "$ogunit" ]  && return 0
45
[fe6843b]46source /scripts/functions
47source /scripts/ogfunctions
48umount $OGIMG
49[ "$2" == "" ] && SRCIMG="ogimages" || SRCIMG="ogimages/$2"
50eval $(grep "OPTIONS=" /scripts/ogfunctions)
51[ "$ogactiveadmin" == "true" ] && RW=",rw" || RW=",ro"
[cead9e3]52
[dc157ae]53ogEcho session log "$MSG_HELP_ogChangeRepo $REPO ${2%/}"
[fe6843b]54ogConnect $REPO $ogprotocol $SRCIMG $OGIMG $RW
55
[cead9e3]56# Si da error volvemos a montar el inicial
57if [ $? -ne 0 ]; then
58    ogConnect $OLDREPO $ogprotocol $SRCIMG $OGIMG $RW
59    ogRaiseError session $OG_ERR_REPO "$REPO"
60    return $?
61fi
62
[fe6843b]63}
64
65
66#/**
[fea70bf]67#         ogGetGroupDir [ str_repo ]
68#@brief   Devuelve el cmaino del directorio para el grupo del cliente.
69#@param   str_repo     repositorio de imágenes (opcional)
70#@return  path_dir  -  Camino al directorio del grupo.
71#@note    repo = { REPO, CACHE }     REPO por defecto
72#@exception OG_ERR_FORMAT    formato incorrecto.
73#@version 1.0.2 - Primera versión para OpenGnSys.
74#@author  Ramon Gomez, ETSII Universidad de Sevilla
75#@date    2011-10-03
76#*/
77function ogGetGroupDir ()
78{
79local REPO DIR GROUP
80if [ "$*" == "help" ]; then
81    ogHelp "$FUNCNAME" "$FUNCNAME str_repo" \
82           "$FUNCNAME REPO  ==>  /opt/opengnsys/images/groups/Grupo1"
83    return
84fi
85# Error si se recibe más de 1 parámetro.
86case $# in
87    0)  REPO="REPO" ;;
88    1)  REPO="$1" ;;
89    *)  ogRaiseError $OG_ERR_FORMAT "$*"
90        return $? ;;
91esac
92
93GROUP="$(ogGetGroupName)"
94if [ -n "$GROUP" ]; then
95    DIR=$(ogGetPath "$REPO" "/groups/$GROUP" 2>/dev/null)
96    [ -d "$DIR" ] && echo "$DIR"
97fi
98}
99
100
101#/**
102#         ogGetGroupName
103#@brief   Devuelve el nombre del grupo al que pertenece el cliente.
104#@return  str_group - Nombre de grupo.
105#@version 1.0.2 - Primera versión para OpenGnSys.
106#@author  Ramon Gomez, ETSII Universidad de Sevilla
107#@date    2011-10-03
108#*/
109function ogGetGroupName ()
110{
[e4dafd6]111if [ "$*" == "help" ]; then
[1cd64e6]112    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  =>  Grupo1"
[e4dafd6]113    return
114fi
[fea70bf]115[ -n "$group" ] && echo "$group"
116}
117
118
119#/**
[e0f3634]120#         ogGetHostname
121#@brief   Muestra el nombre del cliente.
122#@return  str_host - nombre de máquina
[fb8c54b]123#@version 0.10 - Integración en OpenGnSys 0.10
[e0f3634]124#@author  Ramon Gomez, ETSII Universidad de Sevilla
125#@date    2010-02-11
126#*/ ##
127function ogGetHostname ()
128{
129local HOST
[1cd64e6]130if [ "$*" == "help" ]; then
131    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  =>  pc1"
132    return
133fi
[0bfbbe1]134# Tomar nombre de la variable HOSTNAME
[e0f3634]135HOST="$HOSTNAME"
[0bfbbe1]136# Si no, tomar del DHCP, opción host-name       /* (comentario para Doxygen)
[e0f3634]137[ -z "$HOST" ] && HOST=$(awk -F\" '/option host-name/ {gsub(/;/,""); host=$2}
138                                   END {print host}
139                                  ' /var/lib/dhcp3/dhclient.leases)
[0bfbbe1]140# Si no, leer el parámetro del kernel hostname  (comentario para Doxygen) */
[e0f3634]141[ -z "$HOST" ] && HOST=$(awk 'BEGIN {RS=""; FS="="}
142                              $1~/hostname/ {print $2}' /proc/cmdline)
143[ "$HOSTNAME" != "$HOST" ] && export HOSTNAME="$HOST"
[fea70bf]144[ -n "$HOST" ] && echo $HOST
[e0f3634]145}
146
147
148#/**
149#         ogGetIpAddress
150#@brief   Muestra la dirección IP del sistema
151#@return  str_ip - Dirección IP
[fb8c54b]152#@version 0.10 - Integración en OpenGnSys 0.10
[e0f3634]153#@author  Ramon Gomez, ETSII Universidad de Sevilla
154#@date    2010-02-11
[8368c0d]155#@version 1.0 - Integración OpenGnSys 0.10 Opengnsys 0.10-testing
[bd27de2]156#@note    Usa las variables utilizadas por el initrd "/etc/net-ethX.conf
[8368c0d]157#@author  Antonio J. Doblas Viso. Universidad de Malaga.
158#@date    2011-02-24
[f48b8f9]159#@version 1.0.2 - Soporte para varias tarjetas de red
160#@author  Ramon Gomez, ETSII Universidad de Sevilla
161#@date    2011-06-17
[e0f3634]162#*/ ##
163function ogGetIpAddress ()
164{
165local IP
[1cd64e6]166if [ "$*" == "help" ]; then
167    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  =>  192.168.0.10"
168    return
169fi
[c6afc2d]170if [ -n "$IPV4ADDR" ]; then
171     IP="$IPV4ADDR"
[8368c0d]172else
[f48b8f9]173    # Obtener direcciones IP.
174    if [ -n "$DEVICE" ]; then
175        IP=$(ip -o address show up dev "$DEVICE" 2>/dev/null | awk '{if ($3~/inet$/) {printf ("%s ", $4)}}')
176    else
177        IP=$(ip -o address show up | awk '$2!~/lo/ {if ($3~/inet$/) {printf ("%s ", $4)}}')
178    fi
[8368c0d]179fi
[f48b8f9]180# Mostrar solo la primera.
[c6afc2d]181echo "${IP%%/*}"       # (comentario para Doxygen) */
[e0f3634]182}
183
184
185#/**
186#         ogGetMacAddress
187#@brief   Muestra la dirección Ethernet del cliente.
188#@return  str_ether - Dirección Ethernet
[fb8c54b]189#@version 0.10 - Integración en OpenGnSys 0.10
[e0f3634]190#@author  Ramon Gomez, ETSII Universidad de Sevilla
191#@date    2010-02-11
[f48b8f9]192#@version 1.0.2 - Soporte para varias tarjetas de red
193#@author  Ramon Gomez, ETSII Universidad de Sevilla
194#@date    2011-06-17
[e0f3634]195#*/ ##
196function ogGetMacAddress ()
197{
198local MAC
[1cd64e6]199if [ "$*" == "help" ]; then
200    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  =>  00:11:22:33:44:55"
201    return
202fi
[e0f3634]203# Obtener direcciones Ethernet.
[f48b8f9]204if [ -n "$DEVICE" ]; then
[336557d]205    MAC=$(ip -o link show up dev "$DEVICE" 2>/dev/null | awk '{sub (/.*\\/, ""); if ($1~/ether/) printf ("%s ", toupper($2));}')
[f48b8f9]206else
[336557d]207    MAC=$(ip -o link show up | awk '$2!~/lo/ {sub (/.*\\/, ""); if ($1~/ether/) printf ("%s ", toupper($2));}')
[f48b8f9]208fi
[e0f3634]209# Mostrar sólo la primera.
210echo ${MAC%% *}
211}
212
[fb8c54b]213
214#/**
[c6afc2d]215#         ogGetNetInterface
216#@brief   Muestra la interfaz de red del sistema
217#@return  str_interface - interfaz de red
218#@version 1.0 - Integración OpenGnSys 0.10 Opengnsys 0.10-testing
219#@note    Usa las variables utilizadas por el initrd "/etc/net-ethX.conf
220#@author  Antonio J. Doblas Viso. Universidad de Malaga.
221#@date    2011-02-24
222#*/ ##
223function ogGetNetInterface ()
224{
225if [ "$*" == "help" ]; then
226    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  =>  eth0"
227    return
228fi
229[ -n "$DEVICE" ] && echo "$DEVICE"
230}
231
232
233#/**
[fb8c54b]234#         ogGetRepoIp
235#@brief   Muestra la dirección IP del repositorio de datos.
236#@return  str_ip - Dirección IP
237#@version 0.10 - Integración en OpenGnSys 0.10
238#@author  Ramon Gomez, ETSII Universidad de Sevilla
239#@date    2011-01-13
[bd27de2]240#@version 1.0 - Integración OpenGnSys 0.10 Opengnsys 0.10-testing
241#@note   Comprobacion segun protocolo de conexion al Repo
242#@author  Antonio J. Doblas Viso. Universidad de Malaga.
243#@date    2011-02-24
[aab6c6e]244#@version 1.0.6 - Obtener datos del punto de montaje, evitando fallo si $ogprotocol está vacía.
245#@author  Ramon Gomez, ETSII Universidad de Sevilla
246#@date    2014-08-27
[fb8c54b]247#*/ ##
248function ogGetRepoIp ()
249{
[aab6c6e]250# Variables locales.
251local SOURCE FSTYPE
252
253# Mostrar ayuda.
[1cd64e6]254if [ "$*" == "help" ]; then
255    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  =>  192.168.0.2"
256    return
257fi
[aab6c6e]258
259# Obtener direcciones IP, según el tipo de montaje.
260eval $(findmnt -P -o SOURCE,FSTYPE $OGIMG)
261case "$FSTYPE" in
262    nfs)   echo "$SOURCE" | cut -f1 -d: ;;
263    cifs)  echo "$SOURCE" | cut -f3 -d/ ;;
264esac
[fb8c54b]265}
266
267
268#/**
269#         ogGetServerIp
270#@brief   Muestra la dirección IP del Servidor de OpenGnSys.
271#@return  str_ip - Dirección IP
272#@version 0.10 - Integración en OpenGnSys 0.10
273#@author  Ramon Gomez, ETSII Universidad de Sevilla
274#@date    2011-01-13
[bd27de2]275#@version 1.0 - Integración OpenGnSys 0.10 Opengnsys 0.10-testing
276#@note   Comprobacion segun protocolo de conexion al Repo
277#@author  Antonio J. Doblas Viso. Universidad de Malaga.
278#@date    2011-02-24
[aab6c6e]279#@version 1.0.6 - Obtener datos del punto de montaje, evitando fallo si $ogprotocol está vacía.
280#@author  Ramon Gomez, ETSII Universidad de Sevilla
281#@date    2014-08-27
[fb8c54b]282#*/ ##
283function ogGetServerIp ()
284{
[aab6c6e]285# Variables locales.
286local SOURCE FSTYPE
287
288# Mostrar ayuda.
[1cd64e6]289if [ "$*" == "help" ]; then
290    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  =>  192.168.0.2"
291    return
292fi
[aab6c6e]293
294# Obtener direcciones IP, según el tipo de montaje.
295eval $(findmnt -P -o SOURCE,FSTYPE $OPENGNSYS)
296case "$FSTYPE" in
297    nfs)   echo "$SOURCE" | cut -f1 -d: ;;
298    cifs)  echo "$SOURCE" | cut -f3 -d/ ;;
299esac
[fb8c54b]300}
301
[336557d]302
303#/**
304#         ogMakeGroupDir [ str_repo ]
305#@brief   Crea el directorio para el grupo del cliente.
306#@param   str_repo     repositorio de imágenes (opcional)
307#@return  (nada)
308#@note    repo = { REPO, CACHE }     REPO por defecto
309#@exception OG_ERR_FORMAT    formato incorrecto.
310#@version 1.0.5 - Primera versión para OpenGnSys.
311#@author  Ramon Gomez, ETSII Universidad de Sevilla
312#@date    2013-09-26
313#*/
314function ogMakeGroupDir ()
315{
316local REPO DIR GROUP
317if [ "$*" == "help" ]; then
318    ogHelp "$FUNCNAME" "$FUNCNAME str_repo" \
319           "$FUNCNAME" "$FUNCNAME REPO"
320    return
321fi
322# Error si se recibe más de 1 parámetro.
323case $# in
324    0)  REPO="REPO" ;;
325    1)  REPO="$1" ;;
326    *)  ogRaiseError $OG_ERR_FORMAT "$*"
327        return $? ;;
328esac
329# Comprobar tipo de repositorio.
330DIR=$(ogGetPath "$REPO" / 2>/dev/null)
331[ -n "$DIR" ] || ogRaiseError $OG_ERR_FORMAT "$1"
332GROUP="$(ogGetGroupName)"
333if [ -n "$GROUP" ]; then
334    mkdir -p "$DIR/groups/$GROUP" 2>/dev/null
335fi
336}
337
Note: See TracBrowser for help on using the repository browser.