LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-24-2019, 04:34 PM   #1
Scrag
Member
 
Registered: Mar 2004
Location: Wisconsin
Distribution: Kali Linux
Posts: 131

Rep: Reputation: 15
Bash Script Problem


Hello Everybody,

Below is the bash script I'm working on.

Code:
#!/bin/bash

GREEN='\033[0;32m'              #Setup green color to be used in echo commands
NC='\033[0m'                    #This changes color back to normal or no color
RED='\033[0;31m'                #Setup red color to be used in echo statements
YELLO='\033[0;93m'
WHITE='\033[0;97m'


declare -a info
echo ""

for sub in $(cat $1);do
        echo -e "${YELLO}DOMAIN: $WHITE$sub $NC"
        host $sub.$2
        echo ""
        info=(`host $sub.$2 | grep "has address" | cut -d" " -f4`)
        for i in "${info[@]}"; do
                echo -e "$WHITE $i $NC";
                OpenPort=`masscan "$i" -p0-65535 --rate 100000 2>/dev/null | cut -d' ' -f4`
                for x in "$OpenPort"; do
                        echo -e "         |-- ${GREEN}$OpenPort $NC"
                done
        done
        echo ""
done
What it does is it takes in a txt file of domains and then uses the linux host command to print out the discovered IP's. Then it scans the discovered IP's with masscan and prints the ports. It is not pretty but it works and I am a noob.

My problem is in the formatted output. The first port scanned prints in this format: |-- Port in green. But the 2nd port prints in green but not indented and without the preceeding |--

Like so.....

OUTPUT
Code:
root@localhost:~/RECON-master$ ./host-list.sh all.txt
DOMAIN: 1l-api.mail.ru
1l-api.mail.ru has address 195.211.21.5
1l-api.mail.ru has address 195.211.21.6
1l-api.mail.ru mail is handled by 10 mail.terrhq.ru.

        195.211.21.5
          |-- 80/tcp
443/tcp
        195.211.21.6
          |-- 443/tcp
80/tcp
I would like it to print out like below and cant figure out what the issue is

PREFERRED OUTPUT:
Code:
root@localhost:~/RECON-master$ ./host-list.sh all.txt
DOMAIN: 1l-api.mail.ru
1l-api.mail.ru has address 195.211.21.5
1l-api.mail.ru has address 195.211.21.6
1l-api.mail.ru mail is handled by 10 mail.terrhq.ru.

        195.211.21.5
          |-- 80/tcp
          |-- 443/tcp
        195.211.21.6
          |-- 443/tcp
          |-- 80/tcp
 
Old 07-24-2019, 07:28 PM   #2
individual
Member
 
Registered: Jul 2018
Posts: 231

Rep: Reputation: 175Reputation: 175
Your problem is the following loop.
Code:
for x in "$OpenPort"; do
    ...
done
By quoting $OpenPort, it isn't being split into individual strings, meaning the loop runs once.
EDIT: It doesn't look like you're using x, which is also why you're seeing the ports printed as one string. You need to use $x to print each port.

Last edited by individual; 07-24-2019 at 07:30 PM.
 
3 members found this post helpful.
Old 07-24-2019, 08:05 PM   #3
Scrag
Member
 
Registered: Mar 2004
Location: Wisconsin
Distribution: Kali Linux
Posts: 131

Original Poster
Rep: Reputation: 15
Got it.

Thanks again,
Scrag
 
Old 07-25-2019, 01:49 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 12,931

Rep: Reputation: 4078Reputation: 4078Reputation: 4078Reputation: 4078Reputation: 4078Reputation: 4078Reputation: 4078Reputation: 4078Reputation: 4078Reputation: 4078Reputation: 4078
you might want to use shellcheck to check your script. That will give you some hints
 
1 members found this post helpful.
Old 07-25-2019, 02:05 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,715

Rep: Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034
You seem to have your solution, so I thought I would give you some feedback on the script:

1. Right now the script is fresh in your mind, but several months away it may not be, so I would suggest adding in something to advise what the script requires on the command line to be passed in
so $1 and $2 make some sense

2. Further to the above, if you assign those values to variables with meaningful names, it will also help in knowing what is required

3. If this script is to be used by others (or again you later on), do not use a for loop or unnecessary 'cat' to read files. A while loop is better suited in case the data in the files might change format

4. Quote all variables until you find a reason not to, as per solution provided by individual

5. Back ticks are old and not as versatile as $() which can be easily nested as required

