Hi,

I'm doing python studies to write test automation scripts. As a part of that I want a script which should identify the NAT type of my network..
Please help me out in this regard...

Thx.

Dani AI

Generated

As is looking for the underlying logic (and as asked how you can tell), the usual, reliable approach is to probe from the client to one or more external endpoints (STUN-style) and observe two things: (1) whether the router rewrites your source IP:port differently depending on the destination (mapping consistency), and (2) whether an external host can send packets to that mapped address without the client first sending to that external host (inbound acceptance). Those two measurements let you distinguish full-cone, restricted-cone, port-restricted and symmetric NATs, or detect UDP being blocked.

Keywords and protocol bits to search/implement: STUN binding request, MAPPED-ADDRESS, CHANGED-ADDRESS, CHANGE-REQUEST, endpoint-independent vs endpoint-dependent mapping, hairpinning. A practical test plan:

  • Query a STUN server A and record the public mapping (IP:port).
  • Query a different external endpoint B (or use the server’s CHANGED-ADDRESS) and record the mapping again. If the mapping changes, the NAT is symmetric.
  • If the mapping is the same, ask an external host to send a packet to your mapping without you contacting it first. If that packet arrives, the NAT is cone (full cone). If it only arrives after you first send to that host, it’s restricted; test with differing destination ports to decide port-restricted vs simple restricted.
  • For TCP, hairpinning, CGNAT, or ISPs that rewrite ports, STUN may be insufficient; run two controlled servers under your control for deterministic tests.

A quick Python starting point is to use a STUN helper library (many expose a simple get_ip_info() call). For example:

import stun
nat_type, external_ip, external_port = stun.get_ip_info()
print(nat_type, external_ip, external_port)

Run the sequence above against two endpoints and automate comparisons and change-request checks. For the formal protocol behavior, see the STUN specification and classic NAT classification in RFC 3489 and the updated STUN spec in RFC 5389.

Recommended Answers

All 2 Replies

How would you even do that? I mean outside of python how can you figure out if you're receiving natted traffic and what kind of natting is being done?

How would you even do that? I mean outside of python how can you figure out if you're receiving natted traffic and what kind of natting is being done?

When I googled regarding this, I got a tool, "NAT-Tester" which identifies the NAT of my LAN... I just need the logic / keywords so that I will script the same and embed in my automation script...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.