pingAll.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/env python2
  2. # You may redistribute this program and/or modify it under the terms of
  3. # the GNU General Public License as published by the Free Software Foundation,
  4. # either version 3 of the License, or (at your option) any later version.
  5. #
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. #
  11. # You should have received a copy of the GNU General Public License
  12. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. import sys;
  14. from cjdnsadmin.cjdnsadmin import connectWithAdminInfo;
  15. cjdns = connectWithAdminInfo();
  16. allRoutes = [];
  17. magicalLinkConstant = 5366870.0;
  18. def pingNode(addr, path, link):
  19. addrAtPath = addr + '@' + path;
  20. result = cjdns.RouterModule_pingNode(path, 2000);
  21. res = '';
  22. if ('result' in result): res = result['result'];
  23. if (res == 'pong'):
  24. if (addrAtPath != result['from']): addrAtPath += ' mismatch ' + result['from'];
  25. print(addrAtPath +
  26. ' link ' + str(link) +
  27. ' proto ' + str(result['protocol']) +
  28. ' ' + str(result['ms']) + 'ms');
  29. return True;
  30. elif (res == 'timeout'):
  31. print(addrAtPath + ' link ' + str(link) + ' TIMEOUT ' + str(result['ms']) + 'ms');
  32. else:
  33. print(addrAtPath + str(result));
  34. return False;
  35. i = 0;
  36. while True:
  37. table = cjdns.NodeStore_dumpTable(i);
  38. routes = table['routingTable'];
  39. allRoutes += routes;
  40. if (not 'more' in table):
  41. break;
  42. i += 1;
  43. if (len(sys.argv) > 4 and '-s' == sys.argv[4]):
  44. for route in allRoutes:
  45. i = 0;
  46. while i < 3:
  47. result = cjdns.SwitchPinger_ping(route['path'], route['path'], 10000);
  48. if (i > 0): print 'attempt ' + str(i) + ' ';
  49. print result;
  50. if (result['result'] != 'timeout'): break;
  51. i += 1;
  52. else:
  53. i = 0;
  54. responses = 0;
  55. for entry in allRoutes:
  56. path = entry['path'];
  57. addr = entry['ip'];
  58. # if (len(sys.argv) > 4 and '-b' == sys.argv[4]):
  59. skip = False;
  60. for e in allRoutes:
  61. if e['ip'] == addr and e['link'] > entry['link']:
  62. skip = True;
  63. break;
  64. if skip == True: continue;
  65. link = int(entry['link'] / magicalLinkConstant);
  66. if (entry['link'] == 0): continue;
  67. for x in range(0, 3):
  68. if (pingNode(addr, path, link)):
  69. responses += 1;
  70. break;
  71. i += 1;
  72. print(str(i) + ' total ' + str(responses) + ' respond.');