title: Retrieving the netnsid of a network namespace in Python
body: <p>When trying to find the correct peer network interface of a <code>veth</code> pair that lives in a different namespace, that end is not only indicated by its <code>iflink</code> property, but also by a <code>link-netnsid</code>. This <code>link-netnsid</code> is a network namespace ID which is only meaningful within the current network namespace.</p>
  
  <p>The Linux kernel doesn't offer to map a netnsid to a network namespace inode number, which is the only unique identification. However, Linux offers the <code>RTM_GETNSID</code> request that maps a network namespace identified either by fd (<code>NETNSA_FD</code>) or by PID (<code>NETNSA_PID</code>) to the local netnsid.</p>
  
  <p>How do I make such a <code>RTM_GETNSID</code> request in Python, preferably using the <code>pyroute2</code> library? So far, I could not successfully request the netnsid for the namespace identified by PID, but only get back an invalid argument error 22, using the following script:</p>
  
  <pre><code>from pyroute2 import IPRoute
  from pyroute2.netlink import NLM_F_REQUEST
  import pyroute2.netlink.rtnl as rtnl
  import pyroute2.netlink.rtnl.nsidmsg as nsidmsg
  
  netstack = IPRoute()
  req = nsidmsg.nsidmsg()
  req['rtgen_family'] = 0
  # 12345 is PID of a process inside another network namespace
  req['attrs'] = [('NETNSA_PID', 12345)]
  ret = netstack.nlm_request(req, rtnl.RTM_GETNSID, NLM_F_REQUEST)
  </code></pre>