BashScripts.org
http://bashscripts.org/forum/

retrieve default passwords for network device
http://bashscripts.org/forum/viewtopic.php?f=7&t=371
Page 1 of 1

Author:  gubluntu [ Thu May 17, 2007 2:17 pm ]
Post subject:  retrieve default passwords for network device

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

Author:  jeo [ Thu May 17, 2007 8:38 pm ]
Post subject: 

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.

Author:  jeo [ Thu May 17, 2007 10:15 pm ]
Post subject: 

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:
[[email protected] 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...

Author:  jeo [ Thu May 17, 2007 10:38 pm ]
Post subject: 

Alright, got it. I can go to bed now :)

Code:
[[email protected] 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 :)

Author:  gubluntu [ Fri May 18, 2007 7:26 am ]
Post subject: 

awesome.. please post the code.. you da man

Author:  jeo [ Fri May 18, 2007 7:45 am ]
Post subject: 

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

Author:  gubluntu [ Fri May 18, 2007 8:46 am ]
Post subject: 

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?

Author:  gubluntu [ Fri May 18, 2007 8:56 am ]
Post subject: 

sweet.. didnt see you posted when i replied.. nice im tinkering with it now...

Author:  gubluntu [ Fri May 18, 2007 9:12 am ]
Post subject: 

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

Author:  jeo [ Fri May 18, 2007 9:36 am ]
Post subject: 

Hmm... What are you searching for when you get those errors?

Author:  gubluntu [ Fri May 18, 2007 10:51 am ]
Post subject: 

i dont remember that one... try linksys for instance:

Code:
[email protected]:~$ 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

Author:  jeo [ Fri May 18, 2007 11:11 am ]
Post subject: 

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

Author:  gubluntu [ Fri May 18, 2007 1:29 pm ]
Post subject: 

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

Author:  jeo [ Fri May 18, 2007 1:39 pm ]
Post subject: 

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 :)

Author:  crouse [ Fri May 18, 2007 1:54 pm ]
Post subject: 

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

Author:  gubluntu [ Fri May 18, 2007 2:00 pm ]
Post subject: 

looks fine here on my 20in wide :-D

nice i love this thing

Author:  jeo [ Fri May 18, 2007 2:08 pm ]
Post subject: 

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...

Page 1 of 1 All times are UTC - 6 hours
© 2000, 2002, 2005, 2007 phpBB Group • http://www.phpbb.com