Register
It is currently Wed Sep 02, 2015 12:26 am

Switch VLAN query


All times are UTC - 6 hours


Post new topic Reply to topic  [ 11 posts ] 
Author Message
 PostPosted: Thu Apr 23, 2015 11:40 am   

Joined: Wed Apr 15, 2015 2:04 pm
Posts: 15
Location: USA
Hi all,

In my environment, we have a port checker running a php form via an web browser. You enter the IP of the switch and it displays the VLAN configurations. Picture attached to show what I mean.
I want to write a script that will query multiple IP's of all the switches in one location and output that data back to me where I can then make it view nicely in a spreadsheet.
Obviously the best way for something like this would require credentials to log into these switches, but that is an engineer's job in my environment. I don't have that info.
I know that I can use lynx to view the page and put in an IP and get the information I need. I'm trying to think of more clear ways and how to incorporate this into a bash script.

I'll continue to ponder on it. Just throwing out my ideas here.


You do not have the required permissions to view the files attached to this post.


Top
 Profile WWW  
 PostPosted: Thu Apr 23, 2015 11:59 am   

Joined: Wed Apr 15, 2015 2:04 pm
Posts: 15
Location: USA
My thoughts are using lynx with the -cmd_script to pull a list of commands from a file that will automate my 'searching and inputing the IP's one by one. Then printing the received page to a file (concatenate) from which I can then massage the data in the printed file to be importable to a spreadsheet.


Top
 Profile WWW  
 PostPosted: Thu Apr 23, 2015 1:10 pm   

Joined: Wed Apr 15, 2015 2:04 pm
Posts: 15
Location: USA
Here's what I'm starting with. I need to skip the first file in the cwd, though. Else it's concatenating to itself and breaks.

