Forums

Resolved
1 votes
Hi All.
Problem
-------------
I have a dynamic IP address
I wish to write a program (python) that LOGS this IP address into a DB upon every boot/reboot (sqlite)

Questions
--------------------------
What is the best way to find IP address, is there any better way than ipconfig and searching for it
How do I get SAID program to run on every boot/reboot. I want it to run last, and exit correctly

Many thanks
z4ck
Thursday, June 14 2018, 12:14 PM
Share this post:

Accepted Answer

Sunday, June 17 2018, 03:36 PM - #Permalink
Resolved
0 votes
I had a feeling that there would be a load of variables automatically attached to the ip-up script. Searching around the internet I quickly bumped into this which indicates the IP address should be directly available using the variable $4 rather than the big ifconfig command. If you had MultiWAN you'd also want to log the interface with $1 so you knew which interface was coming up.

If you then google around you'll find many variants of how to get the date and time in bash - perhaps google "date time bash". I already have it in one of by scripts:
$(date +%Y-%m-%d" "%H:%M:%S)
So how about for your command something like:
echo $(date +%Y-%m-%d" "%H:%M:%S) $1 $4 >> my_file.txt
You can make the command more complex if you want to filter bits and so on.

I can't check any of this as my external connection is DHCP and not PPPoE.
The reply is currently minimized Show
Responses (7)
  • Accepted Answer

    Monday, June 18 2018, 10:09 AM - #Permalink
    Resolved
    0 votes
    Thankyou NIck.
    You have done most of the heavy lifting for me.
    Did what you suggested and working fine (did a few reboots to test). Thanks.

    Thank god, i did not have to do it via loading a program into memory, and polling connection every X minutes. this is a much better way to achieve the result I am after.
    Thank you for your time.
    zack
    The reply is currently minimized Show
  • Accepted Answer

    Sunday, June 17 2018, 02:27 PM - #Permalink
    Resolved
    0 votes
    Nick Thankyou.

    You're instincts are correct, I do wish to have a date/timestamp. among other things.
    but this is a great start.(tested and works) even this would be semi satisfactory. Is there anyway to add a date/time to this?

    thanks for your help.
    The reply is currently minimized Show
  • Accepted Answer

    Saturday, June 16 2018, 12:14 PM - #Permalink
    Resolved
    0 votes
    After your first couple of posts you are allowed to post without approval so you should be OK now.

    No you don't have to do what you are proposing, but you can if you insist. If you run your program from the /etc/ppp/ip-up/local file just by adding a line to the file. /etc/ppp/ip-up and /etc/ppp/ip-up.local should only fire each time PPPoE gets a new IP. No need to have something sitting in memory looping.

    Have a look at bash scripting. If all you want to do is add an IP address to a file, you can do it in a single line:
    echo `ifconfig enp2s0 | awk '/inet / { gsub(".*:", "", $2) ; print $2 }'` >> my_file.txt
    You could add the line to the end of the /etc/ppp/ip-up/local file or to a separate file and call the file from /etc/ppp/ip-up/local.

    I have a feeling you may want more than that, perhaps at a minimum with a date/timestamp. Also that is only building a text file of IP's rather than some sort of conventional database.
    The reply is currently minimized Show
  • Accepted Answer

    Saturday, June 16 2018, 10:32 AM - #Permalink
    Resolved
    0 votes
    Yeah Sorry Nick,I'm new so I think I not permitted to post without approval.
    I realized about 30 seconds after I posted that I would like to have it run every time the IP changes. As sometimes my connection drops from my rsp/isp and I wish to log this new ip.

    Sorry for the confusion,

    So it would be better to have a program loaded in memory and testing every X minutes for a connection.

    Initially, all I wished to achieve is ro run a program on boot. all this program would do is get the IP address and add this to a DB.

    I guess what I want is, to have a program load on boot, sit in memory and test the IP address every Xminutes /or whenever it changes then, if the ip address has changed log this NEW ip in DB

    something like machine boots/reboots
    After IP is assigned via PPPoE
    the program runs, then logs that IP to DB

    program checks every x minutes for a IP, if it has changed since last IP log to DB else do nothing

    Just not sure how to get a python program to run on boot? (or even if python is the best solution?)
    or how to test for ip change every x minutes? or if there is a better way to do this?


    Hope that is a bit clearer .

    Thanks
    zack
    The reply is currently minimized Show
  • Accepted Answer

    Friday, June 15 2018, 10:57 AM - #Permalink
    Resolved
    0 votes
    ClearOS will always test if you're connected to the internet and, if not, try to establish a connection. That is the purpose of the syswatch daemon.

    If you want something to always run on boot, you can call it from /etc/rc.d/local but you have to make /etc/rc.d/local executable ("chmod u+x /etc/rc.d/local"). The only danger is you can't control when it runs. Looking at my boot log and messages log it looks like mine is running almost immediately after the network comes up.

    What is it exactly that you are trying to do?

    Also do you want to do it only on boot or every time your IP changes?
    The reply is currently minimized Show
  • Accepted Answer

    Friday, June 15 2018, 09:34 AM - #Permalink
    Resolved
    0 votes
    Thanks Nick.
    I'll be trying that!
    I am using PPPoE.
    You may be right that having it run last is not really important.
    I was hoping not to have to have it ALWAYS loaded in memory testing if it's connected to INET. But I can see how that would work.
    Was hoping it just have A PROGRAM run on boot? (like windows startup) I'm far from a linux person. Is this possible?
    somehow insert the program into the startup boot process. excuse my lack of working linux knowledge.
    again thank you for a couple of ideas.
    zack.
    The reply is currently minimized Show
  • Accepted Answer

    Thursday, June 14 2018, 03:31 PM - #Permalink
    Resolved
    0 votes
    You can pick out your IP address with something like:
    ifconfig enp2s0 | awk '/inet / { gsub(".*:", "", $2) ; print $2 }'
    replacing enp2s0 with your interface. I have no idea how to get it to run as the last thing during startup as startup is asynchronous. You'll have to search the systemd/systemctl manuals and perhaps use something like a network-online target.
    For a different approace, depending if you use PPPoE or DHCP there are different exit hooks you can latch onto. For PPPoE have a look at /etc/ppp/ip-up and for DHCP have a look at dhclient-exit-hooks, perhaps with the reasons "BOUND" and "REBOOT". They should trigger every time you IP changes or gets renewed.
    The reply is currently minimized Show
Your Reply