Tag Archives: sed

Travailler avec une mib snmp

Par exemple pour travailler avec une mib netapp :

snmptranslate -m NETWORK-APPLIANCE-MIB -Tl|less

Pour avoir la liste de tous les paramètres avec leurs “valeurs” :

.iso(1).org(3).dod(6).internet(1).mgmt(2).mib-2(1).interfaces(2).ifTable(2).ifEntry(1).ifOutErrors(20)
.iso(1).org(3).dod(6).internet(1).mgmt(2).mib-2(1).interfaces(2).ifTable(2).ifEntry(1).ifOutQLen(21)
.iso(1).org(3).dod(6).internet(1).mgmt(2).mib-2(1).interfaces(2).ifTable(2).ifEntry(1).ifSpecific(22)

Pour traduire ça avec une petite ligne :

echo '.iso(1).org(3).dod(6).internet(1).mgmt(2).mib-2(1).interfaces(2).ifTable(2).ifEntry(1).ifSpecific(22)' | perl -pe 's/\.[a-zA-Z-0-9]+\(/./g;s/\)//g'
.1.3.6.1.2.1.2.2.1.22

Et l’inverse :

snmptranslate -m NETWORK-APPLIANCE-MIB -Tl .1.3.6.1.4.1.789.1.7.3.1.1.5.0
NETWORK-APPLIANCE-MIB::cifsReads.0

sed #2

Comment utiliser sed pour remplacer ça / Howto replace this with sed:

ns01:x:11062:11062::/home/ns01:/bin/sh
:x:11062:11062::/home/ns01:/bin/sh

En ça / To this:

ns01:x:11062:11062:::0:0
:x:11062:11062:d::0:0

Taper les lignes de codes / use there command line:

(echo ns01:x:11062:11062::/home/ns01:/bin/sh; \
echo :x:11062:11062:d:/home/ns01:/bin/sh ) \
| sed -r 's/^(([^:]*:){5}).*/\1:0:0/'

Explications :
s/ pour lancer la recherche
-r pour passer en expression régulière avancée
^ pour commencer en début de chaine
[^:]*: pour chercher tout ce qu’il peut y avoir avant un “:” y compris rien
grouper [^:]*: entre des () suivit de {5} pour avoir 5 occurrences du pattern
le .*$ à la fin de la chaine pour l’enlever dans la substitution

/ pour passer à la partie de ce qu’il faut mettre à la place
\1 pour récupérer, la première partie qui match entre ()
Dans l’exemple

ns01:x:11062:11062::
:x:11062:11062:d:

et :0:0 pour l’ajouter à la fin de la chaine ;)

Explanation:
s/ start the search
-r use extended regular expressions
^ start on the strings beginning
[^:]*: search anything excluding “:” including nothing
group [^:]*: in () followed by {5} to have 5 instance of pattern
the .*$ at the end was here to remove the rest

/ to pass at the remplacement
\1 to get first matching part was betwen ()
In this exemple

ns01:x:11062:11062::
:x:11062:11062:d:

and :0:0 for add it at the end ;)

Maintenant, vous pouvez utiliser la commande sed / now you can use the sed command:

sed -r 's/^(([^:]*:){5}).*/\1:0:0/' /etc/passwd