pmuller / procfs

Python API for Linux /proc
Other
75 stars 25 forks source link

fetching information about ip routes #1

Open abbbe opened 12 years ago

abbbe commented 12 years ago

Hello,

Do you already have a feature to fetch information about IP routes? Can't seem to find it. I don't mind implementing it myself. Do you happen to have a sample piece of code I can use as a base?

Regards, Alex

pmuller commented 12 years ago

Hi !

It's not currently implemented, but it's really easy to add it.

The code looks for parsers of /proc files in procfs.proc.* and parsers of /proc/ files in procfs.processes.*. If no parser is found, access to that file from procfs (for example proc.net.route) returns the raw file content as a string.

To write a parser, you just have to add a route class in procfs/processes/net/__init__.py which inherits from the ProcessFile class with a _parse method :

class route(ProcessFile):
    def _parse(self, data):
        # data is a string read from the file.
        # parse it and return a python object

Tell me if you need more information. And thank you for your interest. :-)

q1208c commented 9 years ago

Hi, Procfs is very nice tool for me, I use it to get more information for the system. Few days ago, I try to find the routing table, and I try to use procfs.Proc.net.route. But, I found a litter different from "ip route show" and the "procfs.Proc.net.route()". The "ip route show" will show the duplicates, but the procfs use Dict, so, no duplicates returned. for normal proc record, the Dict is better, but for route, sometimes, we need the duplicates records.

How i can do for this ?

sorry for my poor English. :(

perryjrandall commented 9 years ago

You can make the route item optionally a list instead of a string if you dont want to break compatibility with existing code, or you can add another class like routelist which has a list of the routes instead of a dictionary.

q1208c commented 9 years ago

thanks. I try it later.