Problems with past arguments in script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problems with past arguments in script
# 1  
Old 05-19-2015
Code Problems with past arguments in script

I have a question is there any possibility for maken a script that you can see if computers are on in your network
when you run the script for ex. ./ping.sh 54 62 62 , a script when you give as argument the last digit of the ip adres of the computers

my network is 192.168.129.0

This is what i already have but i want to give extra parameters to the script
for example
parameter 1: xx-yy: this parameter ensures that all computers be test in the range of xx-yy eg. 61-67, test all the 7 computers with the last digit

parameter 2: -t , ensures that each number count with 200 eg. -t 17 test the ip adress 217 , -t 17-19 test all the adresses 217 to 219

Code:
for ip in 192.168.1.{1..10}; do  # for loop and the {} operator
    ping -c 1 -t 1 192.168.1.1 > /dev/null 2> /dev/null  # ping and discard output
    if [ $? -eq 0 ]; then  # check the exit code
        echo "${ip} is up" # display the output
    else
        echo "${ip} is down"
    fi
done

# 2  
Old 05-19-2015
I'd change to:
Code:
IP_PRE=192.168.1
[ -z "$1" ] && echo "Usage: ${0##*/} 15 30-37 42 .."

for num in "${@}";do
# Parse all the arguments
	if [ "-" = "$(echo $num|tr -d [[:digit:]])" ]
	then	# Its a range, as it contains a '-'
		range_start=${num/-*}
		range_end=${num/*-}
		range=$(seq $range_start 1 $range_end)
	else	# It has no range, its a single number
		range=$num
	fi
	# Parse all the numbers filled in range
	for r in $range;do
		ping -c1 $IP_PRE.$r 2>/dev/zero 1>/dev/zero && \
			echo "$IP_PRE.$r is up" || \
			echo "$IP_PRE.$r is down"
	done
done

Untested, have fun Smilie

Also, sounds quite familiar like: Script with ping

hth

Last edited by sea; 05-19-2015 at 04:32 PM..
This User Gave Thanks to sea For This Post:
# 3  
Old 05-19-2015
Thank you but how do i implement the paramters xx-yy and -t ?
# 4  
Old 05-19-2015
Usage:
Code:
./test.sh 15 30-37 42 ...

Will/should test:
Code:
192.168.1.15
192.168.1.30
192.168.1.31
...
192.168.1.37
192.168.1.42

Or did i misunderstand something?
Btw, updated the code block and added a 'usage' message if you dont pass an argument.
# 5  
Old 05-19-2015
thanks and the paremeter -t that counts for ex 200 by ./test.sh -t 17

Code:
192.168.1.217

what do you mean with update the code block and added a usage message ?
# 6  
Old 05-19-2015
I would try something like:
Code:
#!/bin/ksh
# Print Usage message and exit.
Usage() {
	printf 'Usage:\t%s [-t] lastIP\n\t%s [-t] LOWlastIP-HIGHlastIP\n' \
		"$IAm" "$IAm" >&2
	exit 1
}

BaseIP='192.168.1.'	# All but last component of desired IP address
IAm=${0##*/}		# Last component of script name for diagnostics
addon=0			# Default add-on if -t option is not present

# Parse options...
while getopts 't' opt
do      case "$opt" in
	(t)     addon=200;;
	(?)     Usage;;
	esac
done
shift $((OPTIND - 1))

