Created
July 15, 2014 14:29
-
-
Save limhoff-r7/bf16ce48b298f82559ad to your computer and use it in GitHub Desktop.
Postgres INET contains (<<) operator search using MetasploitDataModels::IPAddress::CIDR
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
attribute = Mdm::Host.arel_table[:address] | |
value = MetasploitDataModels::IPAddress::V4::CIDR.new(value: '1.2.3.4/8') | |
formatted_value = "#{value.address}/#{value.prefix_length}" | |
cast_argument = Arel::Nodes::As.new(formatted_value, Arel::Nodes::SqlLiteral.new('INET')) | |
# @see https://gist.github.com/mrpunkin/1996454 | |
value_as_inet = Arel::Nodes::NamedFunction.new('CAST', [cast_argument]) | |
operation = Arel::Nodes::InfixOperation.new( | |
'<<', | |
attribute, | |
value_as_inet | |
) | |
operation.to_sql # "\"hosts\".\"address\" << CAST('1.2.3.4/8' AS INET)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I had to read AREL's source to figure out how to do AS on a literal string, but thanks to @mrpunkin for how to use CAST in AREL.