Appending a subprocess to a list seems to just append the subprocess object location in python?
Question: Using the following command at the console prints the local MAC address of
wlan0's NIC. I want to integrate this into a script where the 0th sublist of a
list will be filled with the local MAC in exer

    
    
    ifconfig wlan0 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'
    

The list in use, localisation, gets it's 1st and 2nd sublists from a dict
called scanned.

So I want to have the local MAC in the 0th sublist and an entry for every
entry in the 1st and 2nd sublists. I have tried the code:

    
    
    for s in scanned:
        localisation[0].append(subprocess.Popen("ifconfig wlan0 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'", shell=True))
    

but I just get

    
    
    <subprocess.Popen object at 0x2bf0f50>
    

For every entry into the list. Although there are the correct amount of
entries.

I also have the problem that for some reason the program prints the output of
the code to the screen which I don't want to happen.

What am I doing wrong?


Answer: the popen object is a file object in a sense.

    
    
    from subprocess import *
    handle = Popen(c, stdin=PIPE, stderr=PIPE, stdout=PIPE)
    handle.stdin.write('echo "Heeey there"')
    print handle.stdout.read()
    handle.flush()
    

The reason why not ALL output is redirected for you is stderr=PIPE, it must be
otherwise that will be echoed into the console no matter what. Redirecting it
to PIPE is a good idea.

Also, using `shell=True` is generally a bad idea unless you know why you need
it.. in this case (i don't think) you don't need it.

Aaaand finally, you need to devide your command that you would like to execute
into a list, at least a 2 item list or more. example:

    
    
    c = ['ssh', '-t', 'user@host', "service --status-all"]
    

which would be `ssh -t user@root "service --status-all"` normally, notice the
NOT splitted part of `"service --status-all"` because that's a parameter sent
as a whole to the SSH client in my example.

Without trying, try:

    
    
    c = ["ifconfig", "wlan0 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'"]
    

* * *

Or even:

    
    
    from uuid import getnode as get_mac
    mac = get_mac()
    

