1

I'm working on the following script:

!/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

The script takes the number of lines in a file called "dispositivos.csv"

Based on that goes through a for loop in wich it takes an IP address and connect via telnet to that IP.

Then, type "show version" save the output in a file

Using grep it takes the "System serial number" and add it to "dispositivos.csv" in its respective line using awk.

this is 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"

Everything goes well but the last part, in each iteration the output is like this:

 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

It does the job but it is printed just in the terminal, once the script finish if I check dispositivoss.csv nothing changes. like this:

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

I Know the problem is with awk. I would like to keep the SN in the same file. those are the tries I've done:

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

SOLVED:

awk -v "ln=$c" -v "sn=$SN" '{print (NR==ln) ? $0","sn : $0;}' dispositivoss.csv>dispositivosfinal.csv   
mv dispositivosfinal.csv dispositivoss.csv

Thanks.

  • Not an answer, but I am curious why you switch back and forth between awk 'stuff' file and awk 'stuff' < file. – DopeGhoti Mar 23 '17 at 18:14
2

As far as I understand your question, you wonder why your input file, dispositivoss.csv does not change. I suspect you think that awk's print command modifies the input file, but this is not the case.

I suggest trying something along these lines:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.