我正在编写以下脚本:
!/bin/bash
FILE1=dispositivoss.csv
NUMERODISP="$(wc -l $FILE1 | awk '{print $1}')"
for (( c=2; c<=$NUMERODISP; c++ ))
do
IP=$(awk -vnum="$c" 'NR == num { print $NF }' dispositivoss.csv)
expect -f conexionindividual.sh $IP
SN=$(grep "System serial number" SN_$IP.dat | sed 's/^.*: //')
awk -v "ln=$c" -v "sn=$SN" '{print (NR==ln) ? $0","sn : $0;}' <dispositivoss.csv
done
该脚本获取名为“dispositivos.csv”的文件中的行数
在此基础上,通过一个 for 循环,获取一个 IP 地址并通过 telnet 连接到该 IP。
然后,输入“show version”将输出保存在文件中
使用 grep 获取“系统序列号”并使用 awk 将其添加到相应行中的“dispositivos.csv”中。
这是 conexionindividual.sh:
#!/usr/bin/expect -f
set Username "user"
set Password "password"
set IP [lindex $argv 0];
spawn ssh -p 22 cesar@localhost
expect "*?"
send "yes\r"
expect "password: "
send "mycomputerpass\r"
expect "$ "
send "telnet $IP\r"
expect "Username:"
send "$Username\r"
expect "Password: "
send "$Password\r"
expect "*>"
send "show version\r"
log_file -noappend SN_$IP.dat;
expect -ex "--More--" {send -- " "; exp_continue}
expect "*>"
log_file;
expect "*>"
send "exit\r"
一切都很顺利,但最后一部分,在每次迭代中,输出如下:
telnet 148.000.000.253
Trying 148.000.000.253...
Connected to 148.000.000.253.
Escape character is '^]'.
DCyTIC BUAP Solo Usuarios Autorizados. Usuario: user
Password:
Ingenieria_Posgrado>show version
Cisco IOS Software, C2960 Software (C2960-LANBASE-M), Version 12.2(25)SEE3, RELEASE SOFTWARE (fc2)
Copyright (c) 1986-2007 by Cisco Systems, Inc.
Compiled Thu 22-Feb-07 13:57 by myl
Image text-base: 0x00003000, data-base: 0x00AA3380
ROM: Bootstrap program is C2960 boot loader
BOOTLDR: C2960 Boot Loader (C2960-HBOOT-M) Version 12.2(25r)SEE1, RELEASE SOFTWARE (fc1)
Ingenieria_Posgrado uptime is 19 weeks, 4 days, 20 hours, 8 minutes
System returned to ROM by power-on
System restarted at 14:49:42 gmt Sat Nov 5 2016
System image file is "flash:c2960-lanbase-mz.122-25.SEE3/c2960-lanbase-mz.122-25.SEE3.bin"
cisco WS-C2960-24TC-L (PowerPC405) processor (revision E0) with 61440K/4088K bytes of memory.
Processor board ID FOC1128W4DD
Last reset from power-on
1 Virtual Ethernet interface
24 FastEthernet interfaces
2 Gigabit Ethernet interfaces
The password-recovery mechanism is enabled.
64K bytes of flash-simulated non-volatile configuration memory.
Base ethernet MAC Address : 00:1C:F9:AA:52:00
Motherboard assembly number : 73-9832-07
Power supply part number : 341-0097-02
Motherboard serial number : FOC1128255N
Power supply serial number : AZS1128084Q
Model revision number : E0
Motherboard revision number : A0
Model number : WS-C2960-24TC-L
System serial number : FOC1128W4DD # HERE WE HAVE THE SN
Top Assembly Part Number : 800-26671-03
Top Assembly Revision Number : A0
Version ID : V03
CLEI Code Number : COM3K00BRB
Hardware Board Revision Number : 0x01
Switch Ports Model SW Version SW Image
------ ----- ----- ---------- ----------
* 1 26 WS-C2960-24TC-L 12.2(25)SEE3 C2960-LANBASE-M
Configuration register is 0xF
Ingenieria_Posgrado>device_id,ip_address
BIOTERIO, 148.000.00.189 #this is dispositivoss.csv
N7K-LAN(JAF1651ANDL), 148.000.0.192
LAB_PESADO, 148.000.000.130
Arquitectura_Salones, 148.000.000.61
CIVIL_253, 148.000.000.253
Arquitectura, 148.000.000.253
ING_CIVIL, 148.000.000.251
ING_CIVIL_DIR, 148.000.0.188
Ingenieria_Posgrado, 148.000.000.253,FOC1128W4DD #HERE IT IS
Biblio_Barragan, 148.000.000.61
Electronica_Edif_3, 148.000.000.253
spawn ssh -p 22 cesar@localhost
cesar@localhost's password:
Permission denied, please try again.
cesar@localhost's password:
Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 3.13.0-108-generic x86_64)
* Documentation: https://help.ubuntu.com/
206 packages can be updated.
119 updates are security updates.
New release '16.04.2 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
Last login: Thu Mar 23 11:00:39 2017 from localhost
它完成了这项工作,但它只是在终端中打印,一旦脚本完成,如果我检查 dispositivoss.csv 没有任何变化。像这样:
device_id,ip_address
BIOTERIO, 148.000.00.189
N7K-LAN(JAF1651ANDL), 148.000.0.192
LAB_PESADO, 148.000.000.130
Arquitectura_Salones, 148.000.000.61
CIVIL_253, 148.000.000.253
Arquitectura, 148.000.000.253
ING_CIVIL, 148.000.000.251
ING_CIVIL_DIR, 148.000.0.188
Ingenieria_Posgrado, 148.000.000.253
Biblio_Barragan, 148.000.000.61
Electronica_Edif_3, 148.000.000.253
我知道问题出在 awk 上。我想将 SN 保留在同一个文件中。这些是我所做的尝试:
awk -v "ln=$c" -v "sn=$SN" '{print (NR==ln) ? $0","sn : $0;}' dispositivoss.cvs
awk -v "ln=$c" -v "sn=$SN" '{print (NR==ln) ? $0","sn : $0;}' dispositivoss.cvs>dispositivosfinal.csv
awk -v "ln=$c" -v "sn=$SN" '{print (NR==ln) ? $0","sn : $0;}' dispositivoss.cvs
awk -v "ln=$c" -v "sn=$SN" '{print (NR==ln) ? $0","sn : $0;}' dispositivosfinal.csv<dispositivoss.cvs
awk -v "ln=$c" -v "sn=$SN" '{print (NR==ln) ? $0","sn : $0;}'<<dispositivoss.cvs
解决了:
awk -v "ln=$c" -v "sn=$SN" '{print (NR==ln) ? $0","sn : $0;}' dispositivoss.csv>dispositivosfinal.csv
mv dispositivosfinal.csv dispositivoss.csv
谢谢。
答案1
据我了解你的问题,你想知道为什么你的输入文件dispositivoss.csv
没有改变。我怀疑您认为awk
'sprint
命令修改了输入文件,但事实并非如此。
我建议尝试以下方法:
for IP in $(awk 'NR > 1 { print $NF }' dispositivoss.csv)
do expect -f conexionindividual.sh $IP
done | awk 'BEGIN { print } /System serial number/ { print $NF }' > temp_file
paste -d, dispositivoss.csv temp_file