![서로 다른 두 파일의 데이터 일치](https://linux55.com/image/106060/%EC%84%9C%EB%A1%9C%20%EB%8B%A4%EB%A5%B8%20%EB%91%90%20%ED%8C%8C%EC%9D%BC%EC%9D%98%20%EB%8D%B0%EC%9D%B4%ED%84%B0%20%EC%9D%BC%EC%B9%98.png)
네트워크에 연결된 장치를 기록하기 위해 Expect
명령에서 정보를 가져와 Show CDP neighbors detail
특정 필드( Device Name, IP address, Interface and Port ID (outgoing port)
예: IP 주소는 제외)를 내 데이터베이스의 기본 키로 저장하는 스크립트를 작성하고 있습니다. 동일한 IP 주소가 동일한 장치임을 보장하지는 않습니다. 따라서 명령을 사용하여 각 장치의 S/N을 캡처해야 합니다 show inventory
. 두 명령의 출력을 별도의 텍스트 파일에 넣었으며 이제 정보를 일치시켜야 합니다.
파일 1:
Device ID: BIOTERIO
IP address: 000.000.00.001
Interface: GigabitEthernet1/0/6, Port ID (outgoing port): GigabitEthernet0/1
Device ID: N7K-LAN(JAF1651ANDL)
IP address: 000.000.0.002
Interface: GigabitEthernet1/0/1, Port ID (outgoing port): Ethernet7/23
Device ID: LAB_PESADO
--More-- IP address: 000.000.000.003
Interface: GigabitEthernet1/0/11, Port ID (outgoing port): GigabitEthernet0/1
IP address: 000.000.000.003
Device ID: Arquitectura_Salones
IP address: 000.000.000.04
Interface: GigabitEthernet1/0/9, Port ID (outgoing port): GigabitEthernet0/49
IP address: 000.000.000.04
Device ID: CIVIL_253
IP address: 000.000.000.005
Interface: GigabitEthernet1/0/4, Port ID (outgoing port): GigabitEthernet1/0/52
IP address: 000.000.000.005
Device ID: Arquitectura
IP address: 000.000.000.006
Interface: GigabitEthernet1/0/3, Port ID (outgoing port): GigabitEthernet0/1
IP address: 000.000.000.006
Device ID: ING_CIVIL_DIR
IP address: 000.000.0.007
Interface: GigabitEthernet1/0/10, Port ID (outgoing port): GigabitEthernet0/2
Device ID: ING_CIVIL
IP address: 000.000.000.008
Interface: GigabitEthernet1/0/7, Port ID (outgoing port): GigabitEthernet0/2
IP address: 000.000.000.008
Device ID: Ingenieria_Posgrado
IP address: 000.000.000.009
Interface: GigabitEthernet1/0/8, Port ID (outgoing port): GigabitEthernet0/1
IP address: 000.000.000.009
Device ID: Biblio_Barragan
IP address: 000.000.000.10
Interface: GigabitEthernet1/0/2, Port ID (outgoing port): GigabitEthernet0/1
IP address: 000.000.000.10
Device ID: Electronica_Edif_3
IP address: 000.000.000.011
Interface: GigabitEthernet1/0/5, Port ID (outgoing port): GigabitEthernet0/1
IP address: 000.000.000.011
]0;cesar@cesar-HP-Pavilion-15-NoteBook-PC: ~cesar@cesar-HP-Pavilion-15-NoteBook-PC:~$ awk '/Device ID|IP address|Interface|Port ID/ { print }' CDPdet.dat >tabladetallada.dat
파일 2:
show inventory
NAME: "1", DESCR: "WS-C3750G-12S"
PID: WS-C3750G-12S-E , VID: V06, SN: FDO1130Z9ZJ
NAME: "GigabitEthernet1/0/1", DESCR: "1000BaseLX SFP"
PID: , VID: , SN: H006K082
NAME: "GigabitEthernet1/0/2", DESCR: "10/100/1000BaseTX SFP"
PID: GLC-T , VID: , SN: 00000MTC1666080Z
NAME: "GigabitEthernet1/0/3", DESCR: "1000BaseLX SFP"
PID: , VID: , SN: H118K083
NAME: "GigabitEthernet1/0/4", DESCR: "1000BaseLX SFP"
PID: , VID: , SN: H006K041
NAME: "GigabitEthernet1/0/5", DESCR: "1000BaseSX SFP"
PID: , VID: , SN: FNS11140FLE
NAME: "GigabitEthernet1/0/6", DESCR: "1000BaseSX SFP"
PID: , VID: , SN: P7K06UQ
NAME: "GigabitEthernet1/0/7", DESCR: "1000BaseLX SFP"
PID: , VID: , SN: H006K052
NAME: "GigabitEthernet1/0/8", DESCR: "1000BaseLX SFP"
PID: , VID: , SN: H006K030
NAME: "GigabitEthernet1/0/9", DESCR: "1000BaseLX SFP"
PID: , VID: , SN: FNS14200533
NAME: "GigabitEthernet1/0/10", DESCR: "1000BaseLX SFP"
PID: GLC-LH-SMD , VID: V86, SN: FNS14201SG0
NAME: "GigabitEthernet1/0/11", DESCR: "1000BaseLX SFP"
PID: GLC-LH-SMD , VID: V86, SN: FNS174202FT
NAME: "GigabitEthernet1/0/12", DESCR: "1000BaseLX SFP"
PID: GLC-LH-SMD , VID: V86, SN: FNS184203FS
Barragan_3750>
다음과 같이 올바른 순서로 동일한 파일의 모든 정보를 갖고 싶습니다.
Device ID: BIOTERIO
IP address: 000.000.00.001
Interface: GigabitEthernet1/0/6
Port ID (outgoing port): GigabitEthernet0/1
SN: P7K06UQ
Device ID: N7K-LAN(JAF1651ANDL)
IP address: 000.000.0.002
Interface: GigabitEthernet1/0/1
Port ID (outgoing port): Ethernet7/23
SN: H006K082
Device ID: LAB_PESADO
IP address: 000.000.000.003
Interface: GigabitEthernet1/0/11
Port ID (outgoing port): GigabitEthernet0/1
SN: FNS174202FT
...
etc
grep, awk 또는 Perl을 사용하여 이를 수행하는 방법을 아는 사람이 있습니까?
답변1
#!/bin/bash
FILE1=file1
FILE2=file2
while read line; do
if [[ $line == Device\ ID* ]]; then
echo -e "\n$line"
expectIp=1
elif [[ $line == *IP\ address* && $expectIp -eq 1 ]]; then
sed 's/^\s*//' <<< $line
unset expectIp
elif [[ $line == Interface* ]]; then
interface=$(echo $line | grep -o 'Interface[^,]*,' | sed 's/,//')
echo $interface
echo $line | grep -o 'Port ID.*'
justInterface=$(echo $interface | sed 's/Interface: //')
grep -A1 \"$justInterface\" $FILE2 | grep -o 'SN: .*'
fi
done < $FILE1