Identify all relevant ip addresses from ruby Socket.ip_address_list - ruby

When using rubies Socket.ip_address_list, it will return an array of addr_info (https://ruby-doc.org/stdlib-2.0.0/libdoc/socket/rdoc/Addrinfo.html)
Example:
require 'socket'
addr_infos = Socket.ip_address_list
This array could be iterated and listed by all defined ip_addresses and attributes like
addr_infos.each do |addr_info|
puts "#{addr_info.ip_address}
#{addr_info.ipv4? ? 'ipv4? ' : ''}" +
"#{addr_info.ipv4_loopback? ? 'ipv4_loopback? ' : ''}" +
"#{addr_info.ipv4_private? ? 'ipv4_private? ' : ''}" +
"#{addr_info.ipv4_multicast? ? 'ipv4_multicast? ' : ''}" +
"#{addr_info.ipv6? ? 'ipv6? ' : '' }" +
"#{addr_info.ipv6_loopback? ? 'ipv6_loopback? ' : ''}" +
"#{addr_info.ipv6_linklocal? ? 'ipv6_linklocal? ' : ''}" +
"#{addr_info.ipv6_multicast? ? 'ipv6_multicast? ' : ''}" +
"#{addr_info.ipv6_sitelocal? ? 'ipv6_sitelocal? ' : ''}" +
"#{addr_info.ipv6_unique_local? ? 'ipv6_unique_local? ' : ''}" +
"#{addr_info.ipv6_mc_global? ? 'ipv6_mc_global? ' : ''}" +
"#{addr_info.ipv6_unspecified? ? 'ipv6_unspecified? ' : ''}"
end
The result will look like
127.0.0.1
ipv4? ipv4_loopback?
192.168.178.33
ipv4? ipv4_private?
1.2.4.5
ipv4?
::1
ipv6? ipv6_loopback?
fe80::1%lo0
ipv6? ipv6_linklocal?
fe80::aede:48ff:fe00:1122%en5
ipv6? ipv6_linklocal?
fe80::68:e785:4cfb:41e6%en0
ipv6? ipv6_linklocal?
fe80::50fc:46ff:fe4c:c2b4%awdl0
ipv6? ipv6_linklocal?
fe80::3203:d609:ff08:151d%utun0
ipv6? ipv6_linklocal?
fd00::ffff:aaaa:bbbb:7005
ipv6? ipv6_unique_local?
2003:ffff:4723:aaaa:bbbb:8888:269a:42a4
ipv6?
Q: How to identify "correct" ip-addresses to bind listening services to?
IMHO it would be easy to identify the IPv4 addresses like:
IPv4 = ipv4? && (ipv4_loopback? || ipv4_private? || !(ipv4_loopback? || ipv4_private? || ipv4_multicast?))
But in case of those many ipv6_? attributes I wondering what to to check to identify the IPv6 addresses.
Is this the correct suggestion?
It is an IPv6 address when:
a. ipv6? is true and no other ipv6_...? attribute is true
b. ipv6? and ipv6_loopback? are true
c. ipv6? and ipv6_unique_local are true
Do I miss something from IPv6 addresses?

This is my current solution in addition to this question. Maybe some may add a comment or vote to make sure that this goes the right direction
ip_addresses_for_host = []
Socket.ip_address_list.each do |a|
# test for all local valid ipv4 and ipv6 ip_addresses
# check question on stackoverflow for details
# https://stackoverflow.com/questions/59770803/identify-all-relevant-ip-addresses-from-ruby-socket-ip-address-list
ip_addresses_for_host << a.ip_address if \
(a.ipv4? &&
(a.ipv4_loopback? || a.ipv4_private? ||
!(a.ipv4_loopback? || a.ipv4_private? || a.ipv4_multicast?)
)
) ||
(a.ipv6? &&
(a.ipv6_loopback? || a.ipv6_unique_local? ||
!(a.ipv6_loopback? || a.ipv6_unique_local? || a.ipv6_linklocal? || a.ipv6_multicast? || a.ipv6_sitelocal? ||
a.ipv6_mc_global? || a.ipv6_mc_linklocal? || a.ipv6_mc_nodelocal? || a.ipv6_mc_orglocal? || a.ipv6_mc_sitelocal? ||
a.ipv6_v4compat? || a.ipv6_v4mapped? || a.ipv6_unspecified?)
)
)
end
This will resolve for IPv4:
a. 127.0.0.1
b. private addresses like 192.168./16, 10./8, ...
c. other ipv4 addresses like 1.2.3.4
and for IPv6:
a. ::1
b. fc00::
c. abcd::
Appreciate if someone would add more knowledge.

Related

Bash - increment IPv6 address by 1

I'm currently experimenting with a little bash script.
I have an IPv6 address (via mysql query) in a variable and want to increment it by 1 (by also honoring the IPv6 rules [hex]). Any hint how to achieve this?
Is there a simple way in bash to increment a given address by 1? (this is to avoid duplicates).
Basically:
2001::0000:fe04 is given and I want the script to manipulate the variable to output 2001::0000:fe05, 2001::0000:fe06 and after 2001::0000:fe09 it should be 2001::0000:fe0a - 2001::0000:fe0f followed by 2001::0000:fe10 (hex count). If all ip's are full (2001::0:ffff) it should use the next IP subnet by honoring IPv6 subnetting.
So after 2001::0:ffff the next address would be 2001::0001:0001
Hope you understand what I mean ;)
This will add 1 to your ip6 address using the Net::IP perl package.
#!/usr/bin/env perl
use strict;
use Net::IP qw(ip_inttobin ip_bintoip);
my $ip = Net::IP->new('2001::0000:fe05') || die;
print ("IP : ".$ip->ip()."\n");
print ("Sho : ".$ip->short()."\n");
print ("Int : ".$ip->intip()."\n");
# add 1 to the address
my $next_bin = ip_inttobin($ip->intip + 1, $ip->version);
my $next = Net::IP->new(ip_bintoip($next_bin, $ip->version));
print "\nAfter incrementing by 1\n";
print ("IP : ".$next->ip()."\n");
print ("Sho : ".$next->short()."\n");
$ perl ip6_test.pl
IP : 2001:0000:0000:0000:0000:0000:0000:fe05
Sho : 2001::fe05
Int : 42540488161975842760550356425300311557
After incrementing by 1
IP : 2001:0000:0000:0000:0000:0000:0000:fe06
Sho : 2001::fe06

VFP CHR(9) not outputting TAB as expected

I need to create a tab-delimited file from a cursor. I would normally just use COPY TO but in this case I need to a header row which COPY TO doesn't create.
I thought I could use ?|?? along with CHR(9) but it doesn't put a TAB in the file. I opened the file with notepad++ and word with show special characters turned on. I expected to see the right-arrow for TAB but not there. The file looked more like a fixed width format
Here's my code
LOCAL lcWBSIncFile, lcEACIncHeader
lcEACIncHeader = "EarnedValue" + CRLF +;
"ContrName" + CHR(9) + ;
"StruName" + CHR(9) + ;
"WbsNum" + CHR(9) + ;
"EndDate" + CHR(9) + ;
"UnitName" + CHR(9) + ;
"UnitScale" + CHR(9) + ;
"LRE"
lcWBSIncFile = RTRIM(vpcProgram) + "_" + dtoc(vpdStatusDate,1) + "_" + TTOC(DATETIME(),1) + "_WBS_EAC.inc"
SET ALTERNATE TO ( lcWBSIncFile )
SET ALTER ON
SET CONSOLE OFF
? lcEACIncHeader
SELECT _csrEACUpdateWBSFinal
SCAN
? contrname + CHR(9)
?? struname + CHR(9)
?? wbsnum+ CHR(9)
?? enddate + CHR(9)
?? unitname + CHR(9)
?? unitscale + CHR(9)
?? ALLT(STR(lre,24,2 ))
ENDSCAN
SET ALTERNATE TO
SET ALTERNATE OFF
COPY TO (filename) TYPE DELIMITED WITH TAB works fine, I just don't get a header.
? | ?? didn't work but FPUTS() did. Not sure why.

Apex : How to generate modal page URL?

I have the following code that generates a normal APEX page URL :
PageURL := APEX_UTIL.PREPARE_URL(p_url => 'f?p=' || AppId || ':' || PageAlias || ':' || SessionId ||'::NO::' || Arguments || ':' || ArgumentValues, p_checksum_type => Checksum);
Does anyone know how to generate a URL for a modal page please ?
Thanks.
Cheers,

laravel Blade is their a neat solution to : Trying to get property of non-object

I have thins line of code
{{($user->company_privileges->level < 3 ? '' : ' disabled') }}
In my menu file
but when a user doesn't have a company I get the error
Trying to get property of non-object
Is their a neat way of dealing with this in one line?
Something like this comes to mind
{{ ($user->company_privileges === null) : '' ? ($user->company_privileges->level < 3 ? '' : ' disabled') ) }}
as the second if statement is only ran if the user has a company
I have the statement inside a tag aswell
<a class="{{($user->company_privileges->level < 3 ? '' : ' disabled') }}">
Why not something like
<a class="{{($user->company_privileges && $user->company_privileges->level < 3 ? '' : ' disabled') }}">
Essentially, what you need here is to know that null == false for bool comparison. Check out the PHP type comparisons table

Ruby: divisible by 4

This works fine, but I want to make it prettier - and accommodate all values that are divisible by 4:
if i==4 || i==8 || i==12 || i==16 || i==20 || i==24 || i==28 || i==32
# ...
end
Any clever, short method to do this?
Try this:
if i % 4 == 0
This is called the "modulo operator".
There's also modulo, which allows you to do
420.modulo(4).zero?
There's nothing stopping you doing that with %, but it looks weird:
420.%(4).zero?
This is always a good conversation starter:
if (i & 3).zero?

Resources