Code:
#!/bin/bash
lynx -cmd_script=ip_log [IP HERE]
txt1="vlaninfo.txt1"
cwd=$(pwd)
files=$cwd/*.txt*
for f in $files; do
if [[ $f = $txt1 ]]; then
        $f ={$f}2
fi
cat $f >> $txt1
done


Next I need to make sure the listed files from the current working directory are in order. Instead of:
vlaninfo.txt1, vlaninfo.txt10, vlaninfo.txt11, vlaninfo.txt12... ... vlaninfo.txt18, vlaninfo.txt19, vlaninfo.txt2, vlaninfo.txt20, vlaninfo.txt21 etc
I want vlaninfo.txt1, vlaninfo.txt2, vlaninfo.txt3, vlaninfo.txt4, vlaninfo.txt5, vlaninfo.txt6


Top
 Profile WWW  
 PostPosted: Thu Apr 23, 2015 2:01 pm   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 188
Better use curl instead of lynx.
It handles all protocol related things properly and is designed for use in scripts.

And curl does output to stdout.
Hence it is just a loop over ips like this:
Code:
ips=(  one.machine  10.2.1.12  third.machine )

for machine in ${ips[*]}; do
   curl <all needed options>  $machine >> result_file
done


If you want to go the way with different files, there is another problem when it comes to sorting.
As sorting takes place according to the locale, there can't be a numerical correct ordering.
To create sortable file names use something like this:
Code:
numbers=( $( echo {001..999} )  )
for num in ${numbers[*]} ; do
   echo some_name_$num
done


Top
 Profile  
 PostPosted: Thu Apr 23, 2015 4:15 pm   

Joined: Wed Apr 15, 2015 2:04 pm
Posts: 15
Location: USA
So, the site I'm querying uses php. A curl GET for it shows such:

Code:
<html>
<body>
<form action="vlaninfo.php" method="post">
<p>IP Address: <input type="text" name="host" /><br />
<p><input type="submit" value="Submit"></p>
</form>
</body>
</html>


In order to submit info to this and then GET the response page. I'm having a hard time figuring this out.
Something like curl --data "host=[IP]" [website]


Top
 Profile WWW  
 PostPosted: Fri Apr 24, 2015 10:00 am   

Joined: Wed Apr 15, 2015 2:04 pm
Posts: 15
Location: USA
I'm wondering if this would be easier as a php script.


Top
 Profile WWW  
 PostPosted: Fri Apr 24, 2015 5:39 pm   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 188
Code:
curl -F host=some.host.somewhere   


Top
 Profile  
 PostPosted: Mon Apr 27, 2015 9:03 pm   

Joined: Wed Apr 15, 2015 2:04 pm
Posts: 15
Location: USA
Here's what I have so far (IP's changed to generic ones):
Code:
#!/bin/bash
cwd=$(pwd)
rm $cwd/vlanlist
count=36
ip="10.10.2.1"
baseaddr="$(echo $ip | cut -d. -f1-3)"
lso="$(echo $ip | cut -d. -f4)"

while [ $count -gt 0 ]
do
   ips=()
   ips+=( $baseaddr.$lso )
   for machine in ${ips[*]}; do
      curl -silent -F host=$machine 10.10.6.1/vlaninfo.php >> vlanlist
   done
   lso=$(( $lso + 1 ))
   count=$(( $count -1 ))
done


Thanks for the guidance, uhelp.


Top
 Profile WWW  
 PostPosted: Mon Apr 27, 2015 9:11 pm   

Joined: Wed Apr 15, 2015 2:04 pm
Posts: 15
Location: USA
Just as an afterthough, this is the way I had previously did it with lynx. But I prefer the cURL method.
Code:
#!/bin/bash
cwd=$(pwd)
rm $cwd/vlanlist
lynx -cmd_script=$cwd/lynxlog 10.10.4.1
txt1="vlaninfo.txt1"
for f in $(ls -altr $cwd/ | awk '/.txt/'); do
cat $f >> $txt1 2>/dev/null
done
mv $txt1 $cwd/vlanlist
txt1=vlanlist
for i in $(ls $cwd/ | awk '/.txt/'); do
rm -rf $i
done
echo $txt1
cat $txt1


Top
 Profile WWW  
 PostPosted: Tue Apr 28, 2015 6:05 am   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 188
ae to add some annotations.

The following two lines use a subshell $( : ) and a pipe (which in fact ends up with more subshells.
Code:
baseaddr="$(echo $ip | cut -d. -f1-3)"
lso="$(echo $ip | cut -d. -f4)"
You just want to cut the IP into pieces.
Use bashs own facilities. There is a parameter expansion doing this easily.
Try this:
Code:
ip=192.168.33.11
echo ${ip##*.}
echo ${ip%.*}
No subshell. No overhead. And very fast.

Now you are using ls.
Like above unneeded subshells and forks.
But a source of subtle failures is the use of ls
ls gives NOT the filenames, it gives formatted output.
And this output is influenced by LOCALE too. It is NOT guaranteed that this will work on every machine.
Code:
# very bad!
for f in $(ls -altr $cwd/ | awk '/.txt/'); do

# do instead
for f in $cws/*.txt; do
   echo filename is $f
done
This works everywhere and is much more faster. You might check the setting of glob before. (Is enabled by default, but we should never rely on any assumptions.)
Another way to deal with filenames very secure is to use the proper tool for these issues: find
Get used to it. It is fast, reliable and outputs file names. Nothing else.
Code:
find $cwd -iname '*.txt'
find searches from $cwd all directories and lists all files ending wit ".txt". It understands a lot of tests one can do with filenames. In fact you can combine any tests with logical operators like -and, -or and negations !. -iname tests filenames case insensitive (i ) . Hence all files ending with any combination of [Tt][Xx][Tt] will be found.


Top
 Profile  
 PostPosted: Tue Apr 28, 2015 8:49 am   

Joined: Wed Apr 15, 2015 2:04 pm
Posts: 15
Location: USA
Awesome! I didn't realize that bash had a native tool to break up the ip.

I am familiar with the 'find' command and I should have realized that it is better to do it without the subshells and I should also have realized that 'ls' isn't usually the same in different machines.

I will remember these bits. Thank you!


Top
 Profile WWW  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 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