Guif If
Guif If

Reputation: 595

Need to write result to variable

I need to write the result of this continuos output to a file but don't show results with this command:

tcpdump -lni eth0 -vvv -s 0 port bootps | grep --line-buffered -E -i 'requested-ip|client-id|hostname' | awk '{print $NF}' | sed 's/"//g' 

only output results with this command (without the 'sed'):

tcpdump -lni eth0 -vvv -s 0 port bootps | grep --line-buffered -E -i 'requested-ip|client-id|hostname' | awk '{print $NF}'

And is it possible to do any loop to store and output the 3 variables when they appear?

echo "${requested-ip} | ${client-id} | ${hostname}"

The script is:

tcpdump -lni eth0 -vvv -s 0 port bootps | grep --line-buffered -E -i 'requested-ip|client-id|hostname' | awk '{print $NF}' | sed 's/"//g' | while read b; do
requested-ip=`echo $b | head -1`
cliend-id=`echo $b | tail -2 | head -1`
hostname=`echo $b | tail -2 | tail -1`
echo "${requested-ip} | ${client-id} | ${hostname}"
done

and no output with this. Don't understand because added the while and the sed can't run it

This is the complete output without piped grep and awk:

[root@PIDORA ~]# tcpdump -lni eth0 -vvv -s 0 port bootps
tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
20:49:36.019930 IP (tos 0x0, ttl 255, id 22335, offset 0, flags [none], proto UDP (17), length 328)
    0.0.0.0.bootpc > 255.255.255.255.bootps: [udp sum ok] BOOTP/DHCP, Request from 98:9e:63:57:9d:1e, length 300, xid 0xfefa864b, Flags [none] (0x0000)
      Client-Ethernet-Address 98:9e:63:57:8d:1e
      Vendor-rfc1048 Extensions
        Magic Cookie 0x63825363
        DHCP-Message Option 53, length 1: Request
        Parameter-Request Option 55, length 7: 
          Subnet-Mask, Classless-Static-Route, Default-Gateway, Domain-Name-Server
          Domain-Name, Option 119, Option 252
        MSZ Option 57, length 2: 1500
        Client-ID Option 61, length 7: ether 98:9e:63:57:8d:1e
        Requested-IP Option 50, length 4: 10.0.1.16
        Lease-Time Option 51, length 4: 7776000
        Hostname Option 12, length 7: "iGPhone"
        END Option 255, length 0
        PAD Option 0, length 0, occurs 13

Upvotes: 0

Views: 131

Answers (1)

Bsquare ℬℬ
Bsquare ℬℬ

Reputation: 4497

I edited my answer after you posted your complete tcpdump output.

Notice there are several issues in your initial script:

  • you can't have '-' in your GNU/Bash variable name
  • you wrote cliend-id (with a 'd' at end of client), and client-id in another line

This is a working way to reach your needs:

#!/bin/bash

for rawInfo in $( tcpdump -lni eth0 -vvv -s 0 port bootps | grep --line-buffered -E -i 'requested-ip|client-id|hostname' |sort | awk '{print $NF}' | sed 's/"//g;s/$/,/' |tr -d '\n' ); do
  requestedip=$( echo "$rawInfo" |awk -F ',' '{print $1}' )
  hostname=$( echo "$rawInfo" |awk -F ',' '{print $2}' )
  clientid=$( echo "$rawInfo" |awk -F ',' '{print $3}' )
  echo "$requestedip | $clientid | $hostname"
done

Notice: If ever you can have a ',' in one of the value (should not ...), we should use another separator.

Let me know if you need additional explanations.

Upvotes: 1

Related Questions