PDA

View Full Version : Bash Script


CasperLS
11-17-2009, 06:11 AM
Im writing this script in bash under VI can you tell me what my problem is what im trying to do is when an ip address comes up that starts with 10.*.*.* i want it to then do the break line . Thanks

I'm getting an error on line 9.


TD=`date +%Y%m%d%H%M`

cat /home/tmp/206access_log | cut -d" " -f1 | sort | uniq > $HOME/$TD_ip.log

for IPADD in `cat $HOME/$TD_ip.log`
if [ $IPADD = "10*" ] #This is the problem
then
break
fi
do
echo -n $IPADD" "
lynx -dump "http://api.hostip.info/get_html.php?ip=$IPADD"
done

oesxyl
11-17-2009, 06:23 AM
Im writing this script in bash under VI can you tell me what my problem is what im trying to do is when an ip address comes up that starts with 10.*.*.* i want it to then do the break line . Thanks

I'm getting an error on line 9.


TD=`date +%Y%m%d%H%M`

cat /home/tmp/206access_log | cut -d" " -f1 | sort | uniq > $HOME/$TD_ip.log

for IPADD in `cat $HOME/$TD_ip.log`
if [ $IPADD = "10*" ] #This is the problem
then
break
fi
do
echo -n $IPADD" "
lynx -dump "http://api.hostip.info/get_html.php?ip=$IPADD"
done

you are on windows or *nix?

best regards

CasperLS
11-17-2009, 06:24 AM
under *nix

CasperLS
11-17-2009, 06:25 AM
Exact error:

> ./script
./script: line 9: syntax error near unexpected token `if'
./script: line 9: ` if [ $IPADD = "10*"]'

oesxyl
11-17-2009, 06:36 AM
under *nix
206access.log have only IP inside?

linux? what distro?

you can replace this:

cat /home/tmp/206access_log | cut -d" " -f1 | sort | uniq > $HOME/$TD_ip.log

with this:

cut -d' ' -f1 /home/tmp/206access_log | sort -u | grep -v -e "^10" > $HOME/$TD_ip.log

this way you can get rid of that if. You can use anithing from toolbox, sed, awk, egrep instead of grep -v

best regards

oesxyl
11-17-2009, 06:39 AM
Exact error:

> ./script
./script: line 9: syntax error near unexpected token `if'
./script: line 9: ` if [ $IPADD = "10*"]'

the problem is before if:

for i in `cat $HOME/$TD_ip.log` ; do


best regards

CasperLS
11-17-2009, 07:02 AM
The distro is suse and here is a line out of the log:

10.132.15.105 - - [29/Oct/2009:16:20:39 -0700] "GET /mindterm.jar HTTP/1.1" 200 1132813 "-" "Mozilla/4.0 (Windows XP 5.1) Java/1.6.0_16"

The thing is when i run the script i want it to run the log but take out all the 10. address because those are internal addresses

CasperLS
11-17-2009, 07:12 AM
the problem is before if:

for i in `cat $HOME/$TD_ip.log` ; do


best regards

ok i did that now theres another problem lol

./script
./script: line 13: syntax error near unexpected token `do'
./script: line 13: `do'

oesxyl
11-17-2009, 07:21 AM
The distro is suse and here is a line out of the log:

10.132.15.105 - - [29/Oct/2009:16:20:39 -0700] "GET /mindterm.jar HTTP/1.1" 200 1132813 "-" "Mozilla/4.0 (Windows XP 5.1) Java/1.6.0_16"

The thing is when i run the script i want it to run the log but take out all the 10. address because those are internal addresses
it's a server log, :)
swap the tools, :)

grep -v -e "^10\." logfilename | cut -d' ' -f1 | sort -u

you can change the regex for other cases but for this ip it's enought to match 10 and the dot .

best regards

oesxyl
11-17-2009, 07:24 AM
ok i did that now theres another problem lol

./script
./script: line 13: syntax error near unexpected token `do'
./script: line 13: `do'
how your script looks now? post it please

best regards

CasperLS
11-17-2009, 07:30 AM
#!/bin/bash
#log.ip

TD=`date +%Y%m%d%H%M`

cut -d' ' -f1 /home/tmp/206access_log | sort -u | grep -v -e "^10" > $HOME/$TD_ip.log

for i in `cat $HOME/$TD_ip.log` ; do
if [ $IPADD = "10*" ]
then
break
fi
do
echo -n $IPADD" "
lynx -dump "http://api.hostip.info/get_html.php?ip=$IPADD"
done

rgraja
11-17-2009, 07:33 AM
Fits your budget and your style (http://www.replicawatchesindia.info)

oesxyl
11-17-2009, 07:36 AM
you don't need the if block because grep -v already will exclude ip which start with '10.'
more than that, the if condition is never match because what you get from log is a string, full ip and will never match 10*

meantime I rewrite and test your script:

#!/bin/bash

# arguments, not quite clever, need checking!!!
logfile=$1

TD=`date +%Y%m%d%H%M`

grep -v -e "^10\." $logfile | cut -d' ' -f1 | sort -u > x

for ip in `cat x` ; do
echo $ip
lynx -dump http://api.hostip.info/get_html.php?ip=$ip
done

replace x with yours filename.

I change it, was a typo in the line with lynx, http:// was twice

best regards

oesxyl
11-17-2009, 07:51 AM
I don't know if you know about this:

http://www.tldp.org/guides.html

second is Advanced Bash-Scripting Guide, it's pretty good, :)

best regards

CasperLS
11-17-2009, 08:02 AM
Thanks for the help i got it

oesxyl
11-17-2009, 08:15 AM
Thanks for the help i got it
you are welcome, :)

best regards