iface.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import netlink.capi as nl
  2. import netlink.genl.capi as genl
  3. import nl80211
  4. import sys
  5. import traceback
  6. class test_class:
  7. def __init__(self):
  8. self.done = 1;
  9. def msg_handler(m, a):
  10. try:
  11. e, attr = genl.py_genlmsg_parse(nl.nlmsg_hdr(m), 0,
  12. nl80211.NL80211_ATTR_MAX, None)
  13. if nl80211.NL80211_ATTR_WIPHY in attr:
  14. thiswiphy = nl.nla_get_u32(attr[nl80211.NL80211_ATTR_WIPHY])
  15. print("phy#%d" % thiswiphy)
  16. if nl80211.NL80211_ATTR_IFNAME in attr:
  17. print("\tinterface %s" % nl.nla_get_string(attr[nl80211.NL80211_ATTR_IFNAME]));
  18. if nl80211.NL80211_ATTR_IFINDEX in attr:
  19. print("\tifindex %d" % nl.nla_get_u32(attr[nl80211.NL80211_ATTR_IFINDEX]))
  20. if nl80211.NL80211_ATTR_WDEV in attr:
  21. print("\twdev 0x%lx" % nl.nla_get_u64(attr[nl80211.NL80211_ATTR_WDEV]))
  22. if nl80211.NL80211_ATTR_MAC in attr:
  23. print("\tmac %02x:%02x:%02x:%02x:%02x:%02x" % tuple(nl.nla_data(attr[nl80211.NL80211_ATTR_MAC])))
  24. if nl80211.NL80211_ATTR_SSID in attr:
  25. print("\tssid ", nl.nla_data(attr[nl80211.NL80211_ATTR_SSID]))
  26. if nl80211.NL80211_ATTR_IFTYPE in attr:
  27. iftype = nl.nla_get_u32(attr[nl80211.NL80211_ATTR_IFTYPE])
  28. print("\ttype %s" % nl80211.nl80211_iftype2str[iftype])
  29. if nl80211.NL80211_ATTR_WIPHY_FREQ in attr:
  30. freq = nl.nla_get_u32(attr[nl80211.NL80211_ATTR_WIPHY_FREQ])
  31. sys.stdout.write("\tfreq %d MHz" % freq);
  32. if nl80211.NL80211_ATTR_CHANNEL_WIDTH in attr:
  33. chanw = nl.nla_get_u32(attr[nl80211.NL80211_ATTR_CHANNEL_WIDTH])
  34. sys.stdout.write(", width: %s" % nl80211.nl80211_chan_width2str[chanw])
  35. if nl80211.NL80211_ATTR_CENTER_FREQ1 in attr:
  36. sys.stdout.write(", center1: %d MHz" %
  37. nl.nla_get_u32(attr[nl80211.NL80211_ATTR_CENTER_FREQ1]))
  38. if nl80211.NL80211_ATTR_CENTER_FREQ2 in attr:
  39. sys.stdout.write(", center2: %d MHz" %
  40. nl.nla_get_u32(attr[nl80211.NL80211_ATTR_CENTER_FREQ2]))
  41. elif nl80211.NL80211_ATTR_WIPHY_CHANNEL_TYPE in attr:
  42. channel_type = nl.nla_get_u32(attr[nl80211.NL80211_ATTR_WIPHY_CHANNEL_TYPE])
  43. sys.stdout.write(" %s" % nl80211.nl80211_channel_type2str(channel_type));
  44. sys.stdout.write("\n");
  45. return nl.NL_SKIP;
  46. except Exception as e:
  47. (t,v,tb) = sys.exc_info()
  48. print v.message
  49. traceback.print_tb(tb)
  50. def error_handler(err, a):
  51. a.done = err.error
  52. return nl.NL_STOP
  53. def finish_handler(m, a):
  54. return nl.NL_SKIP
  55. def ack_handler(m, a):
  56. a.done = 0
  57. return nl.NL_STOP
  58. try:
  59. cbd = test_class()
  60. tx_cb = nl.nl_cb_alloc(nl.NL_CB_DEFAULT)
  61. rx_cb = nl.nl_cb_clone(tx_cb)
  62. s = nl.nl_socket_alloc_cb(tx_cb)
  63. nl.py_nl_cb_err(rx_cb, nl.NL_CB_CUSTOM, error_handler, cbd);
  64. nl.py_nl_cb_set(rx_cb, nl.NL_CB_FINISH, nl.NL_CB_CUSTOM, finish_handler, cbd);
  65. nl.py_nl_cb_set(rx_cb, nl.NL_CB_ACK, nl.NL_CB_CUSTOM, ack_handler, cbd);
  66. nl.py_nl_cb_set(rx_cb, nl.NL_CB_VALID, nl.NL_CB_CUSTOM, msg_handler, cbd);
  67. genl.genl_connect(s)
  68. family = genl.genl_ctrl_resolve(s, 'nl80211')
  69. m = nl.nlmsg_alloc()
  70. genl.genlmsg_put(m, 0, 0, family, 0, 0, nl80211.NL80211_CMD_GET_INTERFACE, 0)
  71. nl.nla_put_u32(m, nl80211.NL80211_ATTR_IFINDEX, nl.if_nametoindex('wlan0'))
  72. err = nl.nl_send_auto_complete(s, m);
  73. if err < 0:
  74. nl.nlmsg_free(msg)
  75. while cbd.done > 0 and not err < 0:
  76. err = nl.nl_recvmsgs(s, rx_cb)
  77. except Exception as e:
  78. (t, v, tb) = sys.exc_info()
  79. print v.message
  80. traceback.print_tb(tb)