# Process operand...
if [ $# -ne 1 ]
then	printf '%s: 1 operand required; found %d\n' "$IAm" $# >&2
	Usage
fi
# Verifying that the operand is in the correct format is left as an exercise
# for the reader.
low=$((${1%%-*} + addon))	# Set low end of last component of IP address.
high=$((${1##*-} + addon))	# Set high end...

# Loop through the requested IP addresses...
last="$low"
while [ "$last" -le "$high" ]
do	ip="$BaseIP$last"
	# ping, discard otuput, check exit status, and print results
	if ping -c 1 -t 1 "$ip" > /dev/null 2> /dev/null
	then	echo "${ip} is up"
	else	echo "${ip} is down"
	fi
	last=$((last + 1))
done

Although written and tested using the Korn shell, this script only uses POSIX standard shell parameter expansion and looping constructs. So it should work with any shell that tries to conform to the standard.
# 7  
Old 05-19-2015
Well if you want to ping ip 217, then you'll pass 217.. or 1-222, it'll be among it.

EDIT:
Saying/Asking: I'm not getting why there should be a need to increment passed numbers by (say) 200 with a -t toggle.
As that increasing requires an extra argument, why not pass the wanted number directly, its even 1 char less to type?
This User Gave Thanks to sea For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Should pick latest file within past 3 days using UNIX script and perform steps in message below.

Hi , Can anyone help me how do perform below requirement in unix. Step1:we will receive multiple files weekly with same name(as below) in a folder(In folder we will have other files also def.dat,ghf.dat) Filenames: 1) abc_20171204_052389.dat 2)abc_20171204_052428.dat DON'T modify... (23 Replies)
Discussion started by: sunnykamal59
23 Replies

2. Shell Programming and Scripting

Problems passing shell arguments to perl

Semi-newbie, so flame throwers to 'singe-only', please. ;-) I have a large number of (say) .html files, where I'd like to do a recursive in-place search and replace a particular string. The following bit of perl works fine: perl -pi -e 's/oldstring/newstring/g' `find ./ -name *.html` ... (2 Replies)
Discussion started by: johnny_canucl
2 Replies

3. Shell Programming and Scripting

Script variable help, Varying number of arguments to excute script

Hi Guy's. Hopefully someone can help me with what I am trying to archieve. So situation currently is, I have a script already setup however I have another script that sits infront of it. The main script basically goes and searchs multiple platforms for a list of entered data. In... (10 Replies)
Discussion started by: mutley2202
10 Replies

4. Shell Programming and Scripting

Bash script with arguments

Could someone help me with the script below? I am trying to make a script having just one arguement as a command and then it executes the appropriate code #!/bin/bash if then echo "Available commands:" echo "./exec.sh cmd1" echo "./exec.sh cmd2" elif then cmd1 =... (1 Reply)
Discussion started by: spiridakos
1 Replies

5. Shell Programming and Scripting

sub arguments to shell script

Hi, I have a shell script, when run it i get a prompt to enter arguments say 1 for doing my next task otherwise q for quit. What I am trying to do is run the shell script with the argument passed in however it does not seem to work. This is what I did ./test.sh 1 Instead it printed the line... (6 Replies)
Discussion started by: aqua9
6 Replies

6. Shell Programming and Scripting

Using arguments in Shell script

Hello, I have to make a shell script doing that : the program tests if there is an argument, if there is it checks whether this is a directory or not, If it is it opens it. for any .c file in the directory it prints 2 lines in the screen : the dependence line of the .o and compiler commend... (1 Reply)
Discussion started by: dekl
1 Replies

7. Shell Programming and Scripting

need help to pass arguments in script

Hi, I have a my script here-- print "The Perl Script does the User health check and system health check...\n"; print "---------------------------------------------------------------------\n"; # use strict; my($OS); $OS = $^O; # need to test @ARGV before GetOptions shifts it if (@ARGV... (1 Reply)
Discussion started by: namishtiwari
1 Replies

8. Shell Programming and Scripting

Help with options and arguments to a script

I'm trying to write a script that accepts both arguments and options, e.g. ./script -h 1 -m 15 -s 30 or ./script -h 1 -m 15 -s 30 I'd like for any of the arguments and options to be optional, and the option values should be numerals only. I've tried both getopt and getopts but I... (1 Reply)
Discussion started by: Ilja
1 Replies

9. Shell Programming and Scripting

use several inputs as arguments in my script

Hi there, It's pretty hard for me to explain my problem because I'm affraid I'm not using the correct vocabulary. So let me describe the situation. I wrote a script that has one argument. It works like this: ~$ cat /usr/local/bin/squote echo "$@" | sed 's/'\''/'\''\\'\'\''/g; s/.*/'\''&'\''/g'... (2 Replies)
Discussion started by: chebarbudo
2 Replies

10. Shell Programming and Scripting

Passing arguments to a script

I've written a script (bgrep) for a more advanced grep command (& attached a cut down version below). I'm trying allow all grep options to be used, or in any combination. The script works fine if I type say bgrep -i -files product it will return a non-case sensitive list of matches for... (3 Replies)
Discussion started by: Kevin Pryke
3 Replies
Login or Register to Ask a Question