-1

I want to read ip from IP Pool file and get whois from it and then report it to CSV spreadsheet file. I wrote this script to perform that:

#!/bin/bash
echo "ip,netname,org-name,remarks,descr,country,person,address,phone,origin" > csv
while read -r ip
do
whois $ip > whoisip
netname= cat whoisip | grep "netname"
orgname= cat whoisip | grep "org-name"
remarks= cat whoisip | grep "remarks"
descr= cat whoisip | grep "descr"
country= cat whoisip | grep "Country"
person= cat whoisip | grep "person"
address= cat whoisip | grep "address"
phone= cat whoisip | grep "phone"
origin= cat whoisip | grep "origin"
echo $ip,$netname,$orgname,$remarks,$descr,$country,$person,$address,$phone,$origin >> csv
done <pool

BUT, my script generate this CSV file:

ip,netname,org-name,remarks,descr,country,person,address,phone,origin
x.x.x.x,,,,,,,,
y.y.y.y,,,,,,,,
z.z.z.z,,,,,,,,
...

Why the second values are empty?

mbzadegan
  • 71
  • 1
  • 1
  • 4
  • "var=" cannot be followed by a space. 'var=cat whoisip | grep "netname"' would *attempt* to set var equal to a string, not the output of grep. Use NETNAME="$(grep netname whoisip)", for instance, instead. Note that variable names should be capitalized and the output of grep should be quoted. See https://www.shellcheck.net/ for several other pointers. – Wayne Vosberg Jul 15 '17 at 05:15
  • I replaced NETNAME="$(grep netname whoisip)" but still second values are empty! – mbzadegan Jul 15 '17 at 05:23
  • `netname` != `NETNAME` – Cyrus Jul 15 '17 at 05:38
  • Thanks all, My problem was solved by shellcheck.net. – mbzadegan Jul 15 '17 at 06:01

1 Answers1

0

I tried to fix your script:

#!/bin/bash
echo "ip,netname,org-name,remarks,descr,country,person,address,phone,origin" > csv
while read -r ip
do
    whois $ip > whoisip
    netname=`cat whoisip | grep -i "netname"`
    orgname=`cat whoisip | grep -i "org-name"`
    remarks=`cat whoisip | grep -i "remarks"`
    descr=`cat whoisip | grep -i "descr"`
    country=`cat whoisip | grep -i "Country"`
    person=`cat whoisip | grep -i "person"`
    address=`cat whoisip | grep -i "address"`
    phone=`cat whoisip | grep -i "phone"`
    origin=`cat whoisip | grep -i "origin"`
    echo $ip,$netname,$orgname,$remarks,$descr,$country,$person,$address,$phone,$origin >> csv
done <pool

`` executes the enclosed commando and returns the output

-i makes grep case insensitive

Between variable name, equal sign and value in variable declaration and/or assignment no space is allowed:

var=value  #Correct -> var has the value value
var= value #Incorrect -> var is empty