LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 06-29-2010, 04:41 AM   #1
sqn
Member
 
Registered: Apr 2002
Distribution: Ubuntu, Arch, freeBSD, Slackware
Posts: 210

Rep: Reputation: 30
Question BASH threads


Hi all

Is it possible to create threads in BASH?
I need a script with will ping a network to find out unused IP. in a for or while loop the pinging process is taking a very long time. I was wondering if it is possible to send multiple ping on the same time so this will speed up the scanning process.

Thx in advanced
 
Old 06-29-2010, 05:33 AM   #2
yooy
Senior Member
 
Registered: Dec 2009
Posts: 1,387

Rep: Reputation: 174Reputation: 174
tried to run several ping commands to background with sign & at the end?
 
Old 06-29-2010, 06:37 AM   #3
sqn
Member
 
Registered: Apr 2002
Distribution: Ubuntu, Arch, freeBSD, Slackware
Posts: 210

Original Poster
Rep: Reputation: 30
at the moment i'm experimenting with childs
- created a function runChild()witch is runs a ping command
Code:
ping -q -c2 $1|grep \"packet loss\"|cut -f4 -d \",\"|awk {'print $1'}|sed s/%//g
and an if statement to find out if the return of the command above is 100 or not.
Now i'm trying to start 254 childs so they can ping all 254 IP simultaneous. And here i'm a little stuck
 
Old 06-29-2010, 07:02 AM   #4
sqn
Member
 
Registered: Apr 2002
Distribution: Ubuntu, Arch, freeBSD, Slackware
Posts: 210

Original Poster
Rep: Reputation: 30
This is what i have so far, but not working
Code:
#!/bin/bash
iNext=0
net=$1
function ChildReturned() {
        wait $1
        echo "$1 was returned. Its exits status was: $?"
        iNext=$((iNext+1))
        echo $net.$iNext
        read key
        StartChild $net.$iNext
}

function StartChild(){
        echo "ping -q -c2 $1|grep \"packet loss\"|cut -f4 -d \",\"|awk {'print $1'}|sed s/%//g" >/t
        IP=$1
        echo $1
        read key
        bash /tmp/alive.$$ $IP &
        pid=$!
        echo "Child started with PID= $pid"
        trap 'ChildReturned $pid' SIGCHLD
}

for (( iNext=0; iNext<=254; iNext++ ));
do
        echo $net.$iNext
        read key
        StartChild $net.$iNext
done
 
Old 06-29-2010, 08:04 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Well I do not have advice on the threading but I can get rid of several wasted arguments for you:
Code:
#yours
ping -q -c2 $1|grep \"packet loss\"|cut -f4 -d \",\"|awk {'print $1'}|sed s/%//g

#mine
ping -q -c2 $1 | sed -rn '/loss/s/.*([0-9]+)% packet.*/\1/p'
 
Old 06-29-2010, 09:41 AM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
It's possible to use multiple processes (not threads) in bash, but in this case I would suggest
Code:
nmap -sP $net.0-254
 
  


Reply



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
some threads are become unnoticed because of large number of continious threads deepak_cucek LQ Suggestions & Feedback 9 08-20-2009 11:21 PM
Threads in bash???? trscookie Linux - Software 3 05-07-2007 03:09 PM
Java threads listed using kill -3 does not contain all threads found using ps -auxww coneheed Programming 2 11-14-2005 08:57 AM
Java Threads vs Native Threads rjmendez Programming 0 08-16-2004 05:58 AM
Threads in bash? pk21 Programming 1 06-02-2003 05:56 AM

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

All times are GMT -5. The time now is 02:33 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
Open Source Consulting | Domain Registration