Operating System - HP-UX
1837341 Members
3448 Online
110116 Solutions
New Discussion

script from scratch with problems

 
SOLVED
Go to solution
Ratzie
Super Advisor

script from scratch with problems

First I want to thank all for the help, I wa able to get it working regarding the grep of IP. But here is my problem, on further testing and revamping I have put my self in a pickle with out saving th e working copy to go by. But, I have changed what I want done in the script...

I have a IP file
server@ ~/scripts$ more sw_ip.txt |sort
142.160.179.200
123.456.101.20
123.456.102.20
123.456.102.21
123.456.103.20
123.456.104.20
123.456.105.20
123.456.106.20
123.456.109.20
123.456.113.20

I need to run a for loop that takes that IP and then greps a file, that was created from an expect script that logs into a machine and changes passwd. The reason I am doing it this way instead of what was in my question before of, awk the created file sort| uniq, then grep for ip. Is that I now for sure these are the IP that I HAVE to have checked. The created script may not have all logged IP's in it for what ever reason. So if it is missing, the grep will still output that IP as not having "changed passwd"


Now here is the problem...
I have attached my script with comments, but is in short words...

for D_IP in $(awk '{print $1}' sw_ip.txt |sort
do
echo $D_IP
LOGIN=/tmp/login.txt
grep $D_IP $LOGIN |grep -q "changed passwd"
if [ $?! = 0 ]
then echo "$D_IP 'grep'"echo $D_IP >>/tmp/host.not
fi
done


Does not work.
The grep returns a value of 1 but for each line in the login.txt AND NOTHING IN MY HOST.NOT FILE!

This is my login.txt parred down..

123.456.101.20 changed passwd.
123.456.101.20 logged in.
123.456.102.20 changed passwd.
123.456.102.20 logged in.
123.456.102.21 changed passwd.
123.456.102.21 logged in.
123.456.103.20 changed passwd.
123.456.103.20 logged in.
123.456.104.20 changed passwd.
123.456.104.20 logged in.
123.456.105.20 logged in.
123.456.106.20 logged in.
123.456.109.20 logged in.

I should only be getting 3 lines retured in the host.not file
Right now I get nothing!

I really appreciate all the help that was given before, and have filed that for furture use. But, I have been working on this for a day in a half.
Laura

14 REPLIES 14
Kent Ostby
Honored Contributor

Re: script from scratch with problems

Laura -- I may have missed the previous postings on this issue.

What three lines are you expecting to get back ?

Best regards,

Kent M. Ostby
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Ratzie
Super Advisor

Re: script from scratch with problems

What I need the script to do is grep the IP for "changed passwd" if it does not find it out put it to the file, host.not.
So I should get
123.456.105.20
123.456.106.20
123.456.109.20

Or as you see in my login.txt file the bottom 3 IP'S.
Sridhar Bhaskarla
Honored Contributor
Solution

Re: script from scratch with problems

Hi,

I always use rcs to keep track of my versions. Atleast you can save the script as script.1, script.2 etc., whenver you make changes.

The problem is if [ $?! = 0 ]. This should be $? != 0. First like should have closed ")".

This time instead of correcting your script, I will try to put down what I wrote. Replace IPLISt and LOG to wherever they are.


IPLIST=sw_ip.txt
LOGIN=login.txt
RESULT=host.not

for IP in $(cat $IPLIST)
do
grep -q -x "$IP changed passwd." $LOGIN

if [ $? != 0 ]
then
echo $IP >> $RESULT
fi
done


-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Ratzie
Super Advisor

Re: script from scratch with problems

Thank you! I did not have a space!
[ $? [space] != 0 ]

But, I am still haveing a problem, it still out puts the whole list.
I even checked for hidden characters in my $LOGIN_ENTRY_FILE_4 and in my $IPLIST, nothing but the return character at the end.
Even if I grep without the -x still same problem!

This is my $LOGIN_ENTRY_FILE_4
123.456.101.20, logged in.
123.456.101.20, changed passwd.
123.456.102.20, logged in.
123.456.102.20, changed passwd.
123.456.102.21, logged in.
123.456.102.21, changed passwd.
123.456.103.20, logged in.
123.456.104.20, logged in.
123.456.104.20, changed passwd.
123.456.105.20, logged in.
123.456.105.20, changed passwd.

My host.not file still outputs all of the IPs


rm /tmp/host.not
IPLIST=$SCRIPTS_DIR/sw_ip.txt
for IP in `cat $IPLIST`
do
echo $IP
grep -q -x "$IP, changed passwd." $LOGIN_ENTRY_FILE_4
if [ $? != 0 ]
then
echo $IP >>/tmp/host.not
fi
done
Kent Ostby
Honored Contributor

Re: script from scratch with problems

Okay .. after you have your sw_ip.txt and login.txt files created, you can run this:

touch .use1 .use2
rm .use1 .use2
awk '{print "grep ",$1,"login.txt >> .use2"}' < sw_ip.txt > .use1
chmod 755 .use1
./.use1
awk 'BEGIN{useip=0;} /changed passwd/ {useip=$1;next} /logged in/ {if (useip!=$1
) {print $0};useip=0;}' .use2


Explanation:

