Update (2011-04-22): Zach let me know in the meantime that there's a much easier way to implement stickfile in BASH:
0
1
2
3
| while true; do
inotifywait -q -q -e modify -e delete $2
cp $1 $2
done |
Moral of the story: I should have searched for inotify command line which would lead me to inotify-tools which contains inotifywait.
And now to the original post:
My employer uses SonicWALL NetExtender for his VPN needs. Saying that I'm not a fan of IPsec would be definitely an understatement, but my major problem is that NetExtender overwrites my resolv.conf upon every connection which screws the hostname resolution on my LAN from my laptop. chmoding or chowning resolv.conf doesn't help because it gets re-chowned and re-chmoded by NetExtender.
I was thinking about overwriting resolv.conf on a regular basis from a script but it seemed rather inelegant. But how should I do it otherwise? With inotify, of course.
Here's the script I've written which you should save as "stickfile" to a directory that is featured in your $PATH.
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
| #!/usr/bin/env python
import os
from sys import argv, exit
from shutil import copyfile
from pyinotify import *
def add_watch():
global watch_manager, watch_descriptor, file_to_stick
watches = watch_manager.add_watch(file_to_stick, IN_MODIFY | IN_DELETE_SELF)
watch_descriptor = watches[file_to_stick]
def remove_watch():
global watch_manager, watch_descriptor
watch_manager.rm_watch(watch_descriptor)
class MyProcessEvent(ProcessEvent):
def process_IN_DELETE(self, event):
self.stick_file()
def process_IN_MODIFY(self, event):
self.stick_file()
def stick_file(self):
global file_to_stick, file_to_clone
print 'Sticking %s as %s' % (file_to_clone, file_to_stick)
remove_watch()
copyfile(file_to_clone, file_to_stick)
add_watch()
if __name__ == '__main__':
if len(argv) != 1+2:
program_name = argv[0]
print 'Usage %s: FILE-TO-CLONE FILE-TO-STICK' % (program_name)
exit(1)
file_to_clone = argv[1]
file_to_stick = argv[2]
watch_manager = WatchManager()
notifier = Notifier(watch_manager, MyProcessEvent())
add_watch()
while True:
try:
notifier.process_events()
if notifier.check_events():
notifier.read_events()
except KeyboardInterrupt:
notifier.stop()
break |
After I created a valid resolv.conf and saved it as /etc/resolv.conf.orig I only had to execute the following as root before starting up NetExtender:
0
| stickfile /etc/resolv.conf.orig /etc/resolv.conf |