0

I am trying to write a ruby script to change my wireless mac address quickly by using "macchanger" tool.I have all the root permission to change it. And I try this:

  `ifconfig #{@wifi_device.getName()} down`
  output= `macchanger -m AA:BB:CC:DD:EE:FF #{@wifi_device.getName()}`
  `ifconfig #{@wifi_device.getName()} up`

It usually works well , however when there is a problem , for example I enter an invalid mac address, and the bash prints any error , I can not catch it from Ruby.It prints only success messages , not errors. When I tried an invalid mac , My question if there is any suggestion to get rid of executing bash commands and read the output properly.

2 Answers 2

2

When you use this macchanger, then you don't need to call a shell command. You can put macchanger into your current folder and require_relative './macchanger' . After this you can use the class MacChanger

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for suggestion , I am gonna use it
Although this does not answer your question directly. This is a better programmatic solution to your application. Eliminating the need to do error redirect/checking by hand and offloading that onto the greater community.
1

The shell needs to know where to send stderr. 2>&1 is a great way to do this it redirects 2(stderr) to 1(stdout) which is what "output" is capturing.

 output= `macchanger -m AA:BB:CC:DD:EE:FF #{@wifi_device.getName()} 2>&1`

The Advanced Bash Scripting Guide has a great chapter on I/O Redirection.

1 Comment

I am glad I could help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.