getting the last octet of the UP

14 views
Skip to first unread message

Dave LeFevre

unread,
Apr 24, 2017, 10:17:42 AM4/24/17
to help-cfengine
Howdy folks:

I have a colleague that is replacing some old scripts with cfengine, and the old scripts apparently set some cron jobs based on the last octet of the ipv4 address of the machine.  It does some math and then sets the minute it is run based on that calculation.

To create and example:

10.0.0.45

They would take the 45, do some math and then use the modulus of their math as the minutes in a cron job. 


I've helped him get started with all of this (we know how to use eval) but we are having some problems getting the last octet of the machine.  Going through the cfengine docs I see that there are some ready made sys variables for getting the first, first two, or first three octets, but there are no documented sys variables for the other way around.

Is getting that last octet something we would use a split for?  Doing some string operations like this that would be easy in the shell are not something I have experience with when it comes to cfengine.

Thanks!
--dcl
Dave

Neil H. Watson

unread,
Apr 24, 2017, 10:39:46 AM4/24/17
to help-cfengine
Kind of an XY problem. What is the higher end goal? Is the goal to have
deterministic start time? CFEgine has some native time playing tools:

https://docs.cfengine.com/docs/3.7/reference-functions-splayclass.html
https://docs.cfengine.com/docs/3.10/reference-promise-types-classes.html#dist

And some info on using CFEngine as cron:
http://watson-wilson.ca/blog/tag/cron/

--
Neil H Watson @neil_h_watson
CFEngine reporting: https://github.com/neilhwatson/delta_reporting
CFEngine policy: https://github.com/neilhwatson/evolve_cfengine_freelib
CFEngine and vim: https://github.com/neilhwatson/vim_cf3

Nick Anderson

unread,
Apr 24, 2017, 11:46:11 AM4/24/17
to Dave LeFevre, nick.a...@cfengine.com, help-cfengine

Dave LeFevre <dlef...@gmail.com> writes:

> I've helped him get started with all of this (we know how to use eval) but
> we are having some problems getting the last octet of the machine. Going
> through the cfengine docs I see that there are some ready made sys
> variables for getting the first, first two, or first three octets, but
> there are no documented sys variables for the other way around.
>
> Is getting that last octet something we would use a split for? Doing some
> string operations like this that would be easy in the shell are not
> something I have experience with when it comes to cfengine.

You can use string_split() and nth() to extract the last octet.

bundle agent main
{
  vars:
    "last_octet[$(sys.ip_addresses)]"
      string => nth( string_split($(sys.ip_addresses), "\.", 4), 3);

  reports:
    "Running CFEngine $(sys.cf_version)";
    "The last octect of '$(sys.ip_addresses)' is '$(last_octet[$(sys.ip_addresses)])'";
}
R: Running CFEngine 3.10.1
R: The last octect of '127.0.0.1' is '1'
R: The last octect of '192.168.42.189' is '189'
R: The last octect of '172.17.0.1' is '1'
R: The last octect of '192.168.33.1' is '1'


Nick Anderson
Doer of things, CFEngine

Steven Kreuzer

unread,
Apr 24, 2017, 11:55:53 AM4/24/17
to help-cfengine
You could do something like this in the vars: section

"fourth_octet" string => nth(splitstring("$(sys.ipv4)", "\.", "4"), 3);

However, I agree with Neil that this sounds like an XY problem. What
is your end goal? There might be a more
elegant way to do this.
> --
> You received this message because you are subscribed to the Google Groups
> "help-cfengine" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to help-cfengin...@googlegroups.com.
> To post to this group, send email to help-c...@googlegroups.com.
> Visit this group at https://groups.google.com/group/help-cfengine.
> For more options, visit https://groups.google.com/d/optout.

Aleksey Tsalolikhin

unread,
Apr 24, 2017, 12:06:16 PM4/24/17
to Steven Kreuzer, help-cfengine, Mike Weilgart
You could also use the CFEngine module protocol and e.g. call awk to do your system discovery and text processing.

I agree, would like to know what the 4th octet will be used for. :)

On Apr 24, 2017 11:55 AM, "Steven Kreuzer" <skre...@freebsd.org> wrote:
You could do something like this in the vars: section

"fourth_octet" string => nth(splitstring("$(sys.ipv4)", "\.", "4"), 3);

However, I agree with Neil that this sounds like an XY problem. What
is your end goal? There might be a more
elegant way to do this.

On Mon, Apr 24, 2017 at 10:39 AM, Neil H. Watson
<cfen...@watson-wilson.ca> wrote:
> Kind of an XY problem. What is the higher end goal?  Is the goal to have
> deterministic start time? CFEgine has some native time playing tools:
>
> https://docs.cfengine.com/docs/3.7/reference-functions-splayclass.html
> https://docs.cfengine.com/docs/3.10/reference-promise-types-classes.html#dist
>
> And some info on using CFEngine as cron:
> http://watson-wilson.ca/blog/tag/cron/
>
> --
> Neil H Watson @neil_h_watson
> CFEngine reporting: https://github.com/neilhwatson/delta_reporting
> CFEngine policy:    https://github.com/neilhwatson/evolve_cfengine_freelib
> CFEngine and vim:   https://github.com/neilhwatson/vim_cf3
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "help-cfengine" group.
> To unsubscribe from this group and stop receiving emails from it, send an

> To post to this group, send email to help-c...@googlegroups.com.
> Visit this group at https://groups.google.com/group/help-cfengine.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "help-cfengine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to help-cfengine+unsubscribe@googlegroups.com.

Ted Zlatanov

unread,
Apr 24, 2017, 3:06:44 PM4/24/17
to help-c...@googlegroups.com
I like data_regextract() because it lets me name the extracted elements
and extract multiple things in one shot.

#+begin_src cfengine3
# yields $(parse[last_octet])
"parse" data => data_regextract("(?<last_octet>[0-9]+)$", $(sys.ipv4));
#+end_src

Reply all
Reply to author
Forward
0 new messages