Hoping for some input on this, as I'm struggling. I have a csv that contains an IP mask in which I want to get the network IP and broadcast IP.
So for instance I want the input field 1.0.0.0/24 to output 2 new field that contain the vaules: 1.0.0.0 in one and 1.0.0.255 in the other.
I have code to do this.
For broadcast:
for i in $(cat geoip.csv);do bcaddr=$(ipcalc -n -b $i);echo
${bcaddr#BROADCAST=};done
And for network:
for i in $(cat geoip.csv);do bcaddr=$(ipcalc -n -4 $i);echo
${bcaddr#BROADCAST=};done
Where do I go from here? How do I go about appending these 2 new fields to a new output file?
Thanks in advance!
see if this works on RHEL 5.x... do you want the output in csv with comma separator?
for i in $(cat csip.csv); do echo "$i $(ipcalc -n -b $i | grep -E "Address|Broadcast" | awk {'print ","$2'} | tr '\n' ' ')" ; done > new.csv
For your version of ipcalc, please try this:
for i in $(cat csip.csv); do echo "$i $(ipcalc -n -b $i | grep -E "NETWORK|BROADCAST" | awk -F= {'print ","$2'} | tr '\n' ' ')" ; done > new.csv
Why not using awk?
If you are using Linux (Not Unix):
for i in $(cat geoip.csv);do bcaddr=$(ipcalc -n -b $i); echo $bcaddr | egrep -o '([0-9]+\.){3}[0-9]+' ; done > ip.txt
Output:
1.0.0.255
1.0.0.0
If you want the output in a single line:
for i in $(cat geoip.csv);do bcaddr=$(ipcalc -n -b $i); echo $bcaddr | egrep -o '([0-9]+\.){3}[0-9]+' ORS=' ' ; done}' > ip.txt
Output:
1.0.0.255 1.0.0.0
Related
I have 2 files
File 1 - IN.txt
08:43:22 IN 0xabc
08:43:31 IN 0xdef
08:54:45 IN 0xghi
08:54:45 IN 0xjkl
File 2 - OUT.txt
08:43:32 OUT 0xdef
08:54:45 OUT 0xghi
08:54:45 OUT 0xjkl
Basically I am troubleshooting a network issue, IN.txt is packets coming in, OUT.txt is packets going out and column 3 is the packet code so it should match for the packet in the same transaction.
I want to know all IN packets that do not have a matching OUT packet.
Desired output:
08:43:22 IN 0xabc
#!/bin/bash
IN=$(awk -F " " '{print $3}' in.txt)
OUT=$(awk -F " " '{print $3}' out.txt)
for i in $IN
do
flag=false
for o in $OUT
do
if [[ "$i" == "$o" ]]; then
flag=true
break
fi
done
if [[ $flag == false ]]; then
echo "Cannot find packet: $i in out"
fi
done
Result:
dingrui#gdcni:~/onie$ ./filter.sh
Cannot find packet: 0xabc in out
you can use a for.
for i in $(cat IN.txt| awk '{print $3}'); do grep -i $i OUT.txt | wc -l; done
Or more readable:
for i in $(cat IN.txt| awk '{print $3}'); do result=$(grep -i $i OUT.txt | wc -l);echo $i "|" $result; done
OUTPUT:
0xabc | 0
0xdef | 1
0xghi | 1
0xjkl | 1
NOTE: Only matches the packets, I didn't look at the time which doesn't seem important since you want to check packets
You can use fgrep for this:
$ cut -d' ' -f3 < OUT.txt > OUT.txt2
$ fgrep -v IN.txt -f OUT.txt2
08:43:22 IN 0xabc
I need some Specs and Data from Number of Latops in a ini-file.
When I try to get the IP-Adress with ip addr list <interface> nothing happens in the script. Not even a Error.
I try to quote stuff in my code. Nothing change.
for w2 in /sys/class/net/wl*
do
w2i=$(basename $w2)
echo $w2i
addr=$(ip -o -4 addr list $w2i | awk '{print $4}' | cut -d/ -f1)
echo $addr
echo -e "$w2i=$addr"
done
I Think maybe it is because of the Varibales but this fails also (interface is set 'manually'):
for w2 in /sys/class/net/wl*
do
w2i=$(basename $w2)
echo $w2i
addr=$(ip -o -4 addr list wlp3s0 | awk '{print $4}' | cut -d/ -f1)
echo $addr
echo -e "$w2i=$addr"
done
When i run the script i get wlp3s0 and i must close the script with ctl-c. When i run ip -o -4 addr list wlp3s0 | awk '{print $4}' | cut -d/ -f1 it gives me my ip like i expected.
EDIT
I just need to wirte the full-path of the ip-command. Can somebody explain why i have to do this? And especially it is not necessary for cut or awk that also based in /usr/bin/?
for w2 in /sys/class/net/wl*
do
w2i=$(basename $w2)
echo $w2i
addr=$(/usr/bin/ip -o -4 addr list "$w2i" | awk '{print $4}' | cut -d/ -f1)
echo $addr
echo -e "$w2i=$addr"
done
}
I'd like to change my script(s) so that the command output is separated by a comma and is all on one line per host. Any ideas on how I can achieve this:
1st Script:
#!/bin/bash
for i in `cat ~/hostnames.txt`
do
ssh -q $i 'bash -s' < server_info.sh
done
2nd Script (server_info.sh):
#!/bin/bash
echo -n "Name: "
uname -n
echo -n "CPUs: "
cat /proc/cpuinfo* | grep processor | wc -l
echo -n "Memory (kb): "
cat /proc/meminfo | grep MemTotal: | awk '{print $2}'
echo -n "Current Kernel: "
uname -a | awk '{print $3}'
echo -n "IP: "
hostname -i
echo -e
Changing your 1st script:
#!/bin/bash
for i in cat ~/hostnames.txt
do
ssh -q $i 'bash -s' < server_info.sh
done | awk -v RS= '{$1=$1}1'
Note: Your server_info.sh can be a lot more optimized.For example:
cat /proc/meminfo | grep MemTotal: | awk '{print $2}'
could be changed to:
awk '/MemTotal:/{print $2}' /proc/meminfo
I tried:
here is content of file.txt
some other text
#1.something1=kjfk
#2.something2=dfkjdk
#3.something3=3232
some other text
bash script:
ids=( `grep "something" file.txt | cut -d'.' -f1` )
for id in "${ids[#]}"; do
echo $id
done
result:
(nothing newline...)
(nothing newline...)
(nothing newline...)
but all it prints is nothing like newline for every such id found what am i missing?
Your grep and cut should be working but you can use awk and reduce 2 commands into one:
while read -r id;
echo "$id"
done < <(awk -F '\\.' '/something/{print $1}' file.txt)
To populate an array:
ids=()
while read -r id;
ids+=( "$id" )
done < <(awk -F '\\.' '/something/{print $1}' file.txt)
You can use grep's -o option to output only the text matched by a regular expression:
$ ids=($(grep -Eo '^#[0-9]+' file.txt))
$ echo ${ids[#]}
#1 #2 #3
This of course doesn't check for the existence of a period on the line... If that's important, then you could either expand things with another pipe:
$ ids=($(grep -Eo '^#[0-9]+\.something' file.txt | grep -o '^#[0-9]*'))
or you could trim the array values after populating the array:
$ ids=($(grep -Eo '^#[0-9]+\.something' file.txt))
$ echo ${ids[#]}
#1.something #2.something #3.something
$ for key in "${!ids[#]}"; do ids[key]="${ids[key]%.*}"; done
$ echo ${ids[#]}
#1 #2 #3
When I am trying to run the below Script it says invalid option 3 for cat..Whats the problem?
I am tried to use index file which specifies which file is ham and which is spam...to read the files and train spamfilter
#!bin/bash
DirBogoDict=$1
BogoFilter=/home/gunna/Downloads/bogofilter-1.2.4/src/bogofilter
x=0
for i in 'cat index | fgrep spam | head -300 | awk -F "/" '{print$2"/"$3}''
do
x=$((x+1)) ; echo $x
cat /home/gunna/Downloads/db-6.1.19.NC/build_unix/ceas08-1/$i| $BogoFilter -d $DirBogoDict -M -k 1024 -s
done
for i in 'cat index | fgrep ham | head -300 | awk -F "/" '{print$2"/"$3}''
do
x=$((x+1)) ; echo $x
cat /home/gunna/Downloads/db-6.1.19.NC/build_unix/ceas08-1/$i | $BogoFilter -d $DirBogoDict -M -k 1024 -n
done
This part
'cat index | fgrep spam | head -300 | awk -F "/" '{print$2"/"$3}''
needs to be in back-ticks, not single quotes
`cat index | fgrep spam | head -300 | awk -F "/" '{print$2"/"$3}'`
And you could probably simplify it a little with
for i in `fgrep spam index | head -300 | awk "/" '{print$2"/"$3}'`
Kdopen has explained the error you got , here is the improved code for similar for-loop function.
DirBogoDict=$1
BogoFilter=/home/gunna/Downloads/bogofilter-1.2.4/src/bogofilter
awk '/spam/&&++myctr<=300{print $2 FS $3}' FS="/" index |while read i
do
cat /home/gunna/Downloads/db-6.1.19.NC/build_unix/ceas08-1/"$i"| $BogoFilter -d ${DirBogoDict} -M -k 1024 -s
done
awk '/ham/&&++myctr<=300{print $2 FS $3}' FS="/" index |while read i
do
cat /home/gunna/Downloads/db-6.1.19.NC/build_unix/ceas08-1/"$i"| $BogoFilter -d ${DirBogoDict} -M -k 1024 -s
done
Also look at your file names , since cat is giving an error and an option is invalid. To demonstrate this, Let say you have a file a name -3error
executing the following command
cat -3error
will gave
cat: invalid option -- '3'
cat therefore is thinking the "-" is followed by one of its command line arguments. As a result you probably get an invalid option error.