{"id":425,"date":"2017-02-15T16:04:22","date_gmt":"2017-02-15T15:04:22","guid":{"rendered":"http:\/\/www.nax.cz\/?p=425"},"modified":"2017-02-15T16:04:22","modified_gmt":"2017-02-15T15:04:22","slug":"add-command-to-rc-local","status":"publish","type":"post","link":"https:\/\/nax.cz\/?p=425","title":{"rendered":"add command to rc.local"},"content":{"rendered":"<p>Yesterday I was working on simple install script which was suppose to insert a command to rc.local. It is more complicated matter than you would think. Sometimes, there is already &#8222;exit 0&#8220; and then you don&#8217;t want to end up after that command. You also don&#8217;t want to add your command twice there. Here is what I came up with (in python):<\/p>\n<pre>\nimport sys\n\nrclocal = '\/etc\/rc.local'\niptables_redirect = \"\"\"iptables -t nat -A PREROUTING \\\n-i eth0 -p tcp --dport 80 -j REDIRECT --to-ports 8000\"\"\"\nedited_file = [ ]\nif iptables_redirect not in open(rclocal).read():\n    with open(rclocal) as f:\n       lines  = f.readlines()\n    for l in lines:\n      if l == \"exit 0\\n\":\n        edited_file.append(iptables_redirect + \"\\n\")\n      edited_file.append(l)\n    with open(rclocal,\"w\") as f:\n        for l in edited_file:\n            f.write(\"%s\"%l)\n\nif iptables_redirect not in open(rclocal).read():\n    with open(rclocal,'a+') as f:\n        f.write(iptables_redirect+'\\n')\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday I was working on simple install script which was suppose to insert a command to rc.local. It is more complicated matter than you would think. Sometimes, there is already &#8222;exit 0&#8220; and then you don&#8217;t want to end up after that command. You also don&#8217;t want to add your command twice there. Here is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[],"class_list":["post-425","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/nax.cz\/index.php?rest_route=\/wp\/v2\/posts\/425","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nax.cz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nax.cz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nax.cz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nax.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=425"}],"version-history":[{"count":0,"href":"https:\/\/nax.cz\/index.php?rest_route=\/wp\/v2\/posts\/425\/revisions"}],"wp:attachment":[{"href":"https:\/\/nax.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nax.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nax.cz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}