LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 10-20-2009, 05:19 PM   #1
howdy
LQ Newbie
 
Registered: Oct 2009
Posts: 5

Rep: Reputation: 0
Smile 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
 
Old 10-20-2009, 06:01 PM   #2
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 14,751

Rep: Reputation: 2610Reputation: 2610Reputation: 2610Reputation: 2610Reputation: 2610Reputation: 2610Reputation: 2610Reputation: 2610Reputation: 2610Reputation: 2610Reputation: 2610
Quote:
Originally Posted by howdy View Post
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.
 
Old 10-20-2009, 06:57 PM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,287

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
More ksh docs than you can poke a stick at
http://kornshell.com/
 
Old 10-20-2009, 07:27 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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
 
Old 10-21-2009, 08:05 AM   #5
howdy
LQ Newbie
 
Registered: Oct 2009
Posts: 5

Original Poster
Rep: Reputation: 0
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
 
Old 10-21-2009, 09:47 AM   #6
howdy
LQ Newbie
 
Registered: Oct 2009
Posts: 5

Original Poster
Rep: Reputation: 0
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.
 
Old 10-21-2009, 12:19 PM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
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.
 
Old 10-21-2009, 08:22 PM   #8
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,287

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
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.
 
Old 10-22-2009, 01:53 PM   #9
howdy
LQ Newbie
 
Registered: Oct 2009
Posts: 5

Original Poster
Rep: Reputation: 0
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
 
Old 10-22-2009, 02:39 PM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
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).
 
Old 10-22-2009, 04:07 PM   #11
howdy
LQ Newbie
 
Registered: Oct 2009
Posts: 5

Original Poster
Rep: Reputation: 0
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
 
Old 10-22-2009, 05:53 PM   #12
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,287

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
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.
 
  


Reply

Tags
scripting, shell


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Terminal functions for shell scripting with Shell Curses LXer Syndicated Linux News 0 03-26-2008 11:50 PM
SHELL scripting/ shell functions mayaabboud Linux - Newbie 6 12-26-2007 08:18 AM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
shell interface vs shell scripting? I'm confused jcchenz Linux - Software 1 10-26-2005 03:32 PM


All times are GMT -5. The time now is 06:30 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration