LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   shell scripting (https://www.linuxquestions.org/questions/linux-newbie-8/shell-scripting-763298/)

howdy 10-20-2009 05:19 PM

shell scripting
 
hello all,

i'm new to this group, so please be patient with me

I need to automate the following process:

I have a list of devices in a file called host.txt, I need to take that list and run the command

snmpget -v 1 -c private devices snmpget InBadCommunityNames.0 !!!(if its correct)

for each device to see if its running snmpget, I want to create a file for all the the devices that are running snmpget and ofcourse will return zero.

if possible been able to see if someone try to hack mmy devices,will show a number for example drop### =2 (means someone try to hack it twice)

I am new to scripting so any help would be appreciated. This is for a solaris box, and KSH.

Thanks

TB0ne 10-20-2009 06:01 PM

Quote:

Originally Posted by howdy (Post 3726522)
hello all,

i'm new to this group, so please be patient with me

I need to automate the following process:

I have a list of devices in a file called host.txt, I need to take that list and run the command

snmpget -v 1 -c private devices snmpget InBadCommunityNames.0 !!!(if its correct)

for each device to see if its running snmpget, I want to create a file for all the the devices that are running snmpget and ofcourse will return zero.

if possible been able to see if someone try to hack mmy devices,will show a number for example drop### =2 (means someone try to hack it twice)

I am new to scripting so any help would be appreciated. This is for a solaris box, and KSH.

Thanks

The usual answer to such questions here is: Post what you've written, and where you're having a problem, and we'll be glad to help.

There are thousands of shell script tutorials on Google that can help you. Also, this may be more suited to the Solaris forum here, since it's not really Linux related.

chrism01 10-20-2009 06:57 PM

More ksh docs than you can poke a stick at
http://kornshell.com/

pixellany 10-20-2009 07:27 PM

A partial answer:

Code:

for dev in *; do
    somecommand <options> $dev
    otherstuff
done

Here, "somecommand" wants a filename as its last argument. That is supplied by the value of the variable "dev". The way it is written, It would have to be run in the directory where the files are.


the above is for BASH, but I would expect ksh to be similar

howdy 10-21-2009 08:05 AM

thanks pixellany

here is what i did :
LIST = 'ypcat hosts|| sort | awk '{print $2}''
for Devices in $LIST
do
Errorcount=`snmpget -v1 -c privator $Devices snmpInBadCommunityNames.O |awk '{print $4}' `
if [ Errorcount > 0 ]; then
Devices=$Errorcount
echo $Devices
# line added
echo "$sysdate $Devices" >> /tmp/hosthistory.txt

fi
done

but the answer is wrong cause i get the errorcount.
i want to be able to see all the devices equal to zero if the community name is privator

thanks for your help

howdy 10-21-2009 09:47 AM

hello again

i want to Perform an SNMP set request in SNMP agent. return 0 if the connect succeeded.

here is what i did :
LIST = 'ypcat hosts|| sort | awk '{print $2}''
for Devices in $LIST
do
Errorcount=`snmpget -v1 -c privator $Devices snmpInBadCommunityNames.O |awk '{print $4}' `
if [ Errorcount > 0 ]; then
Devices=$Errorcount
echo $Devices
# line added
echo "$sysdate $Devices" >> /tmp/hosthistory.txt

fi
done

but the answer is wrong cause i'm getting an errorcount.

catkin 10-21-2009 12:19 PM

What does var=` <some command> ` do? It doesn't put the exit status of <some command> in var.

See the Advanced Bash-Scripting Guide for "command substitution" explanation and examples and then again here for "exit status" explanation and examples.

chrism01 10-21-2009 08:22 PM

Your 1st line is hopeless; you've got 2 single quotes and a double quote. You prob want backquotes at the start and end, and single quotes for the awk cmd.
Also, you have double pipe symbols after 'hosts'. This is an 'or' operator. You prob meant 1 pipe.
You are using the wrong comparator in the if statement:
http://tldp.org/LDP/abs/html/comparison-ops.html
Have a deep read of that doc.
Also, consider using

$(cmd)

instead of

`cmd`

its easier to read/debug if you have other types of quote marks involved.

howdy 10-22-2009 01:53 PM

thank you chrism01
i fixed all of those comments but actually what i'm looking for is to be able to check for community name for all my devices , if its correct return 0 if not return 1.

Thanks again

catkin 10-22-2009 02:39 PM

So post your corrected script and we'll take it from there. It's easier to read if you put it in code tags (that's a link to instructions or you may prefer to use "Advanced Edit" mode which has a # button for code tags).

howdy 10-22-2009 04:07 PM

Thank you catkin
here is what i did
LIST = 'ypcat hosts| sort | awk '{print $2}''
for Devices in $LIST
do
value=`snmpget -v1 -c privator $Devices snmpInBadCommunityNames.O |awk '{print $4}' `
if [ Errorcount >= 0 ]; then

echo $Devices=0
# line added
echo "$Devices" >> /tmp/hosthistory.txt

fi
done

but i feel that i'm forcing the value of the device to be zero
I still dont think its doing what i wanted to do and get as a result.
i want to Perform an SNMP set or get request in SNMP device. return 0 if the connect succeeded.return 1 if the connect fails.

thank you again

chrism01 10-22-2009 05:53 PM

You may(!) have 'fixed all those comments' somewhere, but the above code does not show it. It's almost identical to your first code post.
Post the 'fixed' code in code tags as requested by Catkin, and we'll go from there.


All times are GMT -5. The time now is 02:54 PM.