6. Another easy solution to your problem would have been to make OpenPort an array like you did 'info' (this helps with the for loop and not the fact that you did not use the correct variable inside the loop)

7. You could look into reducing your grep/cut to use maybe sed or awk


Hope some of that helps Of course you are free to ignore it all
 
3 members found this post helpful.
Old 07-25-2019, 09:34 AM   #6
Scrag
Member
 
Registered: Mar 2004
Location: Wisconsin
Distribution: Kali Linux
Posts: 131

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by grail View Post
You seem to have your solution, so I thought I would give you some feedback on the script:

1. Right now the script is fresh in your mind, but several months away it may not be, so I would suggest adding in something to advise what the script requires on the command line to be passed in
so $1 and $2 make some sense

2. Further to the above, if you assign those values to variables with meaningful names, it will also help in knowing what is required

3. If this script is to be used by others (or again you later on), do not use a for loop or unnecessary 'cat' to read files. A while loop is better suited in case the data in the files might change format

4. Quote all variables until you find a reason not to, as per solution provided by individual

5. Back ticks are old and not as versatile as $() which can be easily nested as required

6. Another easy solution to your problem would have been to make OpenPort an array like you did 'info' (this helps with the for loop and not the fact that you did not use the correct variable inside the loop)

7. You could look into reducing your grep/cut to use maybe sed or awk


Hope some of that helps Of course you are free to ignore it all
Thanks for the advice - its really helpful.

Scrag
 
Old 07-26-2019, 12:29 AM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,152

Rep: Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524
Deleted.

Last edited by MadeInGermany; 08-05-2019 at 06:29 PM. Reason: Somehow the edited post got a new post
 
Old 07-26-2019, 12:30 AM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,152

Rep: Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524
One comment:
the quotes in
Code:
for i in "${info[@]}"; do
are correct because they prevent a "word split on $IFS" (and $IFS defaults to whitespace), but the @ in ${ } is special - it still splits on the array members.
But if you have a simple variable and you want to split on the whitespace in the $variable then you cannot use the protecting quotes.
Code:
for x in $OpenPort; do
Not protecting with quotes also have the effect of doing a glob match against files. Ensure there a no * or other glob characters in the $variable! Or turn these glob matches off with
Code:
set -f
 
1 members found this post helpful.
Old 08-04-2019, 06:05 PM   #9
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (Chicago area)
Distribution: CentOS, MacOS, [Open]SuSE, Raspian, Red Hat, Slackware, Solaris, Tru64
Posts: 1,424

Rep: Reputation: 120Reputation: 120
Quote:
Originally Posted by grail View Post
Right now the script is fresh in your mind, but several months away it may not be, so I would suggest adding in something to advise what the script requires on the command line to be passed in
Or... look into getopt(1) with a simple example being something like:
Code:
...

function usage() {
    echo "Usage: $0  --in=input --out=output [--debug] [--help]" 1>&2
    exit 1
}

...


OPTS=$( getopt --options '' --longoptions debug,help,in:,out: -n 'getopt_test' -- "$@" )

if [ $? != 0 ] ; then echo "Invalid switch." >&2 ; exit 1 ; fi

eval set -- "$OPTS"

DEBUG=FALSE
IN="undefined"
OUT="undefined"

while true; do
    case "$1" in
        --debug ) DEBUG=TRUE; shift ;;
        --help )  usage; exit ;;
        --in )    IN="$2"; shift 2 ;;
        --out )   OUT="$2"; shift 2 ;;
        -- )      shift; break ;;
        * )       break ;;
    esac
done

[ ${DEBUG} == "TRUE" ] && echo "DEBUG is enabled"
echo IN=$IN
echo OUT=$OUT
...
I always forget the details of how getopt(1) works so I keep the above in a file that I can include while writing shell scripts and have similar snippets for Perl and Python. (Handy things to have available for quick use.) You just have to make sure you keep the usage() function and the actual command line switches in sync.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Running bash script from another bash script bulletproof.rs Programming 5 12-10-2017 04:22 AM
[SOLVED] BASH Script - What am I doing wrong in this test? - BASH Script BW-userx Programming 34 04-08-2017 01:36 PM
Bash problem : -bash: [: /bin/bash: unary operator expected J.A.X Linux - Software 1 09-22-2011 05:52 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 08:56 AM
Bash script to create bash script jag7720 Programming 10 09-10-2007 07:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration