Return a list of IPv4 routes than can be used by Scapy. This function parses netstat.
()
| 89 | |
| 90 | |
| 91 | def read_routes(): |
| 92 | # type: () -> List[Tuple[int, int, str, str, str, int]] |
| 93 | """Return a list of IPv4 routes than can be used by Scapy. |
| 94 | |
| 95 | This function parses netstat. |
| 96 | """ |
| 97 | if SOLARIS: |
| 98 | f = os.popen("netstat -rvn -f inet") |
| 99 | elif FREEBSD: |
| 100 | f = os.popen("netstat -rnW -f inet") # -W to show long interface names |
| 101 | else: |
| 102 | f = os.popen("netstat -rn -f inet") |
| 103 | ok = 0 |
| 104 | mtu_present = False |
| 105 | prio_present = False |
| 106 | refs_present = False |
| 107 | use_present = False |
| 108 | routes = [] # type: List[Tuple[int, int, str, str, str, int]] |
| 109 | pending_if = [] # type: List[Tuple[int, int, str]] |
| 110 | for line in f.readlines(): |
| 111 | if not line: |
| 112 | break |
| 113 | line = line.strip().lower() |
| 114 | if line.find("----") >= 0: # a separation line |
| 115 | continue |
| 116 | if not ok: |
| 117 | if line.find("destination") >= 0: |
| 118 | ok = 1 |
| 119 | mtu_present = "mtu" in line |
| 120 | prio_present = "prio" in line |
| 121 | refs_present = "ref" in line # There is no s on Solaris |
| 122 | use_present = "use" in line or "nhop" in line |
| 123 | continue |
| 124 | if not line: |
| 125 | break |
| 126 | rt = line.split() |
| 127 | if SOLARIS: |
| 128 | dest_, netmask_, gw, netif = rt[:4] |
| 129 | flg = rt[4 + mtu_present + refs_present] |
| 130 | else: |
| 131 | dest_, gw, flg = rt[:3] |
| 132 | locked = OPENBSD and rt[6] == "l" |
| 133 | offset = mtu_present + prio_present + refs_present + locked |
| 134 | offset += use_present |
| 135 | netif = rt[3 + offset] |
| 136 | if flg.find("lc") >= 0: |
| 137 | continue |
| 138 | elif dest_ == "default": |
| 139 | dest = 0 |
| 140 | netmask = 0 |
| 141 | elif SOLARIS: |
| 142 | dest = scapy.utils.atol(dest_) |
| 143 | netmask = scapy.utils.atol(netmask_) |
| 144 | else: |
| 145 | if "/" in dest_: |
| 146 | dest_, netmask_ = dest_.split("/") |
| 147 | netmask = scapy.utils.itom(int(netmask_)) |
| 148 | else: |
no test coverage detected