Register
It is currently Sun Sep 21, 2014 4:09 pm

retrieve default passwords for network device


All times are UTC - 6 hours


Post new topic Reply to topic  [ 17 posts ] 
Author Message
 PostPosted: Thu May 17, 2007 2:17 pm   

Joined: Thu May 17, 2007 2:10 pm
Posts: 8
Location: NYC
heres my first bash script for bashscript.org. hope you like it... also an obvious note is that you will need curl installed


Code:
#!/bin/bash
#usage: scriptName "device name or model number" (you must include quotes)
# e.g. scriptName "wrt54g"

curl -s http://www.phenoelit.de/dpl/dpl.html | grep -i $1 | sed "s/<[^>]*>/ /g"



lpease improve and flame me :-D


Top
 Profile  
 PostPosted: Thu May 17, 2007 8:38 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Very nice, I didn't know about that site :)

I can't really think of a way to improve that script unless you could come up with a way to split up and lable the columns, but it looks too inconsistent to be easily parsed.


Top
 Profile YIM  
 PostPosted: Thu May 17, 2007 10:15 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Ok, I just discovered the coolest bash command ever.

"column"

Only problem is that some of the columns of the output are kind of long, so if your terminal isn't very wide it can make it hard to read. You sound like you want to learn some of this yourself, so I'll just say that I used the source (curl -s) just like you do, but instead of replacing the table tags with spaces, I replaced them with commas, so that I could use them as delimiters with the column command.

sed s/"<\/TD><TD>"/","/g
sed s/"^<TD>"/""/g

I hate it when I get obsessed with a script... I made more changes after I started typing this. I can post what I've got if you want, otherwise ask me if you have any questions :) Here's the output:

Code:
[jeo@bonzai test]$ sh test.sh netbuilder
3COM  NetBuilder                      SNMP           ANYCOM  snmp-read                   No   2002-1-10  2005-13-7
3COM  NetBuilder                      SNMP           ILMI    snmp-read                   No   2002-1-10  2005-13-7
3COM  Netbuilder                      Multi  admin   (none)  Admin                       No   2002-1-10  2005-13-7
3COM  Netbuilder                      HTTP   Root    (none)  Admin      http://10.1.0.1  Yes  2004-24-2  2005-13-7
3com  NetBuilder                      SNMP   (none)  admin   User       SNMP_READ        No   2002-1-10  2005-13-7
3com  Netbuilder                      Multi  admin   (none)  Admin                       No   2002-1-10  2005-13-7
3com  superstack II Netbuilder  11.1  Multi  n/a     (none)  Admin                       No   2004-21-8  2005-13-7


Now to work on column headers...


Top
 Profile YIM  
 PostPosted: Thu May 17, 2007 10:38 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Alright, got it. I can go to bed now :)

Code:
[jeo@bonzai test]$ sh test.sh netbuilder
Manufactor  Product                   Revision  Protocol  User ID  Password  Access     comment          Validated  Created    LastMod
3COM        NetBuilder                          SNMP               ANYCOM    snmp-read                   No         2002-1-10  2005-13-7
3COM        NetBuilder                          SNMP               ILMI      snmp-read                   No         2002-1-10  2005-13-7
3COM        Netbuilder                          Multi     admin    (none)    Admin                       No         2002-1-10  2005-13-7
3COM        Netbuilder                          HTTP      Root     (none)    Admin      http://10.1.0.1  Yes        2004-24-2  2005-13-7
3com        NetBuilder                          SNMP      (none)   admin     User       SNMP_READ        No         2002-1-10  2005-13-7
3com        Netbuilder                          Multi     admin    (none)    Admin                       No         2002-1-10  2005-13-7
3com        superstack II Netbuilder  11.1      Multi     n/a      (none)    Admin                       No         2004-21-8  2005-13-7


I modified the sed lines that I posted before, and did things in a slightly different order. I can post the code if anybody's interested. I like to put lots of comments for those that are learning :)


Top
 Profile YIM  
 PostPosted: Fri May 18, 2007 7:26 am   

Joined: Thu May 17, 2007 2:10 pm
Posts: 8
Location: NYC
awesome.. please post the code.. you da man


Top
 Profile  
 PostPosted: Fri May 18, 2007 7:45 am   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
