Example of shell concurrent programming in Linux

Source: Internet
Author: User
Tags in python

In Python, there are many modules that enable concurrent programming, such as threading, processing, Eventlet, and Stackless python.

So how does it work for the shell? In fact, the principle is very simple, I used the method is:
1. The tasks that need to be carried out are put into the background;
2. Summary of the results of the command in the background to the specified file;
3. Use the wait command to complete the execution of all tasks.

The following script uses this concurrent programming method, which features:
Fast (within 3-4 seconds) test and return results for all IP (255) in the same C network by command Ping.

The code is as follows Copy Code

Vim fastping.sh

#!/bin/bash

# Default Settings
SUBNET=$1 # C Type subnet
Retry=2 # retry Times
Timeout=3 # timeout seconds
Output=/tmp/ping.output # Output File

# function Print_help
function Print_help () {
echo "Examples:"
Echo ${0} 172.17.32
Echo ${0} 192.168.1 Unable
Exit 1
}

# Check the parameter
If [$#-lt 1]; Then
Print_help
Fi

# Check the network parameter ' s format
Count=0
For I in $ (echo $ |sed ' s/\.//g ')
Todo
count=$ ((${count}+1))
Done
If [${count}-ne 3]; Then
Print_help
Fi

# Clean the output file
> ${output}

function pingable () {
Ping-c ${retry}-W ${timeout}-Q ${subnet}.${i} &>/dev/null && echo ${i} >> ${output}
}

function unpingable () {
Ping-c ${retry}-W ${timeout}-Q ${subnet}.${i} &>/dev/null | | Echo ${i} >> ${output}
}

# Get the Check type
If ["$" = "unable"]; Then
Status= "Unpingable"
Else
Status= "Pingable"
Fi

# Ping as Paraller mode and write output into file
For i in {1..255}
Todo
${status} &
Done

# Wait for all ping processes done
Wait

# Print output with better order
sum=$ (wc-l ${output} |awk ' {print $} ')
echo "There are\" ${sum}\ "\ ${status}\" IPs begin With\ "${subnet}.\": "
Cat ${output} | Sort-t "."-k1,1n-k2,2n-k3,3n-k4,4n | Xargs-n echo ""
chmod +x fastping.sh
./fastping.sh

Examples:
./fastping 172.17.32
./fastping 192.168.1 Unable
Time./fastping.sh 192.168.1

There are "pingable" IPs begin with "192.168.1":
1 10 12 13 14 15 16 18 19 20 21 22 23 24 25 26 27 28 29-30
31 32 33 34 35 36 37 38 40 41 42 43 44 45 46 47 48 49 50-51
52 53 54 55 56 57 59 60 61 62 63 64 65 66 67 68 69 70 71-72
73 74 75 76 77 78 80 81 83 84 85 86 87 88 89 90 91 92 93-94
95 96 97 98 99 100 101 102 103 104 105 106 107 108 112 113 114 115 116-117
118 119 120 121 122 123 124 125 126 127 128 133 134 135 136 137 138 139 140-141
142 143 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165-166
170 254

Real 0m3.201s
User 0m0.135s
SYS 0m0.495s

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.