.

First shell script... COMPLETE.

<<

SephStorm

User avatar

Hero Member
Hero Member

Posts: 570

Joined: Sat Apr 17, 2010 12:12 pm

Post Mon Oct 01, 2012 4:52 pm

First shell script... COMPLETE.

Well I just wrote my first shell script today and I wanted to share this experience with you all. Its an amazing feeling, jr. programming 101 or something to that effect. I figured i'd share this easy script with you guys, any comments, suggestions or improvements are appreciated and desired.

I used this as my primary guide, I am still going through it, but that and some youtube videos so far gave me what I needed to build this script.

  Code:
#! /bin/bash
# nmap ping scan
# used to ping a subnet

clear
namp -sP 192.168.1.0/24 > /pentest/results/scans/pingscan.txt && echo scan complete.

exit


fairly simple, but useful I hope in future studies... >:)  One thing I wanted to do, not sure if it's possible, but originally, I wanted "scanning..." or something to that effect to be displayed while the command was running, and then "scan complete" at the end. Can that be done without adding too much complexity?

Also, many of the examples I saw used quotation marks around statements that were to be echoed. Based on what I found, quotes are used for variables or something to that effect, can someone explain them to me, and when they should/should not be used?
Last edited by SephStorm on Mon Oct 01, 2012 4:55 pm, edited 1 time in total.
sectestanalysis.blogspot.com/‎
<<

ZeroOne

Jr. Member
Jr. Member

Posts: 59

Joined: Tue Apr 24, 2012 7:41 am

Post Mon Oct 01, 2012 5:30 pm

Re: First shell script... COMPLETE.

You might wanna change "namp" to "nmap", also the gateway isn't always 192.168.1.0, the script is good over all, but try replace 192.168.1.0 with localhost so you don't have to change it every time when using different networks. Just like this:

nmap -sP localhost/24 > /pentest/results/scans/pingscan.txt && echo scan complete.

Thanks for sharing  :)
<<

hayabusa

User avatar

Hero Member
Hero Member

Posts: 1662

Joined: Mon Jan 29, 2007 2:59 pm

Post Mon Oct 01, 2012 5:36 pm

Re: First shell script... COMPLETE.

ZeroOne wrote:You might wanna change "namp" to "nmap", also the gateway isn't always 192.168.1.0, the script is good over all, but try replace 192.168.1.0 with localhost so you don't have to change it every time when using different networks. Just like this:

nmap -sP localhost/24 > /pentest/results/scans/pingscan.txt && echo scan complete.

Thanks for sharing  :)


localhost???  Network is 0, not gateway...  He's scanning a network range.
Last edited by hayabusa on Tue Oct 02, 2012 5:53 am, edited 1 time in total.
~ hayabusa ~ 

"All men can see these tactics whereby I conquer, but what none can see is the strategy out of which victory is evolved." - Sun Tzu, 'The Art of War'


OSCE, OSCP , GPEN, C|EH
<<

ZeroOne

Jr. Member
Jr. Member

Posts: 59

Joined: Tue Apr 24, 2012 7:41 am

Post Mon Oct 01, 2012 5:43 pm

Re: First shell script... COMPLETE.

Ops.. forget what I said apparently blanked out & got the whole thing wrong. I need some rest. Thanks hayabusa for embarrassing me. just joking  ;D
<<

hayabusa

User avatar

Hero Member
Hero Member

Posts: 1662

Joined: Mon Jan 29, 2007 2:59 pm

Post Mon Oct 01, 2012 6:12 pm

Re: First shell script... COMPLETE.

No worries.  Wasn't trying to embarrass anyone.  Just making sure we kept things straight.

Cheers, ZeroOne!
~ hayabusa ~ 

"All men can see these tactics whereby I conquer, but what none can see is the strategy out of which victory is evolved." - Sun Tzu, 'The Art of War'


OSCE, OSCP , GPEN, C|EH
<<

SephStorm

User avatar

Hero Member
Hero Member

Posts: 570

Joined: Sat Apr 17, 2010 12:12 pm

Post Mon Oct 01, 2012 6:18 pm

Re: First shell script... COMPLETE.

lol I was like "localhost... network ???!"

but good catch on the namp.... luckily I rewrote that to post it, the orginal script spells it correctly.
sectestanalysis.blogspot.com/‎
<<

hayabusa

User avatar

Hero Member
Hero Member

Posts: 1662

Joined: Mon Jan 29, 2007 2:59 pm

Post Mon Oct 01, 2012 7:11 pm

Re: First shell script... COMPLETE.

SephStorm - even before 'prettying it up' with your scanning message, might I suggest figuring out something you'll find more useful -