No... I've just got a little OCD when it comes to writing scripts... I added some more error checking type stuff this morning :)

Code:
#!/bin/sh
#
# This is a script to retrieve default passwords for network devices
# Original script by: gubluntu
# Modifications by: jeo

# First we want to make sure the user has supplied an argument
# If not, we tell them what the script expects with a "usage"
# line and exit

if [ $# == 0 ]; then
  echo "usage: $(basename $0) <mfg.> or <model#>"
  echo "Lists possible default passwords for network devices"
  exit 0
fi


# Here we'll set the URL.  I assume at this point that if the
# URL changes, the entire script will have to change, but still...

URL="http://www.phenoelit.de/dpl/dpl.html"


# Now let's grab the list from our URL, convert it into a
# comma seperated list, grab only lines containing commas (to
# get rid of any irrelevant info), and store it in memory
# in the form of a variable.  We'll call it "DATA".

DATA="$(curl -s $URL|sed s/"<\/TD>"/","/g | sed s/"<TD>"/""/g |grep ",")"

# From our comma seperated list, let's extract the
# column headers and store them in a variable

HEADER="$(echo "$DATA"| head -n1)"


# And now let's grab the lines that we're looking for

RESULT="$(echo "$DATA"| grep -i $1)"


# We can also say something useful if we don't find
# what we're looking for

if [ -z $RESULT ]; then
  echo "Sorry, couldn't find anything for \"$1\""
  exit
else

# Still part of our "if"
# And last but not least, we pipe our processed list
# (including headers) through "column" to get a pretty
# print-out.  Problem here is if your terminal isn't wide
# enough, some word-wrapping happens...  Perhaps we could
# pipe this to a text viewer that doesn't do wraping?

  echo -e "$HEADER"\\n"$RESULT" | column -ntx -s","

fi

exit 0


Top
 Profile YIM  
 PostPosted: Fri May 18, 2007 8:46 am   

Joined: Thu May 17, 2007 2:10 pm
Posts: 8
Location: NYC
column is nice

Code:
curl -s http://www.phenoelit.de/dpl/dpl.html | grep -i $1 | sed "s/<[^>]*>/|/g" | column -t -s "|"



how did you get the headers?


Top
 Profile  
 PostPosted: Fri May 18, 2007 8:56 am   

Joined: Thu May 17, 2007 2:10 pm
Posts: 8
Location: NYC
sweet.. didnt see you posted when i replied.. nice im tinkering with it now...


Top
 Profile  
 PostPosted: Fri May 18, 2007 9:12 am   

Joined: Thu May 17, 2007 2:10 pm
Posts: 8
Location: NYC
so cool... every now and then i get some err's like: [: 15: ==: unexpected operator
[: 59: VPN: unexpected operator
-e


and other nonsense.. how do we tell it the output is completely text and not to do anything with any special characters


Top
 Profile  
 PostPosted: Fri May 18, 2007 9:36 am   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Hmm... What are you searching for when you get those errors?


Top
 Profile YIM  
 PostPosted: Fri May 18, 2007 10:51 am   

Joined: Thu May 17, 2007 2:10 pm
Posts: 8
Location: NYC
i dont remember that one... try linksys for instance:

Code:
gsilver@gsilver-desktop:~$ dfltpwd linksys
[: 15: ==: unexpected operator
[: 59: Linksys|DSL||Telnet|n/a|admin|Admin||No|2002-1-10|2005-13-7: unexpected operator
-e Manufactor   Product                                            Revision          Protocol  User ID        Password  Access         comment                             Validated  Created     LastMod
Linksys         WAP11                                                                Multi     n/a            (none)    Admin                                              No         2003-16-6   2005-13-7
Linksys         DSL                                                                  Telnet    n/a            admin     Admin                                              No         2002-1-10   2005-13-7


Top
 Profile  
 PostPosted: Fri May 18, 2007 11:11 am   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Hehe :oops:

I forgot some quotes in the test to see if there are any results...

if [ -z "$RESULT" ]; then

So you were right that it was interpreting the output instead of just treating it as text. I just wasn't noticing the error because it was scrolled off my screen :)

Here's the corrected script:

Code:
#!/bin/sh
#
# This is a script to retrieve default passwords for network devices
# Original script by: gubluntu
# Modifications by: jeo

# First we want to make sure the user has supplied an argument
# If not, we tell them what the script expects with a "usage"
# line and exit

if [ $# == 0 ]; then
  echo "usage: $(basename $0) <mfg.> or <model#>"
  echo "Lists possible default passwords for network devices"
  exit 0
fi


# Here we'll set the URL.  I assume at this point that if the
# URL changes, the entire script will have to change, but still...

URL="http://www.phenoelit.de/dpl/dpl.html"


# Now let's grab the list from our URL, convert it into a
# comma seperated list, grab only lines containing commas (to
# get rid of any irrelevant info), and store it in memory
# in the form of a variable.  We'll call it "DATA".

DATA="$(curl -s $URL|sed s/"<\/TD>"/","/g | sed s/"<TD>"/""/g |grep ",")"

# From our comma seperated list, let's extract the
# column headers and store them in a variable

HEADER="$(echo "$DATA"| head -n1)"


# And now let's grab the lines that we're looking for

RESULT="$(echo "$DATA"| grep -i $1)"


# We can also say something useful if we don't find
# what we're looking for

if [ -z "$RESULT" ]; then
  echo "Sorry, couldn't find anything for \"$1\""
  exit
else

# Still part of our "if"
# And last but not least, we pipe our processed list
# (including headers) through "column" to get a pretty
# print-out.  Problem here is if your terminal isn't wide
# enough, some word-wrapping happens...  Perhaps we could
# pipe this to a text viewer that doesn't do wraping?

  echo -e "$HEADER"\\n"$RESULT" | column -ntx -s","

fi

exit 0


Top
 Profile YIM  
 PostPosted: Fri May 18, 2007 1:29 pm   

Joined: Thu May 17, 2007 2:10 pm
Posts: 8
Location: NYC
excellent.. thanks so much its all slicked up now....

only thing i had to do was change

"if result == 0" to "if result = 0"

and

my echo didnt know of -e, so after removing it everything works perfectly

for whatever its worth im running ubunutu feisty fawn 7.04 dont know if my errors can be duplicated in all other dists


Top
 Profile  
 PostPosted: Fri May 18, 2007 1:39 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Interesting... Maybe the -e is being deprecated for echo? I dunno.

-eq should work in place of = as well

I'm gonna put this thing to use too :)


Top
 Profile YIM  
 PostPosted: Fri May 18, 2007 1:54 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 667
Location: Des Moines, Iowa
hmmm i tried it.....

Code:
[14:56:18 crouse]$ sh defaultnetworkpass.sh 3C16405
column: invalid option -- n
usage: column [-tx] [-c columns] [file ...]


Changed
echo -e "$HEADER"\\n"$RESULT" | column -ntx -s","

to

echo -e "$HEADER"\\n"$RESULT" | column -tx -s","

below is the output on my arch machine --- looks jumbled here.....but on screen it's fine...columns and all ;)

Code:
[15:00:45 crouse]$ sh defaultnetworkpass.sh 3C16405
Manufactor  Product  Revision  Protocol       User ID  Password  Access  comment    Validated  Created  LastMod
3com        3c16405  Multi     n/a            (none)   Admin     No      2002-1-10  2005-13-7
3com        3c16405  Console   Administrator  (none)   Admin     No      2002-1-10  2005-13-7
3com        3C16405  Multi     admin          (none)   Admin     Yes     2002-1-10  2005-13-7
3com        3c16405  Console   Administrator  (none)   Admin     No      2002-1-10  2005-13-7


Top
 Profile WWW  
 PostPosted: Fri May 18, 2007 2:00 pm   

Joined: Thu May 17, 2007 2:10 pm
Posts: 8
Location: NYC
looks fine here on my 20in wide :-D

nice i love this thing


Top
 Profile  
 PostPosted: Fri May 18, 2007 2:08 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Dangit, why doesn't everybody just run Debian and get it over with!!! :P

Okay 'column' probably won't give you the desired results without the '-n' option beacuse it merges adjacent delimiters, which means that some of your data will be offset, like in your example output there crouse. Youve got two columns that got merged in the middle, leaving the last two columns (which got wrapped in your output) empty. I would say awk might do better, but tabs don't line up like columns do... hmm... This requires more research...


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 guests


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