Reputation: 1351
If I run below command on terminal it gives IP address value
ip route get 8.8.8.8 | grep -oP '(?<=src )(\d{1,3}.){4}'
But when I run same command with '2>&1' then it returns empty string:
output = ''
IO.popen("ip route get 8.8.8.8 | grep -oP '(?<=src )(\d{1,3}.){4}' 2>&1", 'r+') do |f|
output = f.read.strip
end
puts output.inspect
Please guide me to understand above scenario. What modifications I need to add to get IP address.
Upvotes: 0
Views: 118
Reputation: 98486
Nothing to do with the redirection. In Ruby, backslashes in strings must be escaped. Just replace \
with \\
:
output = ''
IO.popen("ip route get 8.8.8.8 | grep -oP '(?<=src )(\\d{1,3}.){4}' 2>&1", 'r+') do |f|
output = f.read.strip
end
puts output.inspect
Upvotes: 2