Work out how to pass in variables to your scripts, such that you can pass in a network address and subnet mask, from a command-line, so you could use it in other networks.  (This type of scripting will come in handy on pentests, when you can 'modularize' things and pass in variable data, for faster automation of scanning activity.)
~ hayabusa ~ 

"All men can see these tactics whereby I conquer, but what none can see is the strategy out of which victory is evolved." - Sun Tzu, 'The Art of War'


OSCE, OSCP , GPEN, C|EH
<<

dynamik

Recruiters
Recruiters

Posts: 1119

Joined: Sun Nov 09, 2008 11:00 am

Location: Mile High City

Post Mon Oct 01, 2012 7:25 pm

Re: First shell script... COMPLETE.

I also like just including one/few-liners in my .bashrc as functions or aliases
The day you stop learning is the day you start becoming obsolete.
<<

MaXe

User avatar

Hero Member
Hero Member

Posts: 671

Joined: Tue Aug 17, 2010 9:49 am

Post Mon Oct 01, 2012 8:45 pm

Re: First shell script... COMPLETE.

Using arguments passed from command line is also relatively easy, you should play with this as well  ;D

Example:
echo Hi $1, you like $2 and you also like $3. The script name is $0.

Usage:
./bashscript MaXe 0days knowledge

Output:
Hi MaXe, you like 0days and you also like knowledge. The script name is bashscript.

You can also use more advanced things such as "getopts". But that example I gave you, is very handy when you need a dynamic script quickly  :)
I'm an InterN0T'er
<<

SephStorm

User avatar

Hero Member
Hero Member

Posts: 570

Joined: Sat Apr 17, 2010 12:12 pm

Post Mon Oct 01, 2012 9:02 pm

Re: First shell script... COMPLETE.

I dont think im at that level yet, i'll get right on it. *hides*
sectestanalysis.blogspot.com/‎
<<

hayabusa

User avatar

Hero Member
Hero Member

Posts: 1662

Joined: Mon Jan 29, 2007 2:59 pm

Post Mon Oct 01, 2012 9:06 pm

Re: First shell script... COMPLETE.

Nah.  I got confidence in you.  MaXe literally gave you all you need, in order to do it.  It really IS that easy!
~ hayabusa ~ 

"All men can see these tactics whereby I conquer, but what none can see is the strategy out of which victory is evolved." - Sun Tzu, 'The Art of War'


OSCE, OSCP , GPEN, C|EH
<<

MaXe

User avatar

Hero Member
Hero Member

Posts: 671

Joined: Tue Aug 17, 2010 9:49 am

Post Mon Oct 01, 2012 9:37 pm

Re: First shell script... COMPLETE.

I was surprised when I found out there was a way easier option than getopts  ;D Hence the reason I shared it hehe  :)
I'm an InterN0T'er
<<

SephStorm

User avatar

Hero Member
Hero Member

Posts: 570

Joined: Sat Apr 17, 2010 12:12 pm

Post Mon Oct 01, 2012 9:48 pm

Re: First shell script... COMPLETE.

If i'm not mistaken, you are talking about variables here right?

like this?

http://tldp.org/LDP/abs/html/varsubn.html
sectestanalysis.blogspot.com/‎
<<

superkojiman

User avatar

Jr. Member
Jr. Member

Posts: 81

Joined: Thu Sep 20, 2012 9:42 pm

Post Mon Oct 01, 2012 10:33 pm

Re: First shell script... COMPLETE.

SephStorm wrote:If i'm not mistaken, you are talking about variables here right?

like this?

http://tldp.org/LDP/abs/html/varsubn.html


Yes. Arguments passed into the program are assigned to $1, $2, $3, $4, and so on, with $0 being the name of the program itself. It's actually not that complicated, bash is a pretty easy thing to learn. If you check the manual for bash, you'll see a lot of neat tricks and things you can incorporate into your script.
OSCP + OSCE
<<

SephStorm

User avatar

Hero Member
Hero Member

Posts: 570

Joined: Sat Apr 17, 2010 12:12 pm

Post Mon Oct 01, 2012 10:56 pm

Re: First shell script... COMPLETE.

I think I know what MaXe is saying here, instead of the arguments being defined explicitly in the script, I can specify them in my command string and have for instance:

  Code:
#! /bin/bash
# nmap ping scan
# used to ping a subnet

clear
nmap -sP $IP > /pentest/results/scans/$scanname.txt && echo scan complete.

exit


./pingscan.sh 192.168.1.0/24 pingscan

correct?
sectestanalysis.blogspot.com/‎
Next

Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron
.
Powered by phpBB® Forum Software © phpBB Group.
Designed by ST Software