Computing.Net > Forums > Programming > k shell

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

k shell

Reply to Message Icon

Name: howdy
Date: September 30, 2009 at 15:21:57 Pacific
OS: Microsoft Windows XP Professional
CPU/Ram: 3.192 GHz / 2045 MB
Product: Intel / Product8
Subcategory: C/C++
Comment:

I have a file called Hosts that contain all the devices some of them in fact I know their community names either public or non public, and I used the command for instance:

Router='ypcat hosts| grep router| sort | awk '{print $2}''

The result is fine, but what if we add another device to the file hosts?
My goal is to create a script that reads from a file called “hosts” each device and run using and if or while statement command snmpget -v1 -c "name" $device snmpInBadCommunityName.0 to check if this device is public or non public and if it is public display an error!!

Ex:
if ERRORCOUNT=’ snmpget -v1 -c "nom public" $device snmpInBadCommunityName.0 | awk “{print $2}’’
Then echo”$device=$ERRORCOUNT”
Echo ‘’”
else
if ERRORCOUNT=’ snmpget -v1 -c "nom public" $device snmpInBadCommunityName.0 | awk “{print $2}’’ then
echo”$device=$ERRORCOUNT”
else echo “ display a message”
fi
fi
The issue I’m having with this is : the script does not go further with the else and after
What do u think?
Any common



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: September 30, 2009 at 16:08:21 Pacific
Reply:

In the bourne shell, this only works if you are using command substitution:

Router=`ypcat hosts| grep router| sort | awk '{print $2}'`

Command substiution is back tics in the bourne shell (optional in korn) and $() in the korn:

Router=$(ypcat hosts| grep router| sort | awk '{print $2}')

If you are expecting more than line returned from ycat ...., consider using a while loop:

# untested
ypcat hosts| grep router| sort | awk '{print $2}' while read Router
do
   echo "$Router"
done

Sorry, but I am not following the rest of your post




0

Response Number 2
Name: howdy
Date: October 1, 2009 at 08:42:03 Pacific
Reply:

Thank you naile,
here is an example of what i'm trying to accomplish.

LIST='ypcat hosts|| sort | awk '{print $2}'
for Devices in $LIST
do
if Errorcount='snmpget -v1 -c public $Devices snmpInBadCommunityNames.O |awk '{print $4}' '
then echo "$Devices=$Errorcount"
else
if Errorcount='snmpget -v1 -c private $Devices snmpInBadCommunityNames.O |awk '{print $4}' '
then
"$Devices=$Errorcount"
else
echo "error"
fi
fi
done

when i run it i get an error "syntax error 'else' unexpected "
i want to be able to sort evry device in this list and check if it's community name is public or private!!!!

thanks for your help

thanks again


0

Response Number 3
Name: nails
Date: October 1, 2009 at 21:30:39 Pacific
Reply:


Sorry, but I am not going to be able to help you much since I am not familar with ypcat and snmpget.

but I see a lot of syntax problems:

1) If the then is on a separate line it must be by itself.

2) You still are NOT using back tics for command substitution.

3) You can not set assignments in if statements in the shell.

4) This variable assignment is illegal:

$Devices=$Errorcount"

To set the contents of $Errorcount to variable Devices, it's this:

Devices=$Errorcount

5) Until you really understand how the if statement works, I would use the brackets [ ] as I show in the example.

Obvously, this is untested, but it might give you some ideas:

ypcat hosts|| sort | awk '{print $2}|while read Devices
do
   Errorcount=`snmpget -v1 -c public $Devices snmpInBadCommunityNames.O |awk '{print $4}' ` 
   if [ Errorcount > 0 ]
   then 
      Devices=$Errorcount
      echo $Devices
   else
      Errorcount=`snmpget -v1 -c private $Devices snmpInBadCommunityNames.O |awk '{print $4}' `
      if [ Errorcount > 0 ]
      then
         Devices=$Errorcount
      else
         echo "error"
      fi
   fi
done




0

Response Number 4
Name: howdy
Date: October 5, 2009 at 08:42:05 Pacific
Reply:

Thank you Naile,
The script is working now.

This script check for Community names of a dirrerent devices" public or private" and the number of hits "Errorcount". My question is: Can anyone help me implemented so i can see the difference on number of hits that i got from last time i run it to today. for example: if i run it today and the number of hits of a speific device is 20. If i run it tomorrow or next week or next month to check the same device, i want to be able to see the difference between yesterday and the day i run it.
Thank you again

LIST = 'ypcat hosts|| sort | awk '{print $2}''
for Devices in $LIST
do
Errorcount=`snmpget -v1 -c public $Devices snmpInBadCommunityNames.O |awk '{print $4}' `
if [ Errorcount > 0 ]; then
Devices=$Errorcount
echo $Devices
else
Errorcount=`snmpget -v1 -c private $Devices snmpInBadCommunityNames.O |awk '{print $4}' `
if [ Errorcount > 0 ]; then
Devices=$Errorcount
else
echo "error"
fi
fi
done


0

Response Number 5
Name: nails
Date: October 5, 2009 at 10:44:35 Pacific
Reply:

Any number of ways can address this problem. One way is to echo the system date when the script is run and the devices number to a file:

# set date to your spec: this is month/day/yr/hr/min/sec:
sysdate=`date '+%m/%d/%Y-%H:%M:%S'`
# I suggest NOT putting your datafile in the /tmp directory
echo "$sysdate $Devices" >> /tmp/hosthistory.txt

Now, add some code at the beginning of the script to read the last line of the file:


tail -1 /tmp/hosthistory.txt |while read lastdate mydevices
do
echo $lastdate
echo $mydevices
done


1

Related Posts

See More



Response Number 6
Name: howdy
Date: October 6, 2009 at 10:04:11 Pacific
Reply:

Thank you again,
i'm confused , are u suggesting to add this code into my script?
tail -1 /tmp/hosthistory.txt |while read lastdate mydevicesdo echo $lastdate echo $mydevicesdone , if yes Where exactly.
hepl please
would u consider tutoring?
if yes we can some more about it

Thanks
T


0

Response Number 7
Name: nails
Date: October 6, 2009 at 12:32:54 Pacific
Reply:

First, I will try to answer any question you ask.

Second, no, I am not suggesting that you add the code your script in the way you have demonstrated.

Before we do read the /tmp/hosthistory.txt file, let's discuss how you are going to update it; If it were me I would set you sysdate variable before your main script executes:

# set date to your spec: this is month/day/yr/hr/min/sec:
sysdate=`date '+%m/%d/%Y-%H:%M:%S'`

# get the last line before the history file is modified
tail -1 /tmp/hosthistory.txt |while read lastdate mydevices
do      
   echo $lastdate   
   echo $mydevices
done

# then you could do something like this so each time the script executes, an entry is made to the history file:

# untested
LIST = 'ypcat hosts|| sort | awk '{print $2}'' 
for Devices in $LIST
do
   Errorcount=`snmpget -v1 -c public $Devices   snmpInBadCommunityNames.O |awk '{print $4}' ` 
   if [ Errorcount > 0 ]; then
      Devices=$Errorcount
      echo $Devices
      # line added
      echo "$sysdate $Devices" >> /tmp/hosthistory.txt
else
      Errorcount=`snmpget -v1 -c private $Devices    snmpInBadCommunityNames.O |awk '{print $4}' `
      if [ Errorcount > 0 ]; then
        # line added
        echo "$sysdate $Devices" >> /tmp/hosthistory.txt
        Devices=$Errorcount
      else
         echo "error"
      fi
   fi
done

# I don't know what you mean when you want to "compare" the last run with this run, but you have the variables for the last run in $lastdate and $mydevices.

Finally, I do not know what shell you are using, but I suggest you use the korn shell. Place this line on line 1, column 1:

#!/bin/ksh


1

Response Number 8
Name: howdy
Date: October 6, 2009 at 14:33:48 Pacific
Reply:


Thanks again
1- I’m using K shell.
Since every device that is either public or private return an errorcount, for example device1=4. And the number 4 is the number of hits. If i run this shell script today, I can see the errorcount for each device (public or private). If I run the same shell script tomorrow, I can see the errorcount for each device (public or private).
I want to able to see devices that their errorcount changed. That way i can tell the difference of hits that specific device got. For example
router20 = 30
------
router20 = 35
Now i can tell rounter20 got hits 5 more time since the last run.
Thanks
T




0

Response Number 9
Name: howdy
Date: October 20, 2009 at 09:20:26 Pacific
Reply:

Good morning all,
Based on my script, I can get the community names and the error count.
I want to be able to see if there is anyone who is trying to hack it into my devices.
I know if my device community name is private it should be equal to zero, unless if someone trying to hack into it.
Please advices
Thanks


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: k shell

how to pass command to child shell? www.computing.net/answers/programming/how-to-pass-command-to-child-shell/13388.html

Unix shell script www.computing.net/answers/programming/unix-shell-script/13981.html

perl: eq vs == and www.computing.net/answers/programming/perl-eq-vs-and-/5449.html