A
A
Alexey2010-12-12 23:52:40
linux
Alexey, 2010-12-12 23:52:40

Ubuntu 10.10 bash script containing rules for NAT?

Hello.
Based on the most useful topic , I configured myself in parallel (with balancing) a pair of PPPoE connections with two local providers.
The author of the topic proposed two scripts: the first one for setting routes and masquerading (NAT for distributing the Internet to other local computers), the second one for checking the connection by pings either on one or another interface and, correspondingly, (re)setting gateways (depending on the passage pings, the gateway of the first provider, the second one, or both with balancing is turned on). The second script at its very beginning simply calls the first one. The scripts themselves can be viewed in their entirety in that topic .
Everything worked out great. Available channels work, the Internet is distributed to the local area.
But the idyll continued while I ran the handles in the console: # /etc/balance/check.sh
And oddities began after a completely natural step. To make everything rise by itself after the reboot, I added to the crontab for root:
@reboot /etc/balance/check.sh
After the system boots, I check and see:

# ps ax | grep che<br/>
 1261 ? Ss 0:00 /bin/sh -c /etc/balance/check.sh<br/>
 1262 ? S 0:00 /bin/bash /etc/balance/check.sh

That is, the script would run.
And there is Internet on the machine itself (both on ppp1 and ppp2 - everything is fine), but on eth0 (in the local area) - it is not distributed .
At the same time, as soon as I do again with my hands in the console what, it would seem, has already been done by cron:
# /etc/balance/check.sh
And at first I see the following message:
RTNETLINK answers: File exists
And then the script output begins (“Both channels”, etc.) eth0 the internet appears.
I assumed that the script is being executed too “early” (for example, there is nothing to masquerade at the time of its execution), put this in the crontab:
5 23 * * * /etc/balance/check.sh
And restarted the machine almost exactly at 23:00. The output is redirected to a file. I looked at 23:10 - the script started writing to the log on time, i.e. the script started in a timely manner, but the Internet is not distributed. After starting it manually in the console, everything is ok again.
Tell me, please, what is the magic?
And, ideally, tell me the correct way to start all this economy at boot and automatically restart if the script suddenly dies sometime.
Thank you.
PS I am publishing this question in addition to the comment in the topic, since the author of the topic did not appear on Habré for a couple of days (and only the author receives a notification about a new top-level comment).
PPS I asked this question on Habré, and not on any forum, because I also found a valuable manual in my situation only on Habré, and not on any forum.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
P
peter23, 2010-12-13
@kostin

Write the full path to all used binaries: /sbin/ip, /sbin/iptablesetc.
cron, when executing commands, does not pass the PATH environment variable or passes it a truncated one that does not contain /sbin/.

A
admin4eg, 2010-12-13
@admin4eg

Run the script in cron using sudo
sudo /etc/balance/check.sh
directly like this in cron

V
Vsevolod, 2010-12-13
@sevka_fedoroff

Try to redirect strerr somewhere else. In theory, if an error occurs in the cron-skrit, and there is nowhere to output the message, the script will stop. stderr is redirected with the 2> command, for example:
check.sh >/dev/null 2>/dev/null

V
Veshij, 2010-12-13
@Veshij

The point may be that at the time the script is executed, you still may not have a network on the machine.
To debug, put !/bin/sh -x in the shaban and redirect the output to the log, as the friend advised above.
If justified, run the script with an init script after networking.

A
Alexey, 2010-12-13
@kostin

So.
You must either add sudobefore calling the script from the crontab.
Or write absolute paths /sbin/ipin /sbin/iptablesscripts.
Because the environment variable PATHdoesn't seem to be passed to cron when running /sbin.
Many thanks to everyone who helped here to solve the problem.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question