touch touchs the files we'll use and then rms them on the next line (so we can use >> without picking up old data).

first awk creates the greps instead of doing it inline.
chmod makes the script executable
./.use1 creates ./.use2
then the last awk script sets the useip value when it finds a "changed passwd" line.

If it finds a logged in line that doesnt have a matching "changed passwd" line then it prints it.

Best regards,

Kent M. Ostby
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Sridhar Bhaskarla
Honored Contributor

Re: script from scratch with problems

Hmmm.. you changed your input files. The first example lines didn't have a ',' in them but the recent one showed , again. Here is what I have and it's working for me. I modified grepping to incorporate , and to ignore any number of blank spaces,tabs between "," and the text.

$cat sw_ip.txt
142.160.179.200
123.456.101.20
123.456.102.20
123.456.102.21
123.456.103.20
123.456.104.20
123.456.105.20
123.456.106.20
123.456.109.20
123.456.113.20
123.456.104.2
$cat login_txt
123.456.101.20, logged in.
123.456.101.20, changed passwd.
123.456.102.20, logged in.
123.456.102.20, changed passwd.
123.456.102.21, logged in.
123.456.102.21, changed passwd.
123.456.103.20, logged in.
123.456.104.20, logged in.
123.456.104.20, changed passwd.
123.456.105.20, logged in.
123.456.105.20, changed passwd.
$cat scr
IPLIST=sw_ip.txt
LOGIN=login.txt

for IP in $(cat $IPLIST)
do
grep "${IP}," $LOGIN |grep -q "changed passwd."

if [ $? != 0 ]
then
echo $IP >> host.not
fi
done

$./scr
$cat host.not
142.160.179.200
123.456.103.20
123.456.106.20
123.456.109.20
123.456.113.20
123.456.104.2

Also, unless we see the actual files, it's difficult to carve a 100% solution for you. Please attach them instead of posting them.

-Sri
PS: Please do not assign points until your problem is completely solved atleast to me. Thanks.
You may be disappointed if you fail, but you are doomed if you don't try
Ratzie
Super Advisor

Re: script from scratch with problems

If I put this before my grep...

echo "${IP}, changed passwd."

I get
, changed passwd.

I do not get the IP, so I am having problems with that variable!
Sridhar Bhaskarla
Honored Contributor

Re: script from scratch with problems

What if you simply run

IPLIST=$SCRIPTS_DIR/sw_ip.txt

for IP in $(cat $IPLIST)
do
echo ${IP}
done

Did you have your SCRIPTS_DIR defined properly?.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Ratzie
Super Advisor

Re: script from scratch with problems

I put an echo $IP before it...
rm /tmp/host.not
IPLIST=$SCRIPTS_DIR/sw_ip.txt
for IP in `cat $IPLIST`
do
echo $IP
echo "${IP}, changed passwd."
grep -q -x "$IP, changed passwd." $LOGIN_ENTRY_FILE_4
if [ $? != 0 ]
then
echo $IP >>/tmp/host.not
fi
done


This is my output...
123.456.101.20
, changed passwd.
123.456.102.20
, changed passwd.
123.456.102.21
, changed passwd.
123.456.103.20
, changed passwd.
Ratzie
Super Advisor

Re: script from scratch with problems

simple script
IPLIST=/the/correct/path/sw_ip.txt
for IP in $(cat $IPLIST)
do
echo $IP
echo $IP
echo "$IP"
echo "$IP, changed passwd."
echo "$IP changed passwd."
echo $IP", changed passwd."
done

Output (parred down)

123.456.80.48
123.456.80.48
123.456.80.48
, changed passwd.
changed passwd.
, changed passwd.
123.456.35.48
123.456.35.48
123.456.35.48
, changed passwd.
changed passwd.
, changed passwd.
123.456.20.50
123.456.20.50
123.456.20.50
, changed passwd.
changed passwd.
, changed passwd.
Ratzie
Super Advisor

Re: script from scratch with problems

Can some one explain this...
IPLIST=/the/correct/path/sw_ip.txt
for IP in $(cat $IPLIST)
do
echo $IP
echo $IP
echo "$IP"
echo $IP ","
echo "$IP ,"
echo "$IP, changed passwd."
echo "$IP changed passwd."
echo $IP", changed passwd."
done

Output:
123.456.20.50
123.456.20.50
123.456.20.50
,3.17.20.50
,3.17.20.50
, changed passwd.
changed passwd.
, changed passwd.


Kent Ostby
Honored Contributor

Re: script from scratch with problems

Nope....

The last thing you did, gives me this:

123.456.101.20
123.456.101.20
123.456.101.20
123.456.101.20 ,
123.456.101.20 ,
123.456.101.20, changed passwd.
123.456.101.20 changed passwd.
123.456.101.20, changed passwd.
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Michael Schulte zur Sur
Honored Contributor

Re: script from scratch with problems

Hi,

there is one more problem:
$? != 0

two errors, $? is numeric and grep returns 0 when it finds something so it must be:
$? -eq 0

Michael
Ratzie
Super Advisor

Re: script from scratch with problems

Just to give everyone a heads up on this one, for some reason, the file needed to be dos2ux, it did not show any wonky charecters when I vi'd it but when I did that it worked!

Thanks everyone for there help!