Visit Our UNIX and Linux User Community


Need help with creating a script for Snmpwalking


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with creating a script for Snmpwalking
# 1  
Old 04-01-2016
Need help with creating a script for Snmpwalking

Hello,

Very new to scripting, basically learning on the job. So, I have a task I'm trying to complete. What I need is comparing an input file and running the snmpwalk command to find out which 3 community strings a device is using.

this is what I have so far:

(I've replaced what the community string is with just community01/02/03)

Code:
while read line
do
                device_ip=`echo $line | cut -f1 -d","`
                
		device_name=`echo $line | cut -f2 -d","`
		
		alarm_title=`echo $line | cut -f3 -d","`


if [[ $alarm_title == "MANAGEMENT AGENT LOST" ]]; then
		snmpwalk01=`snmpwalk -v 2c -c community01 "$device_ip" sysName.0`
		
		if [[ -z "$snmpwalk01" ]; then
			snmpwalk02=`snmpwalk -v 2c -c community02 "$device_ip" sysName.0`

			if [[ -z "$snmpwalk02" ]]; then
				snmpwalk03=`snmpwalk -v 2c -c community03 "$device_ip" sysName.0`
				
				if [[ -z "$snmpwalk03" ]]; then
					echo "$device_name"="unknown"
				fi					
			fi

		fi
fi

I'm sure I've completely butchered everything...but this is what I've got by googling, looking at scripts already on the server, etc etc.

Like I said, I have an input file that has the Device IP, Device Name, and the Alarm Detail. Not all of the alarm details are "Management Agent Lost". What I'm trying to do is take one line from that input file, see if the community01 works as a community string. If it doesn't, then test community02. If that doesn't work, test 03.

If none work, echo the device name and the result.
If one of them works, echo device name and the community string that does work.
# 2  
Old 04-01-2016
With quite a lot of assumptions on e.g. your input file's structure, the shell you run (bash?), and your output format, this might do what you want:
Code:
while IFS=, read IP NAME TITLE
  do    [ "$TITLE" = "MANAGEMENT AGENT LOST" ] || continue
        FOUND=0
        for i in 01 02 03
          do    smnpwalk[$i]=$(snmpwalk -v 2c -c community$i "$IP" sysName.0)     
                [ "${smnpwalk[$i]}" ] && { FOUND=1; break; }
          done
        [ $FOUND == 1 ] && echo "device $NAME : ${smnpwalk[$i]} community$i" || echo "device $NAME unknown"
  done < file

It reads the variables immediately from the flle, no cut needed, then tests the title for the desired string ([ ... ] && ... || ... is a shortcut if - then - else which may fail on some complex occasions), skips the line if title is wrong, or else for loops across the desired community extensions (01, 02, 03), fills in an array indexed by the extension, and, if sth. found, breaks out of the loop. Then a message is produced depending on sth. found or not. If the extensions are non-numeric, you'll need to declare an associative array first.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 04-01-2016
Quote:
Originally Posted by RudiC
With quite a lot of assumptions on e.g. your input file's structure, the shell you run (bash?), and your output format, this might do what you want:
Code:
while IFS=, read IP NAME TITLE
  do    [ "$TITLE" = "MANAGEMENT AGENT LOST" ] || continue
        FOUND=0
        for i in 01 02 03
          do    smnpwalk[$i]=$(snmpwalk -v 2c -c community$i "$IP" sysName.0)     
                [ "${smnpwalk[$i]}" ] && { FOUND=1; break; }
          done
        [ $FOUND == 1 ] && echo "device $NAME : ${smnpwalk[$i]} community$i" || echo "device $NAME unknown"
  done < file

It reads the variables immediately from the flle, no cut needed, then tests the title for the desired string ([ ... ] && ... || ... is a shortcut if - then - else which may fail on some complex occasions), skips the line if title is wrong, or else for loops across the desired community extensions (01, 02, 03), fills in an array indexed by the extension, and, if sth. found, breaks out of the loop. Then a message is produced depending on sth. found or not. If the extensions are non-numeric, you'll need to declare an associative array first.
Thanks!!

The problem I've been running into is that one guy that's teaching me is more into bash, and the other guy that's teaching me is more into ksh. So it's been getting a bit confusing.

I'd rather get into bash, only because I've been told it's more popular.

I'll look into the commands, and see if I can get it working.
# 4  
Old 04-01-2016
RudiC has not used any bashism, so it will run with ksh.
This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 04-03-2016
Most of syntax between ksh93 and bash is same. All previous scripts works fine in bash and ksh93.
Both support Posix syntax.
Standard
Code:
test / [

comparing is single =, not ==, but bash and ksh93 accept also using C-like == comparing. Dash is pure Posix compatible and not accept == test comparing.

So standard Posix version is, which works in all Posix-shells (ksh, bash, dash, ...):
Code:
[ $FOUND = 1 ] && echo "device $NAME : ${smnpwalk[$i]} community$i" || echo "device $NAME unknown"
# or using conditional command [[   ]], but it's not in Posix standard.

RudiC has used more built-in syntax = faster and not create so much subprocessing.
This User Gave Thanks to kshji For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #815
Difficulty: Medium
In CSS, E[foo] matches an E element with a 'foo' class name.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Creating script in rc.d

Hi, I have created customized scripts to start httpd and postgres (For CentOS 6) in /etc/init.d. However for it to work even after reboot, I have to put the script in /etc/rc.d/rc0.d, rc1.d, etc. # ls -lrt total 60 -rwxr-xr-x 1 root root 20199 Oct 4 2017 rc.sysinit -rwxr-xr-x 1... (6 Replies)
Discussion started by: anaigini45
6 Replies

2. Shell Programming and Scripting

Help with creating a script

Hi everyone, I am completely new to this forum and I have some questions regarding a script I am writing. I would be happy if anyone could help me with the small and precise script which should include if, then, else, while until, case and select. The scenario is as follows: 1) A user... (3 Replies)
Discussion started by: codenotfound
3 Replies

3. Shell Programming and Scripting

Creating IN list in PLSQL script dynamically by using shell script

Hi all, I have a PLSQL script which has a IN list where it takes some ids as input. For example SELECT * FROM EMPLOYEE WHERE EMPLOYEE_ID IN (comma separated list ) I want to run this quest inside a shell script but I would like to prepare the IN list dynamically where the employee ids... (1 Reply)
Discussion started by: LoneRanger
1 Replies

4. Shell Programming and Scripting

Need help in creating file restoration script from a backup script.

Hi all i am struggling in creating a restore of env files while doing applications clone. the first file i created for copying the important configurations file which is running perfect now for reverting the changes i mean when i am restoring these files to its original places i have to do... (7 Replies)
Discussion started by: javeedkaleem
7 Replies

5. UNIX for Dummies Questions & Answers

Creating a script

Alright, well I did some more research since I originally posted this thread, and as much as I'd like to delete it, I can't, so I'll just extend my initial question a little. Right now I have 3 scripts: 1#!/bin/bash # script1 - Write all files modfied x days ago find .. -daystart -mtime 0... (2 Replies)
Discussion started by: Aussiemick
2 Replies

6. Shell Programming and Scripting

help needed with creating challenging bash script with creating directories

Hi, Can someone help me with creating a bash shell script. I need to create a script that gets a positive number n as an argument. The script must create n directories in the current directory with names like map_1, map_2 etcetera. Each directory must be contained within its predecessor. So... (7 Replies)
Discussion started by: I-1
7 Replies

7. Shell Programming and Scripting

(Urgent):Creating flat file using sql script and sqlplus from UNIX Shell Script

Hi, I need help urgently for following issue. Pls help me to resolve this issue. I am calling sql script file(file1.sql) from UNIX Shell Script(script1.ksh) using sql plus and trying to create flat file that contains all records returned from SQL query in SQL script(file1.sql) I given... (6 Replies)
Discussion started by: praka
6 Replies

8. Shell Programming and Scripting

Need help creating a script

I need to automate the following process: I have a list of ip address for printers in a file called iplist.txt, I need to take that list and run the command snmpget -v 1 -c public ip address sysName.0 for each ip address to see if the printer is running snmp, I want to the create a file... (4 Replies)
Discussion started by: inLine6
4 Replies

9. UNIX for Dummies Questions & Answers

creating a script

I am trying to create a application in OSX through UNIX that will run a script to mount an image from a CD-ROM and run the application which it corresponds to, all with double clicking on a icon in OSX. Any thoughts or ideas? -Mad (3 Replies)
Discussion started by: madknowledge
3 Replies

10. Programming

creating a new C script

All right. Heres the deal, I need to know everysingle command or funtion there is to create a new c file (file.c). Heres the catch: I cannot use text editors!!!:mad: I heard of a "gcc" command is that any good?:confused: Thanks..:cool: (2 Replies)
Discussion started by: AbRa-KaDabRa
2 Replies

Featured Tech Videos