Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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

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

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
donewhen 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

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

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 againLIST = '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

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.txtNow, 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

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 itThanks
T

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

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

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

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |