Jump to content

Python Question: Correct way to get stdout as variable and use in a system call?


vailixi

Recommended Posts

I'm trying to get the output of a command as a variable and use it for a system call. Mostly I'm trying to get the concept of this by doing something easy like changing a MAC address.
Firstly I want to get the wireless interface. I know I can make <iface> a command line option and I'm sure that's a better way to go but it's not really the point.

There are several ways to get the wireless interface. Some of the stuff I tried.

cat /proc/net/wireless | tail -n 1 | cut -d ' ' -f 2 | tr -d ':' | sed '/^\s*$/d' #this method is completely unreliable

ifconfig | tail | head -2 |  sed '/^\s*$/d' | cut -d ":" -f 1 #this seems to work

ip link show | tail -n 2 | head -n 1 | cut -d ':' -f 2 | cut -d ' ' -f 2 #maybe

Then I tried iwfconfig I think it is several outputs instead of one. So head, tail, and grep don't seem to work on it. I tried readarray as well. I didn't want to write to a file then cat the file into read array or read the file from the python script because usually there is a more elegant way of doing things. I just don't know how to do it.

Question is there a more elegant way of doing this stuff?

import subprocess
from subprocess import call

batcmd="cat /proc/net/wireless | tail -n 1 | cut -d ' ' -f 2 | tr -d ':'"
wlan = subprocess.check_output(batcmd.strip(), shell=True)
print(wlan).strip()

call(["ifconfig", wlan.strip(), "down"])
call(["macchanger", "-r", wlan.strip()])
call(["ifconfig",  wlan.strip(), "up"])

I tried this and it works sometimes. But sometimes fails because cat fails to return the wireless interface.

import subprocess
from subprocess import call

batcmd="ifconfig | tail | head -2 |  sed '/^\s*$/d' | cut -d ':' -f 1"
wlan = subprocess.check_output(batcmd.strip(), shell=True)
print(wlan).strip()

call(["ifconfig", wlan.strip(), "down"])
call(["macchanger", "-r", wlan.strip()])
call(["ifconfig",  wlan.strip(), "up"])

I wasn't sure if subprocess is the best of way of doing this. What I really need is ways to get stdout and use it in a system call.

Edited by vailixi
Link to comment
Share on other sites

I would use 'iw dev' (i think)all of the linux wireless networking software is a wrapper of `iw`
 

iw dev |grep -i grep interface.

 

System('iw dev'){ |line| 

   if line include?('interface')

      puts line.split[1] #=> wlan

   End

End

 

Just a ruby snip. what if you have multiple wireless interfaces. I would assume you don't want the one associated with a access point. So further system variable checks would need to be made...

 

I'm unsure of your plan. Automatic wifi attacks? 

Link to comment
Share on other sites

iw dev |grep -i interface | cut -d ' ' -f 2

Perfect. Thanks.

Actually there are plenty of automated wireless attack scripts available already. I'm trying to learn how to code deeper system automation. I need to be able to deal with outputs. I started working with python because python is easy compared to other languages. I'll probably port some of my BASH scripts over to python once I get a better handle on that. It seems to be what the cool kids are doing.

Edited by vailixi
Link to comment
Share on other sites

if your learning python. don't use bash or pipes to grep your information. Get the information you need with python.

 

System('Iw dev'){ |input,output,thread|
   write.input('type yes to save')

   print(output)

   Print thread.pid.info

      If thread.poop then

          thread.kill.pid

      End

}
 

 

This code is generic. check if python has a lib popen3. Write to stdin, read stdout, get pid and thread information. Use python to get the information you need.

Edited by i8igmac
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...