Skip to content Skip to sidebar Skip to footer

Store Os.system Result In Variable

hello guys i'm wondering how to store os.system result in variable as we know it's return 0 so i'm wondering what i should do to store the result and second question : how to get

Solution 1:

import os
from subprocess import *

defrun_cmd(cmd):
        p = Popen(cmd, shell=True, stdout=PIPE)
        output = p.communicate()[0]
        return output

As for the second question, see http://www.cyberciti.biz/tips/read-unixlinux-system-ip-address-in-a-shell-script.html

Solution 2:

Since you're first question is a python question, here is how to get the IP address in linux using python:

import socket
import fcntl
importstruct

ifname='eth0'
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
address = socket.inet_ntoa(fcntl.ioctl(
                    s.fileno(),
                    0x8915,  # SIOCGIFADDR
                    struct.pack('256s', ifname[:15])
                    )[20:24])

Solution 3:

Hi You can Create Subprocess.pipe and can print output of ifconfig Here is a code for Ref:

import os
import subprocess
from subprocess import *
subprocess.call(["ifconfig","en0”])
p=subprocess.Popen(["ifconfig","en0"],stdout=subprocess.PIPE)
for line in p.stdout:
    print line

Post a Comment for "Store Os.system Result In Variable"