简体   繁体   中英

How to replace a particular string found in URL taken from console with file content found on each line written in text file in Ruby?

I want to replace fuzz word which has been written in URL taken from console with contents of text file having one string per line. After replacing this fuzz word with file content want to fire http request with this replaced content file and store responses with modified requests in new file.

I have written like this but getting error:

fuzz1.rb:16: private method `gsub' called for #<URI::HTTP:0x2969040> (NoMethodError)

Code is here:

require 'net/http'

puts "Enter Target:\n"
target = URI(gets())
new_reference = target
a1 = target.clone
Net::HTTP.start(target.host, target.port) do |http|
request = Net::HTTP::Get.new target.request_uri
response = http.request request 
puts response.body
end
puts "File contents:\n"
f= File.open("fuzz.txt","r")
while line = f.gets do
    puts "Attack value: #{line}"
    b = a1.gsub('fuzz','#{line}')
    c = b
    Net::HTTP.start(c.host, c.port) do |http|
    request = Net::HTTP::Get.new c.request_uri
    response = http.request request 
    puts response.body
    end
end

No idea why this gsub error is coming and not replacing fuzz word found in URL with file line content.

a1 is an URI object, it doesn't have a gsub method. You need to cast it to a string. Try this

    b = URI.parse(a1.to_s.gsub('fuzz','#{line}'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM