LinuxQuestions.org
Visit Jeremy's Blog.
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 07-20-2011, 12:47 PM   #16
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260

Quote:
Originally Posted by tibix00 View Post
Sorry but I dont really get this. How should I do that?

Tibi
It’s a bash command:

Code:
$ echo *
foo bar
$ set -f
$ echo *
*
$ set +f
$ echo *
foo bar
 
Old 07-20-2011, 12:49 PM   #17
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Maybe you could explain what you actually want to do with your variable CHECK1a ? Or is it you really want to echo it?

Reuti's idea is as follows:
Code:
CHECK1a=$($ipt -nL FORWARD -v | grep $ip)
set -f
echo "$CHECK1a"
set +f
 
Old 07-20-2011, 12:53 PM   #18
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
Quote:
Originally Posted by grail View Post
Maybe you could explain what you actually want to do with your variable CHECK1a ? Or is it you really want to echo it?

Reuti's idea is as follows:
Code:
CHECK1a=$($ipt -nL FORWARD -v | grep $ip)
set -f
echo "$CHECK1a"
set +f
AFAICS it must already be set before the assignment of CHECK1a, although I’m not sure why this is interpreted recursively by the bash.
 
Old 07-20-2011, 01:18 PM   #19
tibix00
LQ Newbie
 
Registered: Jul 2011
Distribution: CentOS 5.6
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Reuti View Post
AFAICS it must already be set before the assignment of CHECK1a, although I’m not sure why this is interpreted recursively by the bash.
Thank you guys for your help and patience with this one.

Basically what I'm trying to do here (do you know a better way?) is a script that checks whether a rule (matching the IP address provided ie a.b.c.d) is active or not. If not the rule would be added/inserted. Checking if the rule exists based only on the IP address being present in the iptables chain is pretty dumb of course, but for now I would be happy for this to work.

I only introduce the echo part to see what $CHECKa returns. What I actually have in mind is something like this:
===============
#!/bin/bash

ipt=/sbin/iptables
ip=a.b.c.d

CHECK1a=$($ipt -nL FORWARD -v | grep $ip) # just for emailing the result

CHECK1b=$($ipt -nL FORWARD -v | grep -c $ip) # to count the matches

if [ $CHECK1b -ne 0 ]; then

echo "$CHECK1a" > ./tmp1.txt
mail -s "Working" some addres < ./tmp1.txt
#rm -f ./tmp1.txt

else

$ipt -I FORWARD -s $ip -j ACCEPT # as example

fi
===================

If you have better ideas on how to check firewall rules and amend it if necessary as per my above (poor) explanation please share.

Tibi
 
Old 07-20-2011, 11:53 PM   #20
troya2
LQ Newbie
 
Registered: Oct 2010
Posts: 3

Rep: Reputation: 0
Quote:
Originally Posted by tibix00 View Post
Sorry but I dont really get this. How should I do that?

Tibi
# Original Script Here

# Command to get result of iptables into CHECK1a
CHECK1a=`$ipt -nL FORWARD -v`

# Output Check1a without filename expansion
set -f
echo $CHECK1a
set +f
 
Old 07-21-2011, 11:40 AM   #21
tibix00
LQ Newbie
 
Registered: Jul 2011
Distribution: CentOS 5.6
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by troya2 View Post
# Original Script Here

# Command to get result of iptables into CHECK1a
CHECK1a=`$ipt -nL FORWARD -v`

# Output Check1a without filename expansion
set -f
echo $CHECK1a
set +f
It's working. Thanks!

Any ideas on other ways to achieve this?

Regards,
Tibi
 
Old 07-21-2011, 11:47 AM   #22
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
I thought that the assignment already expanded the asterisk. But maybe I made a mistake during my tests. You can enclose the variable after the echo in double quotes too to avoid it to be expanded.
 
Old 07-21-2011, 12:23 PM   #23
tibix00
LQ Newbie
 
Registered: Jul 2011
Distribution: CentOS 5.6
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Reuti View Post
I thought that the assignment already expanded the asterisk. But maybe I made a mistake during my tests. You can enclose the variable after the echo in double quotes too to avoid it to be expanded.
Thanks!

Any other ideas for such a script? To check rules and amend if needed.

Perhaps a different approach?

Tibi
 
  


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
Cygwin issue with bash script jdwilder Other *NIX 7 09-20-2013 05:53 AM
[SOLVED] Strange issue with Bash script and resolution Jinx-Wolf Programming 7 03-19-2011 06:03 PM
Bash script debugging issue internetSurfer Programming 4 01-28-2008 08:24 PM
[SOLVED] issue with variable in bash script angel115 Programming 4 08-21-2006 01:42 PM
small bash script issue zoomzoom Linux - General 7 06-08-2004 06:33 PM

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

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