How to check if rule is already in iptables










0















I'm having a problem with my python script that's supposed to check if a certain rule is written in iptables, and if not add it there.



def blockip(packet_ip):

cmd="sudo /sbin/iptables -A INPUT -s "+packet_ip+" -j DROP"
print cmd

if ["$(sudo cat /sbin/iptables --list | grep -- packet_ip)"]:
print "Already Blocked"
else:
subprocess.call(cmd,shell=True)
print packet_ip + " Has been Blocked"


Here is my function that should take care of it but it does not work and writes the same rule multiple times in iptables if same packet_ip is met. Running on RPI btw if it makes a difference.










share|improve this question



















  • 1





    if ["$(command)"] is not the right way to execute a command in python -- it looks like you've gotten shell syntax mixed up with python syntax. Also, that command greps for the literal string packet_ip instead of the value of the variable.

    – John Gordon
    Nov 14 '18 at 19:13












  • I thought that might be the problem but how do i fix it?

    – jakkis
    Nov 14 '18 at 19:17











  • I think adding "shell" or sth similar would make the question easier to be found + answered.

    – stdout
    Nov 14 '18 at 19:18












  • As written, that if statement is always true, so this function should just print the command and then print "Already Blocked". I don't see how it's doing anything else, as you say it is.

    – John Gordon
    Nov 14 '18 at 20:08












  • well how should it be modified to fit my purpose? Error on my part, this did write multiple rules BEFORE i added that if statement. After that it prints "Already Blocked" like you said.

    – jakkis
    Nov 14 '18 at 20:27
















0















I'm having a problem with my python script that's supposed to check if a certain rule is written in iptables, and if not add it there.



def blockip(packet_ip):

cmd="sudo /sbin/iptables -A INPUT -s "+packet_ip+" -j DROP"
print cmd

if ["$(sudo cat /sbin/iptables --list | grep -- packet_ip)"]:
print "Already Blocked"
else:
subprocess.call(cmd,shell=True)
print packet_ip + " Has been Blocked"


Here is my function that should take care of it but it does not work and writes the same rule multiple times in iptables if same packet_ip is met. Running on RPI btw if it makes a difference.










share|improve this question



















  • 1





    if ["$(command)"] is not the right way to execute a command in python -- it looks like you've gotten shell syntax mixed up with python syntax. Also, that command greps for the literal string packet_ip instead of the value of the variable.

    – John Gordon
    Nov 14 '18 at 19:13












  • I thought that might be the problem but how do i fix it?

    – jakkis
    Nov 14 '18 at 19:17











  • I think adding "shell" or sth similar would make the question easier to be found + answered.

    – stdout
    Nov 14 '18 at 19:18












  • As written, that if statement is always true, so this function should just print the command and then print "Already Blocked". I don't see how it's doing anything else, as you say it is.

    – John Gordon
    Nov 14 '18 at 20:08












  • well how should it be modified to fit my purpose? Error on my part, this did write multiple rules BEFORE i added that if statement. After that it prints "Already Blocked" like you said.

    – jakkis
    Nov 14 '18 at 20:27














0












0








0








I'm having a problem with my python script that's supposed to check if a certain rule is written in iptables, and if not add it there.



def blockip(packet_ip):

cmd="sudo /sbin/iptables -A INPUT -s "+packet_ip+" -j DROP"
print cmd

if ["$(sudo cat /sbin/iptables --list | grep -- packet_ip)"]:
print "Already Blocked"
else:
subprocess.call(cmd,shell=True)
print packet_ip + " Has been Blocked"


Here is my function that should take care of it but it does not work and writes the same rule multiple times in iptables if same packet_ip is met. Running on RPI btw if it makes a difference.










share|improve this question
















I'm having a problem with my python script that's supposed to check if a certain rule is written in iptables, and if not add it there.



def blockip(packet_ip):

cmd="sudo /sbin/iptables -A INPUT -s "+packet_ip+" -j DROP"
print cmd

if ["$(sudo cat /sbin/iptables --list | grep -- packet_ip)"]:
print "Already Blocked"
else:
subprocess.call(cmd,shell=True)
print packet_ip + " Has been Blocked"


Here is my function that should take care of it but it does not work and writes the same rule multiple times in iptables if same packet_ip is met. Running on RPI btw if it makes a difference.







python shell grep raspberry-pi






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 19:19







jakkis

















asked Nov 14 '18 at 19:07









jakkisjakkis

64




64







  • 1





    if ["$(command)"] is not the right way to execute a command in python -- it looks like you've gotten shell syntax mixed up with python syntax. Also, that command greps for the literal string packet_ip instead of the value of the variable.

    – John Gordon
    Nov 14 '18 at 19:13












  • I thought that might be the problem but how do i fix it?

    – jakkis
    Nov 14 '18 at 19:17











  • I think adding "shell" or sth similar would make the question easier to be found + answered.

    – stdout
    Nov 14 '18 at 19:18












  • As written, that if statement is always true, so this function should just print the command and then print "Already Blocked". I don't see how it's doing anything else, as you say it is.

    – John Gordon
    Nov 14 '18 at 20:08












  • well how should it be modified to fit my purpose? Error on my part, this did write multiple rules BEFORE i added that if statement. After that it prints "Already Blocked" like you said.

    – jakkis
    Nov 14 '18 at 20:27













  • 1





    if ["$(command)"] is not the right way to execute a command in python -- it looks like you've gotten shell syntax mixed up with python syntax. Also, that command greps for the literal string packet_ip instead of the value of the variable.

    – John Gordon
    Nov 14 '18 at 19:13












  • I thought that might be the problem but how do i fix it?

    – jakkis
    Nov 14 '18 at 19:17











  • I think adding "shell" or sth similar would make the question easier to be found + answered.

    – stdout
    Nov 14 '18 at 19:18












  • As written, that if statement is always true, so this function should just print the command and then print "Already Blocked". I don't see how it's doing anything else, as you say it is.

    – John Gordon
    Nov 14 '18 at 20:08












  • well how should it be modified to fit my purpose? Error on my part, this did write multiple rules BEFORE i added that if statement. After that it prints "Already Blocked" like you said.

    – jakkis
    Nov 14 '18 at 20:27








1




1





if ["$(command)"] is not the right way to execute a command in python -- it looks like you've gotten shell syntax mixed up with python syntax. Also, that command greps for the literal string packet_ip instead of the value of the variable.

– John Gordon
Nov 14 '18 at 19:13






if ["$(command)"] is not the right way to execute a command in python -- it looks like you've gotten shell syntax mixed up with python syntax. Also, that command greps for the literal string packet_ip instead of the value of the variable.

– John Gordon
Nov 14 '18 at 19:13














I thought that might be the problem but how do i fix it?

– jakkis
Nov 14 '18 at 19:17





I thought that might be the problem but how do i fix it?

– jakkis
Nov 14 '18 at 19:17













I think adding "shell" or sth similar would make the question easier to be found + answered.

– stdout
Nov 14 '18 at 19:18






I think adding "shell" or sth similar would make the question easier to be found + answered.

– stdout
Nov 14 '18 at 19:18














As written, that if statement is always true, so this function should just print the command and then print "Already Blocked". I don't see how it's doing anything else, as you say it is.

– John Gordon
Nov 14 '18 at 20:08






As written, that if statement is always true, so this function should just print the command and then print "Already Blocked". I don't see how it's doing anything else, as you say it is.

– John Gordon
Nov 14 '18 at 20:08














well how should it be modified to fit my purpose? Error on my part, this did write multiple rules BEFORE i added that if statement. After that it prints "Already Blocked" like you said.

– jakkis
Nov 14 '18 at 20:27






well how should it be modified to fit my purpose? Error on my part, this did write multiple rules BEFORE i added that if statement. After that it prints "Already Blocked" like you said.

– jakkis
Nov 14 '18 at 20:27













0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307169%2fhow-to-check-if-rule-is-already-in-iptables%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307169%2fhow-to-check-if-rule-is-already-in-iptables%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo