You are not logged in.

#1 2012-06-26 13:47:58

n1x4
Member
From: Chernobyl
Registered: 2012-02-17
Posts: 149
Website

[BASH] looping text until function completes???[SOLVED]

Sup guys. Sorry for the rather n00bish question here. I'm having trouble looping a funtion in bash. The function runs a simple nmap command generated from user input but instead of showing the progress of nmap itself I'd like it to clear the screen and display 'Scanning' instead until the function completes then move on.. Everything I have tried either runs nmap first then will display 'Scanning' in an infinite loop or display 'Scanning' while the function runs but it to is an infinite loop or just doesn't work at all. I'd kind of like to figure this out so it'll help me learn a little more. I don't need the answer per sa just a little nudge in the right direction would be greatly appreciated.

the nmap function:

function nmap1 {
    nmap -Pn -p$PRT -iR $TRGT -oG $FILE &>/dev/null
}

and one of the loops I've tried (while). Also have done FOR and UNTIL but I'm just don't have the understanding yet.

while $(nmap1); do
    echo -e "\E0;5;31mScanning\E0m\n"
    sleep 2s
    clear
done

Thanks.

**EDIT**
The whole script in case I didn't explain it right. It's kind of messy, riddled with commented tries.. lol

#!/bin/bash

function target {
echo "Enter the number of IPs you wish to scan."
read TARGETS
}

function port {
echo "Enter the port you wish to scan for."
read PRT
}

function filename {
echo "Enter grepable filename: "
read FILE
}

clear
target
clear
port
clear
filename
clear

#echo "The port you selected to scan was $PRT"
#echo "Press Y to continue or any key to quit."
#read CONF
#clear

function nmap1 {
     nmap  -Pn -p$PRT -iR $TARGETS -oG $FILE &>/dev/null
}

#function scan {
   # if [ $CONF = y ]; then
    #$nmap1
   # else
   # echo "Bye!"
   # exit
#fi
#}

#scan
#nmap1

while [ $(nmap1) -n ]; do
    echo -e "\E[0;5;31mScanning\E[0m\nPlease be patient."
    sleep 10s
    clear
done

clear

function grep1 {
grep -E '/open/' $FILE | awk '{print $2}' >> $FILE-open
}

grep1
clear

function open {
cat $FILE-open | wc -l
}

if [ $(open) = 0 ]; then
	echo "There were no open services on port $PRT of these IPs."
	else
	cat $FILE-open
fi

rm -r $FILE

Last edited by n1x4 (2012-06-26 14:34:36)


||github||

Offline

#2 2012-06-26 14:03:01

seveg
Member
From: Bratislava, Slovakia
Registered: 2012-04-17
Posts: 33

Re: [BASH] looping text until function completes???[SOLVED]

I'm not sure why you need while, or maybe what exacly are you trying to do.. but, it looks like you want to display 'Scanning..' while your function is running.
In that case:

function nmap1 {
    nmap -Pn -p$PRT -iR $TRGT -oG $FILE 2&>/dev/null
}

clear
echo "Scanning ...'
nmap1
clear
# Do something else

Just another drug abuser..

Offline

#3 2012-06-26 14:07:21

davvil
Member
Registered: 2008-05-06
Posts: 165

Re: [BASH] looping text until function completes???[SOLVED]

I must admit I don't really understand the problem. Why can't you just do something like this

clear
echo Scanning
nmap1

with nmap1 modified so that it doesn't go into background?

If you want to display some kind of "progress bar", you could try something like this

nmap1
echo -n Scanning
while pgrep nmap >& /dev/null; do
  echo -n .
  sleep 2
done

This of course has problems if you have more than one nmap running, it would be better to scan for pids. Have a look at the $! variable. Perhaps the 'wait' command of bash can alse be useful here.

Offline

#4 2012-06-26 14:09:46

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [BASH] looping text until function completes???[SOLVED]

Read this: help wait

Background a job by appending an &
The job ID of the last backgrounded job is under the variable $!

example with "sleep 5" as a long running process:
sleep 5 & echo wait for sleep to finish; wait $!; echo sleeping doneoops this needs piping to /dev/null too

Note that in this case you can also just suppress all output from nmap with &>/dev/null

clear; echo scanning; nmap......... &>/dev/null; clear; echo scanning complete

Last edited by Procyon (2012-06-26 14:26:42)

Offline

#5 2012-06-26 14:13:53

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: [BASH] looping text until function completes???[SOLVED]

Almost usable example:

#!/bin/bash
find / -name *.pacnew > ~/pacnew_files 2> /dev/null &
clear
echo "Please wait..."
wait
echo "finished"

EDIT too slow. wink

Last edited by skanky (2012-06-26 14:14:23)


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#6 2012-06-26 14:34:13

n1x4
Member
From: Chernobyl
Registered: 2012-02-17
Posts: 149
Website

Re: [BASH] looping text until function completes???[SOLVED]

Thank you gents for your help!!! I always try to over think things, that's why I thought a loop was needed hmm < dumbass


||github||

Offline

Board footer

Powered by FluxBB