WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: bootlocal.sh is not executing my iptables rule  (Read 1740 times)

Offline kh335m

  • Newbie
  • *
  • Posts: 28
bootlocal.sh is not executing my iptables rule
« on: November 10, 2023, 09:13:13 AM »
Hi,
My bootlocal.sh is not executing myfw.sh file during boot time. here is my bootlocal.sh

Code: [Select]
#!/bin/sh
# put other system startup commands here
/opt/myfw.sh

and here is myfw.sh

Code: [Select]
#!/bin/sh
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -s x.x.x.x -j ACCEPT
iptables -A INPUT -s x.x.x.x -j ACCEPT
iptables -A INPUT -s x.x.x.x -j ACCEPT
iptables -A INPUT -j DROP
iptables -A FORWARD --j DROP

I can manually execute the bootlocal.sh file, there is no problem but did not execute during boot. Where is the issue?

Thanks

    [Edit]: Added code tags.  Rich
« Last Edit: November 10, 2023, 09:24:13 AM by Rich »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11516
Re: bootlocal.sh is not executing my iptables rule
« Reply #1 on: November 10, 2023, 09:27:33 AM »
Hi kh335m
Is  iptables.tcz  listed in your  onboot.lst  file?
Does  iptables  need the network to be up in order to work?

Offline kh335m

  • Newbie
  • *
  • Posts: 28
Re: bootlocal.sh is not executing my iptables rule
« Reply #2 on: November 10, 2023, 11:05:06 AM »
Hi,
The iptables installed and it is in the onboot.ist list. when I run "iptables -L" , I can see bellow

Code: [Select]
#iptables -L
Chain   INPUT (policy ACCEPT)
target       prot      opt   SOURCE           destination

Chain   FORWARD (policy ACCEPT)
target       prot      opt   SOURCE           destination

Chain   OUTPUT (policy ACCEPT)
target       prot      opt   SOURCE           destination

    [Edit]: Added code tags.  Rich
« Last Edit: November 10, 2023, 12:11:48 PM by Rich »

Offline kh335m

  • Newbie
  • *
  • Posts: 28
Re: bootlocal.sh is not executing my iptables rule
« Reply #3 on: November 10, 2023, 11:58:00 AM »
Still no luck

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11516
Re: bootlocal.sh is not executing my iptables rule
« Reply #4 on: November 10, 2023, 12:09:44 PM »
Hi kh335m
Try changing  bootlocal.sh  to this:
Code: [Select]
#!/bin/sh
# put other system startup commands here

# Wait for network to come up
SEC="60"
while [ $SEC -gt 0 ]
do
        SEC=$(($SEC - 1))
        ifconfig | grep -q "Bcast:" && break
        sleep 1
done

if [ $SEC -gt 0 ]
then
        echo "$(date +"%m-%d-%y %H:%M:%S") : Starting /opt/myfw.sh" >> /opt/myfw.err
        /opt/myfw.sh
        echo "$(date +"%m-%d-%y %H:%M:%S") : Finished /opt/myfw.sh" >> /opt/myfw.err
else
        echo "$(date +"%m-%d-%y %H:%M:%S") : Network interface is down" >> /opt/myfw.err
fi

Then see what got logged to  /opt/myfw.err.

Offline CardealRusso

  • Full Member
  • ***
  • Posts: 178
Re: bootlocal.sh is not executing my iptables rule
« Reply #5 on: November 10, 2023, 02:22:36 PM »
Then see what got logged to  /opt/myfw.err.

I came to recommend the same. I tried to replicate and it didn't work, after adding
Code: [Select]
/opt/test.sh > /tmp/log.txt 2>&1I got a permissions error message, which was fixed with chmod u+x in test.sh
I didn't know that the files needed proper permissions, since they run as root
« Last Edit: November 10, 2023, 02:24:29 PM by CardealRusso »

Offline patrikg

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 694
Re: bootlocal.sh is not executing my iptables rule
« Reply #6 on: November 10, 2023, 03:33:49 PM »
I think you missed just to set the execution bit with the script.
If you typed in that command with the root user you should see with the ls -l command that the x is there for the root user.

The kernel need to know if this file is executable. And what she bang interpreter it should execute.
Like something like this

Code: [Select]
#/bin/bash
Code: [Select]
#/bin/sh
You can also use some other interpreters like python.
Code: [Select]
#/bin/env python3

Offline kh335m

  • Newbie
  • *
  • Posts: 28
Re: bootlocal.sh is not executing my iptables rule
« Reply #7 on: November 10, 2023, 09:09:09 PM »
is there any other way to run the script from boot?

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11516
Re: bootlocal.sh is not executing my iptables rule
« Reply #8 on: November 10, 2023, 11:39:34 PM »
Hi kh335m
Sure. If you are running a GUI, create a file in
~/.X.d/  (any name you want) containing:
Code: [Select]
/opt/myfw.sh &Add sudo if that's needed. The contents of the
file will be automatically executed after the
GUI is started.

If you are strictly in text mode, add the above
command to the end of  ~/.ashrc  instead.

Offline kh335m

  • Newbie
  • *
  • Posts: 28
Re: bootlocal.sh is not executing my iptables rule
« Reply #9 on: November 11, 2023, 08:31:09 AM »
Here is my question -- I have bootlocal.sh and myfw.sh, but when I reboot the tinycore, I am not seeing any output the error log, and also all entries are gone from bootlocal.sh (empty) .
That is wired or am I doing wrong.
« Last Edit: November 11, 2023, 09:05:38 AM by kh335m »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11516
Re: bootlocal.sh is not executing my iptables rule
« Reply #10 on: November 11, 2023, 11:16:48 AM »
Hi kh335m
Restore your  myfw.sh  and  bootlocal.sh  files.
Then:
Code: [Select]
echo "opt/bootlocal.sh" >> /opt/.filetool.lst
echo "opt/myfw.sh" >> /opt/.filetool.lst
filetool.sh -b

Reboot and see if things are any better.

Offline kh335m

  • Newbie
  • *
  • Posts: 28
Re: bootlocal.sh is not executing my iptables rule
« Reply #11 on: November 11, 2023, 12:17:30 PM »
Thanks. it works.