Register
It is currently Sat Dec 20, 2014 11:46 pm

Snmpget in bash results to variable and save to sql


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Wed Aug 26, 2009 9:10 am   

Joined: Wed Aug 26, 2009 9:07 am
Posts: 1
Hi all,

I'm working on a switch reader and need a little help how to do this the good way
I want each results in a variable and save it to sql

Code:
#!/bin/bash

snmpget -v 1 xx.xx.xx.xx -c public ifInOctets.1 |cut -b34-70 >/var/www/html/stats/1.in
snmpget -v 1 xx.xx.xx.xx -c public ifOutOctets.1 |cut -b37-70 >/var/www/html/stats/1.out
snmpget -v 1 xx.xx.xx.xx -c public ifMtu.1 |cut -b28-70 >/var/www/html/stats/1.mtu
snmpget -v 1 xx.xx.xx.xx -c public ifOperStatus.1 |cut -b35-70 >/var/www/html/stats/1.status
snmpget -v 1 xx.xx.xx.xx -c public ifLastChange.1 |cut -b36-70 >/var/www/html/stats/1.change
chmod 755 /var/www/html/stats/1.out
chmod 755 /var/www/html/stats/1.in
chmod 755 /var/www/html/stats/1.mtu
chmod 755 /var/www/html/stats/1.status
chmod 755 /var/www/html/stats/1.change

this block repeats 24 time till .24


Can i do this command in 1? and how do u set the results to a variable?

Greetz
Gerd


Top
 Profile  
 PostPosted: Wed Aug 26, 2009 11:18 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 579
basically, with a c-style for loop:
Code:
## the snmp stuff
If=( InOctets OutOctets Mtu OperStatus LastChange )
Of=( in out mtu status change )

for (( i=1; i<=24; i++ ))
do
   for item in ${If[@]}
    do
      snmpget -v 1 xx.xx.xx.xx -c public if${If[item]}.$i | cut -b34-70 > \
/var/www/html/stats/${i}.${Of[ind++]}
   done
unset ind
done

chmod 755 /var/www/html/stats/{1..24}.{in,out,mtu,status,change}

## the sql stuff
for file in /var/www/html/stats/{1..24}.{in,out,mtu,status,change}
do
   while read line
    do
    # something like that:
      mysql -u User -h Host -D Database -p Pass \
-e 'INSERT INTO TABLE your_Table (column) VALUES('"$i"')'
   done < $file
done


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP