source: client/engine/Net.lib @ 0a735488

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 0a735488 was aab6c6e, checked in by ramon <ramongomez@…>, 11 years ago

#541 #645: Evitar fallos de detección en funciones ogGetRepoIp y ogGetServerIp cuando no se ha dado un valor a la variable ogprotocol.

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

  • Property mode set to 100755
File size: 8.0 KB
Line 
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.
7#@version  1.0.6
8#@warning  License: GNU GPLv3+
9#*/
10
11
12#/**
13#         ogGetGroupDir [ str_repo ]
14#@brief   Devuelve el cmaino del directorio para el grupo del cliente.
15#@param   str_repo     repositorio de imágenes (opcional)
16#@return  path_dir  -  Camino al directorio del grupo.
17#@note    repo = { REPO, CACHE }     REPO por defecto
18#@exception OG_ERR_FORMAT    formato incorrecto.
19#@version 1.0.2 - Primera versión para OpenGnSys.
20#@author  Ramon Gomez, ETSII Universidad de Sevilla
21#@date    2011-10-03
22#*/
23function ogGetGroupDir ()
24{
25local REPO DIR GROUP
26if [ "$*" == "help" ]; then
27    ogHelp "$FUNCNAME" "$FUNCNAME str_repo" \
28           "$FUNCNAME REPO  ==>  /opt/opengnsys/images/groups/Grupo1"
29    return
30fi
31# Error si se recibe más de 1 parámetro.
32case $# in
33    0)  REPO="REPO" ;;
34    1)  REPO="$1" ;;
35    *)  ogRaiseError $OG_ERR_FORMAT "$*"
36        return $? ;;
37esac
38
39GROUP="$(ogGetGroupName)"
40if [ -n "$GROUP" ]; then
41    DIR=$(ogGetPath "$REPO" "/groups/$GROUP" 2>/dev/null)
42    [ -d "$DIR" ] && echo "$DIR"
43fi
44}
45
46
47#/**
48#         ogGetGroupName
49#@brief   Devuelve el nombre del grupo al que pertenece el cliente.
50#@return  str_group - Nombre de grupo.
51#@version 1.0.2 - Primera versión para OpenGnSys.
52#@author  Ramon Gomez, ETSII Universidad de Sevilla
53#@date    2011-10-03
54#*/
55function ogGetGroupName ()
56{
57if [ "$*" == "help" ]; then
58    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  =>  Grupo1"
59    return
60fi
61[ -n "$group" ] && echo "$group"
62}
63
64
65#/**
66#         ogGetHostname
67#@brief   Muestra el nombre del cliente.
68#@return  str_host - nombre de máquina
69#@version 0.10 - Integración en OpenGnSys 0.10
70#@author  Ramon Gomez, ETSII Universidad de Sevilla
71#@date    2010-02-11
72#*/ ##
73function ogGetHostname ()
74{
75local HOST
76if [ "$*" == "help" ]; then
77    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  =>  pc1"
78    return
79fi
80# Tomar nombre de la variable HOSTNAME
81HOST="$HOSTNAME"
82# Si no, tomar del DHCP, opción host-name       /* (comentario para Doxygen)
83[ -z "$HOST" ] && HOST=$(awk -F\" '/option host-name/ {gsub(/;/,""); host=$2}
84                                   END {print host}
85                                  ' /var/lib/dhcp3/dhclient.leases)
86# Si no, leer el parámetro del kernel hostname  (comentario para Doxygen) */
87[ -z "$HOST" ] && HOST=$(awk 'BEGIN {RS=""; FS="="}
88                              $1~/hostname/ {print $2}' /proc/cmdline)
89[ "$HOSTNAME" != "$HOST" ] && export HOSTNAME="$HOST"
90[ -n "$HOST" ] && echo $HOST
91}
92
93
94#/**
95#         ogGetIpAddress
96#@brief   Muestra la dirección IP del sistema
97#@return  str_ip - Dirección IP
98#@version 0.10 - Integración en OpenGnSys 0.10
99#@author  Ramon Gomez, ETSII Universidad de Sevilla
100#@date    2010-02-11
101#@version 1.0 - Integración OpenGnSys 0.10 Opengnsys 0.10-testing
102#@note    Usa las variables utilizadas por el initrd "/etc/net-ethX.conf
103#@author  Antonio J. Doblas Viso. Universidad de Malaga.
104#@date    2011-02-24
105#@version 1.0.2 - Soporte para varias tarjetas de red
106#@author  Ramon Gomez, ETSII Universidad de Sevilla
107#@date    2011-06-17
108#*/ ##
109function ogGetIpAddress ()
110{
111local IP
112if [ "$*" == "help" ]; then
113    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  =>  192.168.0.10"
114    return
115fi
116if [ -n "$IPV4ADDR" ]; then
117     IP="$IPV4ADDR"
118else
119    # Obtener direcciones IP.
120    if [ -n "$DEVICE" ]; then
121        IP=$(ip -o address show up dev "$DEVICE" 2>/dev/null | awk '{if ($3~/inet$/) {printf ("%s ", $4)}}')
122    else
123        IP=$(ip -o address show up | awk '$2!~/lo/ {if ($3~/inet$/) {printf ("%s ", $4)}}')
124    fi
125fi
126# Mostrar solo la primera.
127echo "${IP%%/*}"       # (comentario para Doxygen) */
128}
129
130
131#/**
132#         ogGetMacAddress
133#@brief   Muestra la dirección Ethernet del cliente.
134#@return  str_ether - Dirección Ethernet
135#@version 0.10 - Integración en OpenGnSys 0.10
136#@author  Ramon Gomez, ETSII Universidad de Sevilla
137#@date    2010-02-11
138#@version 1.0.2 - Soporte para varias tarjetas de red
139#@author  Ramon Gomez, ETSII Universidad de Sevilla
140#@date    2011-06-17
141#*/ ##
142function ogGetMacAddress ()
143{
144local MAC
145if [ "$*" == "help" ]; then
146    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  =>  00:11:22:33:44:55"
147    return
148fi
149# Obtener direcciones Ethernet.
150if [ -n "$DEVICE" ]; then
151    MAC=$(ip -o link show up dev "$DEVICE" 2>/dev/null | awk '{sub (/.*\\/, ""); if ($1~/ether/) printf ("%s ", toupper($2));}')
152else
153    MAC=$(ip -o link show up | awk '$2!~/lo/ {sub (/.*\\/, ""); if ($1~/ether/) printf ("%s ", toupper($2));}')
154fi
155# Mostrar sólo la primera.
156echo ${MAC%% *}
157}
158
159
160#/**
161#         ogGetNetInterface
162#@brief   Muestra la interfaz de red del sistema
163#@return  str_interface - interfaz de red
164#@version 1.0 - Integración OpenGnSys 0.10 Opengnsys 0.10-testing
165#@note    Usa las variables utilizadas por el initrd "/etc/net-ethX.conf
166#@author  Antonio J. Doblas Viso. Universidad de Malaga.
167#@date    2011-02-24
168#*/ ##
169function ogGetNetInterface ()
170{
171if [ "$*" == "help" ]; then
172    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  =>  eth0"
173    return
174fi
175[ -n "$DEVICE" ] && echo "$DEVICE"
176}
177
178
179#/**
180#         ogGetRepoIp
181#@brief   Muestra la dirección IP del repositorio de datos.
182#@return  str_ip - Dirección IP
183#@version 0.10 - Integración en OpenGnSys 0.10
184#@author  Ramon Gomez, ETSII Universidad de Sevilla
185#@date    2011-01-13
186#@version 1.0 - Integración OpenGnSys 0.10 Opengnsys 0.10-testing
187#@note   Comprobacion segun protocolo de conexion al Repo
188#@author  Antonio J. Doblas Viso. Universidad de Malaga.
189#@date    2011-02-24
190#@version 1.0.6 - Obtener datos del punto de montaje, evitando fallo si $ogprotocol está vacía.
191#@author  Ramon Gomez, ETSII Universidad de Sevilla
192#@date    2014-08-27
193#*/ ##
194function ogGetRepoIp ()
195{
196# Variables locales.
197local SOURCE FSTYPE
198
199# Mostrar ayuda.
200if [ "$*" == "help" ]; then
201    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  =>  192.168.0.2"
202    return
203fi
204
205# Obtener direcciones IP, según el tipo de montaje.
206eval $(findmnt -P -o SOURCE,FSTYPE $OGIMG)
207case "$FSTYPE" in
208    nfs)   echo "$SOURCE" | cut -f1 -d: ;;
209    cifs)  echo "$SOURCE" | cut -f3 -d/ ;;
210esac
211}
212
213
214#/**
215#         ogGetServerIp
216#@brief   Muestra la dirección IP del Servidor de OpenGnSys.
217#@return  str_ip - Dirección IP
218#@version 0.10 - Integración en OpenGnSys 0.10
219#@author  Ramon Gomez, ETSII Universidad de Sevilla
220#@date    2011-01-13
221#@version 1.0 - Integración OpenGnSys 0.10 Opengnsys 0.10-testing
222#@note   Comprobacion segun protocolo de conexion al Repo
223#@author  Antonio J. Doblas Viso. Universidad de Malaga.
224#@date    2011-02-24
225#@version 1.0.6 - Obtener datos del punto de montaje, evitando fallo si $ogprotocol está vacía.
226#@author  Ramon Gomez, ETSII Universidad de Sevilla
227#@date    2014-08-27
228#*/ ##
229function ogGetServerIp ()
230{
231# Variables locales.
232local SOURCE FSTYPE
233
234# Mostrar ayuda.
235if [ "$*" == "help" ]; then
236    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  =>  192.168.0.2"
237    return
238fi
239
240# Obtener direcciones IP, según el tipo de montaje.
241eval $(findmnt -P -o SOURCE,FSTYPE $OPENGNSYS)
242case "$FSTYPE" in
243    nfs)   echo "$SOURCE" | cut -f1 -d: ;;
244    cifs)  echo "$SOURCE" | cut -f3 -d/ ;;
245esac
246}
247
248
249#/**
250#         ogMakeGroupDir [ str_repo ]
251#@brief   Crea el directorio para el grupo del cliente.
252#@param   str_repo     repositorio de imágenes (opcional)
253#@return  (nada)
254#@note    repo = { REPO, CACHE }     REPO por defecto
255#@exception OG_ERR_FORMAT    formato incorrecto.
256#@version 1.0.5 - Primera versión para OpenGnSys.
257#@author  Ramon Gomez, ETSII Universidad de Sevilla
258#@date    2013-09-26
259#*/
260function ogMakeGroupDir ()
261{
262local REPO DIR GROUP
263if [ "$*" == "help" ]; then
264    ogHelp "$FUNCNAME" "$FUNCNAME str_repo" \
265           "$FUNCNAME" "$FUNCNAME REPO"
266    return
267fi
268# Error si se recibe más de 1 parámetro.
269case $# in
270    0)  REPO="REPO" ;;
271    1)  REPO="$1" ;;
272    *)  ogRaiseError $OG_ERR_FORMAT "$*"
273        return $? ;;
274esac
275# Comprobar tipo de repositorio.
276DIR=$(ogGetPath "$REPO" / 2>/dev/null)
277[ -n "$DIR" ] || ogRaiseError $OG_ERR_FORMAT "$1"
278GROUP="$(ogGetGroupName)"
279if [ -n "$GROUP" ]; then
280    mkdir -p "$DIR/groups/$GROUP" 2>/dev/null
281fi
282}
283
Note: See TracBrowser for help on using the repository browser.