Pergunta

I have to execute a ping command from ruby script. If I directly execute ping 8.8.8.8 it works.

How can i use a variable in place of IP?

I tried

dst_ip="8.8.8.8"
ping_string = "ping "+dst_ip
puts ping_string
ping = `ping_string` 

but it dint work.

Foi útil?

Solução

Do as below :

C:\>irb
irb(main):001:0> ip = '66.249.64.0'
=> "66.249.64.0"
irb(main):002:0> `ping #{ip}`
=> "\nPinging 66.249.64.0 with 32 bytes of data:\nRequest timed out.\nRequest ti
med out.\nRequest timed out.\nRequest timed out.\n\nPing statistics for 66.249.6
4.0:\n    Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),\n"
irb(main):003:0>

You don't need to do string operation like this - ping_string = "ping "+dst_ip.

Outras dicas

Because interpolation works in `` operator, this should work:

ping = `#{ping_string}`
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top