LinuxQuestions.org
Review your favorite Linux distribution.
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 04-01-2023, 10:56 PM   #1
blason
Member
 
Registered: Feb 2016
Posts: 122

Rep: Reputation: Disabled
bash script - unable to run single ARG and usage is being shown


Hi team,

Here is one more and I stuck in loop.

I am modifying the script which shows IP addresses blocked on firewall.
However it has parameters with certain flags must on and I wanted to run a particular flag without $2 or $3 flag and it has to run as a individual flag.

Here are the codes.

Code:
#help info
usage() {
        echo "Usage: $0 -a <on|off|stat|allow> [ -l <IP_Address> ] [ -al <IP_Address>] [-g <gw_list_file>] [-b <bypass_file>] [-f <feed_file>] [-s <script_file>] [ -t total IP Addresses ]" 1>&2;
        echo "Option:" 1>&2;
        echo "  -a on: activate blocking the IP addresses in the feeds" 1>&2;
        echo "  -a off: stops blocking the IP addresses in the feeds" 1>&2;
        echo "  -a stat: prints the feature status of each GW" 1>&2;
        echo "  -a allow: activate bypass for given IP addresses even if they are on the blocking feeds" 1>&2;
        echo "  -a delete_bypass: deactivate bypass list" 1>&2;
        echo "  -l find the given address on particular firewall" 1>&2;
        echo "  -al fine the given address on all firewalls" 1>&2;
        echo "  -t Shows Total number of IP Addresses Blocked" 1>&2;
        echo "  -g gw_list_file: a list of GW IPs" 1>&2;
        echo "  -b bypass_file: a list of IPs to bypass" 1>&2;
        echo "  -f feed_file: a list of feeds URLs with IPs to block" 1>&2;
        echo "  -s script_file: full path to ip_block.sh to copy to the GWs" 1>&2;
        echo "Example:" 1>&2;
        echo "  $0 -a on -g local_gw_file -f local_feed_list -l <IP_Address> -al <All_FW_IPs>"
        exit 1;
}
while getopts ":a:g:b:f:s:t" o; do
        case "${o}" in
        a)
                        op=${OPTARG}
            ((op == "on" || op == "off" || op == "stat" || op == "allow" || op == "delete_bypass")) || usage
            ;;
        g)
            gw_list_file=${OPTARG}
            ;;
                b)
            local_bypass_file=${OPTARG}
            ;;
                f)
            local_feed_file=${OPTARG}
            ;;
                s)
                        script_file=${OPTARG}
                        ;;
        t) total_ips_on_all_fws
                ;;
        *)
            usage
            ;;
    esac
done
shift $((OPTIND-1))


if  [ -z "$op" ]
then
        usage
fi

if  [ -z "$gw_list_file" ]
then
        echo "Error: missing gw list file" 1>&2;
        usage
fi
What I wanted to achieve is

Code:
# isn-threat-activate -t
On Firewall 192.168.5.16 Total IP Addresses Blocked are: 183284

While below things are happening and I am not sure why!!

Code:
[Expert@MUM-MGMT:0]# isn-threat-activate -t
On Firewall 192.168.5.16 Total IP Addresses Blocked are: 183284

Error: missing gw list file
Usage: /opt/CPshrd-R81.10/bin/isn-threat-activate -a <on|off|stat|allow> [ -l <IP_Address> ] [ -al <IP_Address>] [-g <gw_list_file>] [-b <bypass_file>] [-f <feed_file>] [-s <script_file>] [ -t total IP Addresses ]
Option:
        -a on: activate blocking the IP addresses in the feeds
        -a off: stops blocking the IP addresses in the feeds
        -a stat: prints the feature status of each GW
        -a allow: activate bypass for given IP addresses even if they are on the blocking feeds
        -a delete_bypass: deactivate bypass list
  -l find the given address on particular firewall
  -al fine the given address on all firewalls
  -t Shows Total number of IP Addresses Blocked
        -g gw_list_file: a list of GW IPs
        -b bypass_file: a list of IPs to bypass
        -f feed_file: a list of feeds URLs with IPs to block
        -s script_file: full path to ip_block.sh to copy to the GWs
Example:
        /opt/CPshrd-R81.10/bin/isn-threat-activate -a on -g local_gw_file -f local_feed_list -l <IP_Address> -al <All_FW_IPs>
```

Even after I gave those parameters which script is complaining I am getting same error
```
[Expert@MUM-MGMT:0]# isn-threat-activate -t -g /usr/share/isnti/fw-ips.txt -f /usr/share/isnti/isnfeeds.txt
On Firewall 192.168.5.16 Total IP Addresses Blocked are: 183284

Usage: /opt/CPshrd-R81.10/bin/isn-threat-activate -a <on|off|stat|allow> [ -l <IP_Address> ] [ -al <IP_Address>] [-g <gw_list_file>] [-b <bypass_file>] [-f <feed_file>] [-s <script_file>] [ -t total IP Addresses ]
Option:
        -a on: activate blocking the IP addresses in the feeds
        -a off: stops blocking the IP addresses in the feeds
        -a stat: prints the feature status of each GW
        -a allow: activate bypass for given IP addresses even if they are on the blocking feeds
        -a delete_bypass: deactivate bypass list
  -l find the given address on particular firewall
  -al fine the given address on all firewalls
  -t Shows Total number of IP Addresses Blocked
        -g gw_list_file: a list of GW IPs
        -b bypass_file: a list of IPs to bypass
        -f feed_file: a list of feeds URLs with IPs to block
        -s script_file: full path to ip_block.sh to copy to the GWs
Example:
        /opt/CPshrd-R81.10/bin/isn-threat-activate -a on -g local_gw_file -f local_feed_list -l <IP_Address> -al <All_FW_IPs>

Eventually I want to run -t flag individually
 
Old 04-02-2023, 03:05 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,039

Rep: Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347
use shellcheck to check your script, that may help you to fix issues.
use default values for options you do not want to specify.
Otherwise I have no idea what is it and how does it work and how can it be configured.
 
Old 04-02-2023, 06:47 AM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by blason View Post

Even after I gave those parameters which script is complaining I am getting same error
You didn't give the -a <op> parameter, so the error looks expected to me, from this part:

Code:
if  [ -z "$op" ]
then
        usage
fi
By the way, the ((op == "on" || ... )) || usage part is wrong; ((...)) checks for numeric equality, not string equality.
 
Old 04-04-2023, 12:21 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,369

Rep: Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753
I'd start with this :
Code:
Error: missing gw list file

also, for debugging, i like to use
Code:
set -xv
as the 2nd line in the script ie next line after #!....
 
Old 04-04-2023, 04:11 AM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,832

Rep: Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218
If you compare one variable against several strings then consider a case-esac
Code:
            case $op in
            (on|off|stat|allow|delete_bypass)
            ;;
            (*)
                usage
            esac
If matched then do nothing else run usage.
 
  


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
usb keyboard identified and events shown but key character not shown on the prompt flyxtop Linux - Software 0 11-30-2013 07:45 AM
[SOLVED] Arg! Virtual machine, sudo won't work, can't log in as root, visudo won't work Arg! edpatterson Linux - Newbie 2 01-03-2012 05:33 PM
User shown as "already exist" when there is no user directory shown on home directory Sharpeye Linux - Newbie 3 03-18-2009 01:17 AM
While booting no option is shown for Linux! Only windows xp is shown!! mon avis Linux - General 7 08-06-2006 04:14 PM
arg. checking help in a script kirmet Programming 12 09-12-2005 01:02 AM

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

All times are GMT -5. The time now is 05:46 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