index.php 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104
  1. <?php
  2. /**
  3. * HSDN Looking Glass version 1.2.3b
  4. *
  5. * General Features:
  6. * - Supports the Telnet and SSH (through Putty/plink)
  7. * - Supports the Cisco, MikroTik v5 and v6, Quagga (Zebra) and JunOS routers
  8. * - Supports the IPv4 and IPv6 protocols
  9. * - Automatic conversion IPs to subnets using RADb (for MikroTik)
  10. * - Drawing graph of BGP AS pathes using GraphViz toolkit
  11. * - Works on php 5.2.0 and above
  12. *
  13. * System Requirements:
  14. * - php version 5.2.0 and above with Sockets and Filter
  15. * http://www.php.net/
  16. * - Putty for SSH connections usign `plink' command
  17. * http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
  18. * - GraphViz toolkit for drawing BGP pathes graph
  19. * http://www.graphviz.org/
  20. * - php pear package Image_GraphViz
  21. * http://pear.php.net/package/Image_GraphViz
  22. *
  23. *
  24. * Copyright (C) 2012-2017 Information Networks Ltd. <info@hsdn.org>
  25. * http://www.hsdn.org/
  26. *
  27. * Copyright (C) 2000-2002 Cougar <cougar@random.ee>
  28. * http://www.version6.net/
  29. *
  30. * Copyright (C) 2014 Regional Networks Ltd. <info@regnets.ru>
  31. * http://www.regnets.ru/
  32. *
  33. * This program is free software: you can redistribute it and/or modify
  34. * it under the terms of the GNU General Public License as published by
  35. * the Free Software Foundation, either version 3 of the License, or
  36. * (at your option) any later version.
  37. *
  38. * This program is distributed in the hope that it will be useful,
  39. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  40. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  41. * GNU General Public License for more details.
  42. *
  43. * You should have received a copy of the GNU General Public License
  44. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  45. */
  46. // ------------------------------------------------------------------------
  47. // BEGIN CONFIGURATIONS
  48. // ------------------------------------------------------------------------
  49. /*
  50. * Your own AS number
  51. */
  52. $_CONFIG['asn'] = '56727';
  53. /*
  54. * Your own company name
  55. */
  56. $_CONFIG['company'] = 'Information Networks Ltd.';
  57. /*
  58. * Your logo URL (or FALSE)
  59. */
  60. $_CONFIG['logo'] = 'lg_logo.gif';
  61. /*
  62. * Main style color
  63. */
  64. $_CONFIG['color'] = '#3366cc';
  65. /*
  66. * Putty `plink' command path (for SSH connections)
  67. */
  68. $_CONFIG['plink'] = '/usr/local/bin/plink';
  69. /*
  70. * URL address of the IP whois service
  71. */
  72. $_CONFIG['ipwhois'] = 'http://noc.hsdn.org/whois/';
  73. /*
  74. * URL address of the AS whois service
  75. */
  76. $_CONFIG['aswhois'] = 'http://noc.hsdn.org/aswhois/';
  77. /**
  78. * Router nodes
  79. *
  80. * Parameters:
  81. * url - URL address in format: [ssh|telnet]://[login]:[password]@[host]:[port]
  82. * pingtraceurl - URL address for ping and traceroute tools (or FALSE)
  83. * description - Node description
  84. * group - Node group name (of FALSE)
  85. * ipv6 - Node is supports IPv6 (TRUE/FALSE)
  86. * os - Node OS (ios, mikrotik, quagga, junos)
  87. */
  88. $_CONFIG['routers'] = array
  89. (
  90. /*'saratov-br1' => array
  91. (
  92. 'url' => 'telnet://rviews@91.226.136.130:23',
  93. 'pingtraceurl' => 'telnet://rviews:Rviews@91.226.136.130:2300',
  94. 'description' => 'Saratov (RU), BR1',
  95. 'group' => 'AS56727',
  96. 'ipv6' => TRUE,
  97. 'os' => 'quagga',
  98. ),*/
  99. 'saratov-br1' => array
  100. (
  101. 'url' => 'telnet://91.226.136.129:23',
  102. 'pingtraceurl' => FALSE,
  103. 'description' => 'Saratov (RU), BR1',
  104. 'group' => 'AS56727',
  105. 'ipv6' => TRUE,
  106. 'os' => 'ios',
  107. ),
  108. 'rs' => array
  109. (
  110. 'url' => 'telnet://91.226.136.180:23',
  111. 'pingtraceurl' => FALSE,
  112. 'description' => 'Route Server',
  113. 'group' => 'AS56727',
  114. 'ipv6' => TRUE,
  115. 'os' => 'ios',
  116. ),
  117. 'kemerovo-br1' => array
  118. (
  119. 'url' => 'telnet://46.8.40.1:23',
  120. 'pingtraceurl' => FALSE,
  121. 'description' => 'Kemerovo (RU), BR1',
  122. 'group' => 'AS60894',
  123. 'ipv6' => TRUE,
  124. 'os' => 'ios',
  125. ),
  126. /*'test' => array
  127. (
  128. 'url' => 'telnet://rviews:rviews@route-server.ip.att.net',
  129. 'pingtraceurl' => FALSE,
  130. 'description' => 'AT&T test',
  131. 'group' => '',
  132. 'ipv6' => FALSE,
  133. 'os' => 'junos',
  134. ),*/
  135. );
  136. // ------------------------------------------------------------------------
  137. // END OF CONFIGURATIONS
  138. // ------------------------------------------------------------------------
  139. // ------------------------------------------------------------------------
  140. // Do not edit below this line, unless you fully understand the implications.
  141. // ------------------------------------------------------------------------
  142. //error_reporting(0);
  143. @ob_end_flush();
  144. $router = isset($_REQUEST['router']) ? trim($_REQUEST['router']) : FALSE;
  145. $protocol = isset($_REQUEST['protocol']) ? trim($_REQUEST['protocol']) : FALSE;
  146. $command = isset($_REQUEST['command']) ? trim($_REQUEST['command']) : FALSE;
  147. $query = isset($_REQUEST['query']) ? trim($_REQUEST['query']) : FALSE;
  148. if ($command != 'graph' OR !isset($_REQUEST['render']) OR !isset($_CONFIG['routers'][$router]))
  149. {
  150. // HTML header
  151. ?>
  152. <!DOCTYPE html>
  153. <html lang="en">
  154. <head>
  155. <title>AS<?php print $_CONFIG['asn'] ?> Looking Glass</title>
  156. <meta charset="utf-8">
  157. <link rel="shortcut icon" href="favicon.ico">
  158. <style type="text/css">
  159. <!--
  160. body { font: 14px normal Arial, Helvetica, sans-serif; margin: 30px 10%; color: #000; background: #fff; }
  161. h2 { font-size: 24px; font-weight: normal; }
  162. form { margin: 0; padding: 0 0 15px 0; }
  163. p, object { margin: 0; padding: 0 0 15px 0; }
  164. hr { margin: 0 0 15px 0; border: none; color: #000; background-color: #000; height: 1px; }
  165. a:link, a:visited { color: <?php print $_CONFIG['color'] ?>; }
  166. a:hover { color: #ccc; }
  167. table { border: 0; }
  168. table th { background: <?php print $_CONFIG['color'] ?>; color: #fff; white-space: nowrap; font-size: 14px; text-align: center; }
  169. .form { margin: auto; text-align: left; background: #efefef; border: 5px solid #efefef; }
  170. .center { text-align: center; }
  171. .error { color: red; font-weight: bold; }
  172. .warning { color: blue; font-weight: bold; }
  173. .legend { font-size: 12px; margin: auto; }
  174. //-->
  175. </style>
  176. <script type="text/javascript">
  177. <!--
  178. function load()
  179. {
  180. var loading = document.getElementById('loading');
  181. if (loading !== null)
  182. {
  183. loading.style.display = 'none';
  184. }
  185. }
  186. //-->
  187. </script>
  188. </head>
  189. <body onload="load();">
  190. <?php if (isset($_CONFIG['logo']) AND $_CONFIG['logo']): ?>
  191. <div class="center"><a href="?"><img src="<?php print $_CONFIG['logo'] ?>" border="0" alt="lg"></a></div>
  192. <?php endif ?>
  193. <div class="center"><h2>AS<?php print $_CONFIG['asn'] ?> Looking Glass</h2></div>
  194. <hr>
  195. <?php
  196. flush();
  197. }
  198. $queries = array
  199. (
  200. 'ios' => array
  201. (
  202. 'ipv4' => array
  203. (
  204. 'bgp' => 'show ip bgp %s',
  205. 'advertised-routes' => 'show ip bgp neighbors %s advertised-routes',
  206. 'received-routes' => 'show ip bgp neighbors %s received-routes',
  207. 'routes' => 'show ip bgp neighbors %s routes',
  208. 'summary' => 'show ip bgp summary',
  209. 'ping' => 'ping ip %s',
  210. 'trace' => 'traceroute ip %s'
  211. ),
  212. 'ipv6' => array
  213. (
  214. 'bgp' => 'show bgp ipv6 unicast %s',
  215. 'advertised-routes' => 'show bgp ipv6 neighbors %s advertised-routes',
  216. 'received-routes' => 'show bgp ipv6 neighbors %s received-routes',
  217. 'routes' => 'show bgp ipv6 neighbors %s routes',
  218. 'summary' => 'show bgp ipv6 unicast summary',
  219. 'ping' => 'ping ipv6 %s',
  220. 'trace' => 'traceroute ipv6 %s'
  221. )
  222. ),
  223. 'quagga' => array
  224. (
  225. 'ipv4' => array
  226. (
  227. 'bgp' => 'show ip bgp %s',
  228. 'advertised-routes' => 'show ip bgp neighbors %s advertised-routes',
  229. 'received-routes' => 'show ip bgp neighbors %s received-routes',
  230. 'routes' => 'show ip bgp neighbors %s routes',
  231. 'summary' => 'show ip bgp summary',
  232. 'ping' => 'ping -t 5 -c 5 %s',
  233. 'trace' => 'traceroute -Iaw 1 %s',
  234. ),
  235. 'ipv6' => array
  236. (
  237. 'bgp' => 'show ipv6 bgp %s',
  238. 'advertised-routes' => 'show ipv6 bgp neighbors %s advertised-routes',
  239. 'received-routes' => 'show ipv6 bgp neighbors %s received-routes',
  240. 'routes' => 'show ipv6 bgp neighbors %s routes',
  241. 'summary' => 'show ipv6 bgp summary',
  242. 'ping' => 'ping6 -c 5 %s',
  243. 'trace' => 'traceroute6 -Ialw 1 %s',
  244. )
  245. ),
  246. 'mikrotik' => array
  247. (
  248. 'ipv4' => array
  249. (
  250. 'bgp' => '/ip route print detail where dst-address=%s',
  251. 'advertised-routes' => '/routing bgp advertisements print peer=%s',
  252. 'routes' => '/ip route print where gateway=%s',
  253. 'summary' => '/routing bgp peer print status where address-families=ip',
  254. 'ping' => '/ping count=5 size=56 %s',
  255. 'trace' => '/tool traceroute %s size=60 count=1',
  256. ),
  257. 'ipv6' => array
  258. (
  259. 'bgp' => '/ipv6 route print detail where dst-address=%s',
  260. 'advertised-routes' => '/routing bgp advertisements print peer=%s',
  261. 'routes' => '/ipv6 route print where gateway=%s',
  262. 'summary' => '/routing bgp peer print status where address-families=ipv6',
  263. 'ping' => '/ping count=5 size=56 %s',
  264. 'trace' => '/tool traceroute %s size=60 count=1',
  265. )
  266. ),
  267. 'junos' => array
  268. (
  269. 'ipv4' => array
  270. (
  271. 'bgp' => 'show bgp %s',
  272. 'advertised-routes' => 'show route advertising-protocol bgp %s',
  273. 'routes' => 'show route protocol bgp %s terse',
  274. 'summary' => 'show bgp summary',
  275. 'ping' => 'ping count 5 %s',
  276. 'trace' => 'traceroute %s as-number-lookup',
  277. ),
  278. 'ipv6' => array
  279. (
  280. 'bgp' => 'show bgp %s',
  281. 'advertised-routes' => 'show route advertising-protocol bgp %s',
  282. 'routes' => 'show route protocol bgp %s terse',
  283. 'summary' => 'show bgp summary',
  284. 'ping' => 'ping count 5 %s',
  285. 'trace' => 'traceroute %s',
  286. )
  287. ),
  288. );
  289. if (isset($_CONFIG['routers'][$router]) AND
  290. isset($queries[$_CONFIG['routers'][$router]['os']][$protocol]) AND
  291. (isset($queries[$_CONFIG['routers'][$router]['os']][$protocol][$command]) OR $command == 'graph'))
  292. {
  293. if ($protocol == 'ipv6' AND (!isset($_CONFIG['routers'][$router]['ipv6']) OR
  294. $_CONFIG['routers'][$router]['ipv6'] !== TRUE))
  295. {
  296. $protocol = 'ipv4';
  297. print '<div class="center"><p class="warning">The router does not support IPv6. Using IPv4.</p></div>';
  298. print '<hr>';
  299. }
  300. $url = $_CONFIG['routers'][$router]['url'];
  301. if (($command == 'ping' OR $command == 'trace') AND
  302. isset($_CONFIG['routers'][$router]['pingtraceurl']) AND
  303. $_CONFIG['routers'][$router]['pingtraceurl'] != FALSE)
  304. {
  305. $url = $_CONFIG['routers'][$router]['pingtraceurl'];
  306. }
  307. $url = @parse_url($url);
  308. $os = $_CONFIG['routers'][$router]['os'];
  309. if ($command == 'graph' AND isset($queries[$os][$protocol]['bgp']))
  310. {
  311. $exec = $queries[$os][$protocol]['bgp'];
  312. }
  313. else
  314. {
  315. $exec = $queries[$os][$protocol][$command];
  316. }
  317. if (strpos($exec, '%s') !== FALSE)
  318. {
  319. if (preg_match('/^.[.a-z0-9_-]+\.[a-z]+$/i', $query))
  320. {
  321. if ($command != 'advertised-routes')
  322. {
  323. $query = get_host($query);
  324. }
  325. }
  326. if ($query AND ($command == 'bgp' OR $command == 'graph') AND ($os == 'mikrotik' OR ($protocol == 'ipv6' AND $os == 'ios')))
  327. {
  328. if (strpos($query, '/') === FALSE AND $radb = get_radb($query))
  329. {
  330. $route = FALSE;
  331. if (strpos($query, ':') AND isset($radb['route6']))
  332. {
  333. $route = $radb['route6'];
  334. }
  335. else if (isset($radb['route']))
  336. {
  337. $route = $radb['route'];
  338. }
  339. if ($route)
  340. {
  341. if ($command != 'graph')
  342. {
  343. print '<p>Address <b>'.$query.'</b> is converted to a subnet <b>'.$route.'</b> using the <a href="http://radb.net/" target="_blank">Merit RADb</a></p>';
  344. }
  345. $query = $route;
  346. }
  347. }
  348. }
  349. $exec = sprintf($exec, escapeshellcmd($query));
  350. if (!$query)
  351. {
  352. $exec = FALSE;
  353. if ($query === FALSE)
  354. {
  355. print '<div class="center"><p class="error">Can\'t resolve the hostname.</p></div>';
  356. }
  357. else
  358. {
  359. print '<div class="center"><p class="error">Parameter missing.</p></div>';
  360. }
  361. }
  362. }
  363. else if ($query != '' AND $command != 'graph')
  364. {
  365. print '<div class="center"><p class="warning">No parameter needed.</p></div>';
  366. print '<hr>';
  367. }
  368. if ($exec)
  369. {
  370. if ($os == 'junos')
  371. {
  372. // @see JunOS Routing Table Names (http://www.net-gyver.com/?p=602)
  373. $table = ($protocol == 'ipv6') ? 'inet6.0' : 'inet.0';
  374. if (preg_match("/^show bgp n\w*\s+([\d\.A-Fa-f:]+)$/", $exec, $exec_exp))
  375. {
  376. $exec = 'show bgp neighbor '.$exec_exp[1];
  377. }
  378. else if (preg_match("/^show bgp n\w*\s+([\d\.A-Fa-f:]+) ro\w*$/", $exec, $exec_exp))
  379. {
  380. $exec = 'show route receive-protocol bgp '.$exec_exp[1];
  381. }
  382. else if (preg_match("/^show bgp neighbors ([\d\.A-Fa-f:]+) routes all$/", $exec, $exec_exp))
  383. {
  384. $exec = 'show route receive-protocol bgp '.$exec_exp[1].' all';
  385. }
  386. else if (preg_match("/^show bgp neighbors ([\d\.A-Fa-f:]+) routes damping suppressed$/", $exec, $exec_exp))
  387. {
  388. $exec = 'show route receive-protocol bgp '.$exec_exp[1].' damping suppressed';
  389. }
  390. else if (preg_match("/^show bgp n\w*\s+([\d\.A-Fa-f:]+) advertised-routes ([\d\.A-Fa-f:\/]+)$/", $exec, $exec_exp))
  391. {
  392. $exec = 'show route advertising-protocol bgp '.$exec_exp[1].' '.$exec_exp[2].' exact detail';
  393. }
  394. else if (preg_match("/^show bgp n\w*\s+([\d\.A-Fa-f:]+) receive-protocol ([\d\.A-Fa-f:\/]+)$/", $exec, $exec_exp))
  395. {
  396. $exec = 'show route receive-protocol bgp '.$exec_exp[1].' '.$exec_exp[2].' exact detail';
  397. }
  398. else if (preg_match("/^show bgp n\w*\s+([\d\.A-Fa-f:]+) a[\w\-]*$/", $exec, $exec_exp))
  399. {
  400. $exec = 'show route advertising-protocol bgp '.$exec_exp[1];
  401. }
  402. else if (preg_match("/^show bgp\s+([\d\.A-Fa-f:]+\/\d+)$/", $exec, $exec_exp))
  403. {
  404. $exec = 'show route protocol bgp '.$exec_exp[1].' terse exact';
  405. }
  406. else if (preg_match("/^show bgp\s+([\d\.A-Fa-f:]+)$/", $exec, $exec_exp))
  407. {
  408. $exec = 'show route protocol bgp '.$exec_exp[1].' table '.$table; // terse
  409. }
  410. else if (preg_match("/^show bgp\s+([\d\.A-Fa-f:\/]+) exact$/", $exec, $exec_exp))
  411. {
  412. $exec = 'show route protocol bgp '.$exec_exp[1].' exact detail all';
  413. }
  414. else if (preg_match("/^show bgp re\s+(.*)$/", $exec, $exec_exp))
  415. {
  416. $re = $exec_exp[1];
  417. if (!preg_match('/^\^/', $re))
  418. {
  419. $re = "^.*".$re;
  420. }
  421. if (!preg_match('/\$$/', $re))
  422. {
  423. $re = $re.".*\$";
  424. }
  425. $exec = 'show route aspath-regex "'.str_replace('_', ' ', $re).'" all';
  426. }
  427. }
  428. if ($command == 'graph')
  429. {
  430. $ER = error_reporting(0);
  431. @include 'Image/GraphViz.php';
  432. if (!class_exists('Image_GraphViz'))
  433. {
  434. print '<div class="center"><p class="error">Class Image_GraphViz not found!</p></div>';
  435. }
  436. else
  437. {
  438. if (isset($_REQUEST['render']))
  439. {
  440. $format = ($_REQUEST['render'] == 'png') ? 'png' : 'svg';
  441. if ($output = process($url, $exec, TRUE) AND $as_bgp_path = parse_bgp_path($output))
  442. {
  443. $as_best_path = $as_bgp_path['best'];
  444. $as_pathes = $as_bgp_path['pathes'];
  445. if (sizeof($as_pathes) < 1)
  446. {
  447. get_blank_graph('Not found BGP information of request.', $format);
  448. }
  449. get_path_graph($router, $query, $as_pathes, $as_best_path, $format);
  450. }
  451. get_blank_graph('Unable to get BGP information.', $format);
  452. }
  453. ?>
  454. <div class="center">
  455. <p>BGP routing graph for <b><?php print $query ?></b>, router: <b><?php print $_CONFIG['routers'][$router]['description'] ?></b></p>
  456. <p><a href="?command=bgp&amp;protocol=<?php print $protocol ?>&amp;query=<?php print $query ?>&amp;router=<?php print $router ?>">Run a bgp command on this router</a></p>
  457. <table border="0" class="legend">
  458. <tr><td bgcolor="#CCCCFF" width="15">&nbsp;</td><td>Upstream AS</td><td width="80">&nbsp;</td><td bgcolor="#CCFFCC" width="15">&nbsp;</td><td>Peering AS</td><td width="80">&nbsp;</td><td bgcolor="white"><div style="height:12px;width:37px;background-image:url('data:image/gif;base64,R0lGODlhJQAMAOcAAAQCBISChJQ2NMTCxERCRcSCTGRiZYRWNEw2HKSipOTi5CQiJWRCJORCROyiXPzClISGnFRSVXRydKyi5DQyNRQSFJSSqdwCBPTy99xqlNTS1PwiJNSGvMSm7FxGRGxijIR+fPzk5JySzPSKVLS0tCQlNFxSdCQXDERCXOTC3LS21Dw6PSwCBNQeHAwKDPxSLHxyo+x2hNTS9MTG5KRyROzs7JSGwRwaHeyitPzU1KSivGxsbGRagVxaXXx8fLSq8ZycnPw0NOSaXLyCTExKTrxqbHwiJPylpDQzRPz+/GxuhCwqN4yOjJRkPEQ+WeTk/CwsLPRCTKSa3PwUFBQOCtza/Py0tNSPVGxKLIyKn1RVZBQWHPwDBPz09PxkZPybnIR3rFxcbExKZPzExDw6VPx8fNzO/KyqxJyatPQeLBQCBMzMzERGVGRldFw+JDQiFHRyhNzb3PQqPPTu/PyEhLy8vLyy/AwNFPxVVMzK6pSOxBweLOza7Lyq9HxunDQuQfxMTLwmJPzavPyUlMyb3JxaXLx+TFRKbCQeLOxufKysrPSuvORKZMSy/OTO7Hx+lPwKDIyCt/x0dPzc3Pw8PPysrPy8vPxsbPzNzPyMjPxaXIyKjMyOzHRqlcyKVCweFLy+3JSWlNyWW4R+tJxqPMTC3LSyzFw6PPwaHPwsLPzs7AwGBFQ5JGxGLPSiYLSi5hwSDKSV1ExCXLy61tzW/Dw1TJSSlOzi9PxERKya3BwWJMQqLISClNTW5CwmNKx2ROzs/PSmtGxsfHx7jJyarOyeXJxmPHRNLFxafExKbJyOzIx+fPR2hIx+tAQGBERGRWRmZOTm5CQmJWRGLPzGnFRWVDQ2NZSWrPT298Sq9GxmjlxWeTw+PQwODNTW9pSKxBweHaSmvWRehFxeXExOTCwuNNze/IyOpExOXDw+TKyuypyetXR2iMzO7Lyu+FROcPwODHRunPweHfSmZLSm7ExGZOzm/Kye44SGhMTGxIRaNKSmpHR2dBQWFNTW1PwmJCwAAAAAJQAMAAAIagCTCBxIsKBBVQYTKlxo8AsXL5YYSpzokAuXKZIwTdxYsKJFi/LK5OC40ePHj6kyhRh4pKXLlzBjXjpJ82OQRElq6tw5ZWfNKCQb+rQYZFDQhCZPpqIz6ajCpCFHOl1YccqlMVMnHoG4MSAAOw==')"></div></td><td>Best route</td></tr>
  459. </table>
  460. <br>
  461. <div id="loading" style="display:inline"><p><b>Please wait...</b></p></div>
  462. <!--[if IE]>
  463. <p><img src="?command=graph&amp;protocol=<?php print $protocol ?>&amp;query=<?php print $query ?>&amp;router=<?php print $router ?>&amp;render=png" type="image/svg+xml" alt="" title=""></p>
  464. <![endif]-->
  465. <![if ! IE]>
  466. <object data="?command=graph&amp;protocol=<?php print $protocol ?>&amp;query=<?php print $query ?>&amp;router=<?php print $router ?>&amp;render=true" type="image/svg+xml"></object>
  467. <![endif]>
  468. <br>
  469. </div>
  470. <?php
  471. }
  472. error_reporting($ER);
  473. }
  474. else
  475. {
  476. print '<p><b>Router:</b> '.$_CONFIG['routers'][$router]['description'].'<br><b>Command:</b> '.$exec.'</p><pre><code>';
  477. flush();
  478. process($url, $exec);
  479. print '</code></pre>';
  480. }
  481. }
  482. }
  483. else
  484. {
  485. $routers = group_routers($_CONFIG['routers']);
  486. // HTML form
  487. ?>
  488. <form method="get" action="">
  489. <div class="center">
  490. <table class="form" cellpadding="2" cellspacing="2">
  491. <tr><th>Type of Query</th><th>Additional parameters</th><th>Node</th></tr>
  492. <tr><td>
  493. <table border="0" cellpadding="2" cellspacing="2">
  494. <tr><td><input type="radio" name="command" id="bgp" value="bgp" checked="checked"></td><td><label for="bgp">bgp</label></td></tr>
  495. <tr><td><input type="radio" name="command" id="advertised-routes" value="advertised-routes"></td><td><label for="advertised-routes">bgp&nbsp;advertised-routes</label></td></tr>
  496. <tr><td><input type="radio" name="command" id="summary" value="summary"></td><td><label for="summary">bgp&nbsp;summary</label></td></tr>
  497. <tr><td><input type="radio" name="command" id="graph" value="graph"></td><td><label for="graph">bgp graph</label></td></tr>
  498. <tr><td><input type="radio" name="command" id="trace" value="trace"></td><td><label for="trace">traceroute</label></td></tr>
  499. <tr><td><input type="radio" name="command" id="ping" value="ping"></td><td><label for="ping">ping</label></td></tr>
  500. <tr><td></td><td style="padding-top:10px">
  501. <select name="protocol">
  502. <option value="ipv4">IPv4</option>
  503. <option value="ipv6">IPv6</option>
  504. </select></td></tr>
  505. </table></td>
  506. <td align="center"><input name="query" size="30"></td>
  507. <td align="right">
  508. <select name="router" style="min-width: 180px">
  509. <?php foreach ($routers as $group => $group_data): ?>
  510. <?php if ($group != ''): ?>
  511. <optgroup label="<?php print htmlspecialchars($group) ?>">
  512. <?php endif ?>
  513. <?php foreach ($group_data as $router_id => $router_data): ?>
  514. <option value="<?php print $router_id ?>"><?php print htmlspecialchars($router_data['description']) ?></option>
  515. <?php endforeach ?>
  516. <?php if ($group != ''): ?>
  517. </optgroup>
  518. <?php endif ?>
  519. <?php endforeach ?>
  520. </select></td></tr>
  521. <tr><td align="center" colspan="3"><p><input type="submit" value="Submit"> | <input type="reset" value="Reset"></p></td></tr>
  522. </table>
  523. </div>
  524. </form>
  525. <?php
  526. }
  527. // HTML footer
  528. ?>
  529. <hr>
  530. <div class="center">
  531. <p><small>Information: <a href="https://stat.ripe.net/AS<?php print $_CONFIG['asn'] ?>" target="_blank">RIPEstat</a> <a href="http://bgp.he.net/AS<?php print $_CONFIG['asn'] ?>" target="_blank">he.net</a> <a href="http://as.robtex.com/as<?php print $_CONFIG['asn'] ?>.html#asinfo" target="_blank">robtex.com</a> <a href="http://as.robtex.com/as<?php print $_CONFIG['asn'] ?>.png" target="_blank">Peering Map</a> <a href="http://www.peeringdb.com/view.php?asn=<?php print $_CONFIG['asn'] ?>" target="_blank">PeeringDB</a></small></p>
  532. <p>Copyright &copy; <?php print date('Y') ?> <?php print htmlspecialchars($_CONFIG['company']) ?></p>
  533. </div>
  534. </body>
  535. </html>
  536. <?php
  537. // ------------------------------------------------------------------------
  538. /**
  539. * Execute command and print output
  540. */
  541. function process($url, $exec, $return_buffer = FALSE)
  542. {
  543. global $_CONFIG, $router, $protocol, $os, $command, $query, $ros;
  544. $lines = $line = $is_exception = FALSE;
  545. $index = 0;
  546. $str_in = array();
  547. switch ($url['scheme'])
  548. {
  549. case 'ssh':
  550. $params = array('-ssh');
  551. if (isset($url['user']) AND $url['user'] != '')
  552. {
  553. $params[] = '-l '.$url['user'];
  554. }
  555. if (isset($url['pass']) AND $url['pass'] != '')
  556. {
  557. $params[] = '-pw '.$url['pass'];
  558. }
  559. if (isset($url['port']) AND $url['port'] != '')
  560. {
  561. $params[] = '-P '.$url['port'];
  562. }
  563. $params[] = $url['host'];
  564. // Get MikroTik additional summary information
  565. if (preg_match('/^\/routing bgp peer print status/i', $exec) AND $os == 'mikrotik' AND $return_buffer != TRUE)
  566. {
  567. if ($instance = @shell_exec('echo n | '.$_CONFIG['plink'].' '.implode(' ', $params).' /routing bgp instance print'))
  568. {
  569. $instance_list = parse_list($instance);
  570. print 'BGP router identifier '.$instance_list['router-id'].', local AS number '.link_as($instance_list['as'])."\n";
  571. }
  572. }
  573. // Get MikroTik version for traceroute
  574. if (preg_match('/^\/tool traceroute/i', $exec) AND $os == 'mikrotik' AND $return_buffer != TRUE)
  575. {
  576. if ($instance = @shell_exec('echo n | '.$_CONFIG['plink'].' '.implode(' ', $params).' /system resource print'))
  577. {
  578. if (preg_match('/version: (\d+)/', $instance, $ver))
  579. {
  580. if (isset($ver[1]))
  581. {
  582. if ($ver[1] == 6)
  583. {
  584. $exec .= ' count=1';
  585. }
  586. $ros = $ver[1];
  587. }
  588. }
  589. }
  590. }
  591. if ($fp = @popen('echo n | '.$_CONFIG['plink'].' '.implode(' ', $params).' '.$exec, 'r'))
  592. {
  593. while (!feof($fp))
  594. {
  595. if (!$output = fgets($fp, 1024))
  596. {
  597. continue;
  598. }
  599. $line = !$return_buffer ? parse_out($output, TRUE) : $output;
  600. if ($line === TRUE)
  601. {
  602. if (!$return_buffer)
  603. {
  604. print '<p class="error">Command aborted.</p>';
  605. }
  606. break;
  607. }
  608. if ($line === FALSE OR $return_buffer === TRUE)
  609. {
  610. $lines .= $output;
  611. continue;
  612. }
  613. print $line;
  614. flush();
  615. if ($line === NULL)
  616. {
  617. $line = $output;
  618. }
  619. }
  620. pclose($fp);
  621. }
  622. break;
  623. case 'telnet':
  624. if (!isset($url['port']) OR $url['port'] == '')
  625. {
  626. $url['port'] = 23;
  627. }
  628. if (!isset($url['user']) OR $url['user'] == '')
  629. {
  630. $url['user'] = FALSE;
  631. }
  632. if (!isset($url['pass']) OR $url['pass'] == '')
  633. {
  634. $url['pass'] = FALSE;
  635. }
  636. try
  637. {
  638. if ($os == 'mikrotik')
  639. {
  640. $url['user'] .= '+ct';
  641. $prompt = '/[^\s]{2,} [>]/';
  642. }
  643. else
  644. {
  645. $prompt = '/[^\s]{2,}[\$%>] {0,1}$/';
  646. }
  647. $telnet = new Telnet($url['host'], $url['port']);
  648. $telnet->connect();
  649. $telnet->login($url['user'], $url['pass']);
  650. $telnet->write(($os == 'junos') ? $exec.' | no-more' : $exec);
  651. $buffer = '';
  652. $i = $j = 0;
  653. do
  654. {
  655. $c = $telnet->getc();
  656. if ($c === false)
  657. {
  658. break;
  659. }
  660. if ($c == $telnet->IAC)
  661. {
  662. if ($telnet->negotiateTelnetOptions())
  663. {
  664. continue;
  665. }
  666. }
  667. $buffer .= $c;
  668. // Clear buffer berofe backspace or escape notice
  669. if ($c == "\x08" OR $buffer == "Type escape sequence to abort.\r\n")
  670. {
  671. $buffer = '';
  672. continue;
  673. }
  674. //$c = preg_replace('/\[\d;?(\d+)?;?(\d)?m/x', ' ', $c); // Strip remaining
  675. //$c = preg_replace('/\x1B\x5B\x30\x6D/x', '\x0A', $c); // Convert to \n
  676. //$c = preg_replace('/[\x80-\xFF]/x', ' ', $c); // Strip Ext ASCII
  677. //$c = preg_replace('/[\x00-\x09\x0B-\x1F]/x', ' ', $c); // Strip Low ASCII
  678. $buffer = preg_replace('/\x1b\x5b;?([^\x6d]+)?\x6d/x', '', $buffer); // Strip colors
  679. if (preg_match($prompt, substr($buffer, -4)))
  680. {
  681. if ($j < 2)
  682. {
  683. $j++;
  684. if (substr($buffer, -2) != "\r\n")
  685. {
  686. $telnet->write();
  687. $i = 0;
  688. continue;
  689. }
  690. }
  691. $telnet->disconnect();
  692. break;
  693. }
  694. if (substr($buffer, -10) == ' --More-- ')
  695. {
  696. $telnet->write();
  697. }
  698. else if (substr($buffer, -2) == "\r\n")
  699. {
  700. // Clear buffer on first empty line
  701. if ($i == 1 AND $buffer == "\r\n")
  702. {
  703. $buffer = '';
  704. }
  705. $i++;
  706. if ($i > 1)
  707. {
  708. $line = !$return_buffer ? parse_out($buffer, TRUE) : $buffer;
  709. if ($line === TRUE)
  710. {
  711. if (!$return_buffer)
  712. {
  713. print '<p class="error">Command aborted.</p>';
  714. }
  715. $telnet->disconnect();
  716. break;
  717. }
  718. if ($line === FALSE OR $return_buffer === TRUE)
  719. {
  720. $lines .= $buffer;
  721. }
  722. else
  723. {
  724. print $line;
  725. flush();
  726. if ($line === NULL)
  727. {
  728. $line = $buffer;
  729. }
  730. }
  731. }
  732. $buffer = '';
  733. }
  734. }
  735. while ($c != $telnet->NULL OR $c != $telnet->DC1);
  736. }
  737. catch (Exception $exception)
  738. {
  739. $is_exception = TRUE;
  740. if (!$return_buffer)
  741. {
  742. print '<p class="error">Telnet error: '.$exception->getMessage().'</p>';
  743. }
  744. }
  745. break;
  746. }
  747. if ($lines)
  748. {
  749. if ($return_buffer)
  750. {
  751. return $lines;
  752. }
  753. if ($line = parse_out($lines))
  754. {
  755. print $line;
  756. }
  757. if (empty($line) AND $is_exception == FALSE)
  758. {
  759. print '<p class="error">Command failed.</p>';
  760. }
  761. }
  762. flush();
  763. }
  764. /**
  765. * Parse output contents
  766. */
  767. function parse_out($output, $check = FALSE)
  768. {
  769. global $_CONFIG, $router, $protocol, $os, $command, $exec, $query, $index, $lastip, $best, $count, $str_in, $ros;
  770. $output = str_replace("\r\n", "\n", $output);
  771. // MikroTik
  772. if (preg_match("/^\/(ip|ipv6) route print detail/i", $exec) AND $os == 'mikrotik')
  773. {
  774. if ($check)
  775. {
  776. return FALSE;
  777. }
  778. $output_parts = explode("\n" , trim($output), ($protocol != 'ipv6' ? 4 : 3));
  779. if (!isset($output_parts[2]))
  780. {
  781. return 'Records for '.strip_tags($query).' is not found';
  782. }
  783. $summary_parts = explode("\n\n" , $output_parts[($protocol != 'ipv6' ? 3 : 2)]);
  784. $output = implode("\n", array_slice($output_parts, 0, ($protocol != 'ipv6' ? 3 : 2)))."\n\n";
  785. foreach ($summary_parts as $summary_part)
  786. {
  787. $data_exp = explode(' ', trim($summary_part), 3);
  788. $summary_part = preg_replace_callback(
  789. "/bgp-as-path=\"([^\"]+)\"/x",
  790. function ($matches) {
  791. return stripslashes('bgp-as-path=\"'.link_as($matches[1]).'\"');
  792. },
  793. $summary_part
  794. );
  795. if (strpos($data_exp[1], 'A') !== FALSE)
  796. {
  797. $output .= '<span style="color:#ff0000">'.$summary_part."</span>\n\n";
  798. }
  799. else
  800. {
  801. $output .= $summary_part."\n\n";
  802. }
  803. }
  804. return $output;
  805. }
  806. // MikroTik
  807. if (preg_match("/^\/routing bgp advertisements print/i", $exec) AND $os == 'mikrotik')
  808. {
  809. return preg_replace_callback(
  810. "/^(.{8}\s)([\d\.A-Fa-f:\/]+)(\s+)/",
  811. function ($matches) {
  812. return $matches[1].link_command("bgp", $matches[2]).$matches[3];
  813. },
  814. $output
  815. );
  816. }
  817. // MikroTik
  818. if (preg_match("/^\/(ip|ipv6) route print/i", $exec) AND $os == 'mikrotik')
  819. {
  820. return preg_replace_callback(
  821. "/(^[\s\d]+)(\s+[A-z]+\s+)([\d\.A-Fa-f:\/]+)(\s+)/",
  822. function ($matches) {
  823. return $matches[1].$matches[2].link_command("bgp", $matches[3]).$matches[4];
  824. },
  825. $output
  826. );
  827. }
  828. // MikroTik
  829. if (preg_match("/^\/routing bgp peer print status/i", $exec) AND $os == 'mikrotik')
  830. {
  831. if ($check)
  832. {
  833. return FALSE;
  834. }
  835. $output_parts = explode("\n" , trim($output), 2);
  836. if (!isset($output_parts[1]))
  837. {
  838. return 'Records for '.strip_tags($query).' is not found';
  839. }
  840. $summary_parts = explode("\n\n" , $output_parts[1]);
  841. $table_array[] = array
  842. (
  843. 'remote-address' => 'Neighbor',
  844. 'name' => 'PeerName',
  845. 'remote-id' => 'RemoteID',
  846. 'remote-as' => 'AS',
  847. 'withdrawn-received' => 'MsgRcvd',
  848. 'updates-received' => 'MsgSent',
  849. 'uptime' => 'Up/Down',
  850. 'state' => 'State',
  851. 'prefix-count' => 'PfxRcd',
  852. 'updates-sent' => 'PfxSnd',
  853. );
  854. foreach ($summary_parts as $summary_part)
  855. {
  856. $data_exp = explode(' ', trim($summary_part), 3);
  857. if (sizeof($data_exp) != 3)
  858. {
  859. continue;
  860. }
  861. $data = parse_list($data_exp[2]);
  862. $table_array[] = array
  863. (
  864. 'remote-address' => isset($data['remote-address']) ? link_whois($data['remote-address']) : '',
  865. 'name' => isset($data['name']) ? $data['name'] : '',
  866. 'remote-id' => isset($data['remote-id']) ? $data['remote-id'] : '',
  867. 'remote-as' => isset($data['remote-as']) ? link_as($data['remote-as']) : '',
  868. 'withdrawn-received' => isset($data['withdrawn-received']) ? $data['withdrawn-received'] : '',
  869. 'updates-received' => isset($data['updates-received']) ? $data['updates-received'] : '',
  870. 'uptime' => isset($data['uptime']) ? $data['uptime'] : '',
  871. 'state' => $data_exp[1],
  872. 'prefix-count' => (isset($data['remote-address']) AND isset($data['prefix-count']))
  873. ? link_command('routes', $data['remote-address'], $data['prefix-count'])
  874. : '',
  875. 'updates-sent' => (isset($data['name']) AND isset($data['updates-sent']))
  876. ? link_command('advertised-routes', $data['name'], $data['updates-sent'])
  877. : '',
  878. );
  879. }
  880. return
  881. str_replace('Flags', 'State', $output_parts[0])."\n\n".
  882. build_table($table_array)."\n\n".
  883. 'Total number of neighbors '.(sizeof($table_array) - 1);
  884. }
  885. // MikroTik
  886. if (preg_match("/^\/ping/i", $exec) AND $os == 'mikrotik')
  887. {
  888. if (preg_match('/^HOST/', $output) AND $index == 0)
  889. {
  890. $index++;
  891. return 'PING '.$query.' ('.get_ptr($query).'): 56 data bytes'."\n";
  892. }
  893. if ($index > 0)
  894. {
  895. $exp = explode(' ', preg_replace('/[\s\t]+/', ' ', trim($output)));
  896. $exp = array_slice($exp, 0, 4);
  897. if (!$exp OR $exp[0] == '' OR $exp[0] == 'HOST' OR (isset($exp[1]) AND $exp[1] == 'timeout'))
  898. {
  899. return;
  900. }
  901. if (preg_match('/sent=([0-9]+) received=([0-9]+) packet-loss=([^ ]+)/', $output, $stat))
  902. {
  903. $return = "\n".'--- '.$query.' ping statistics ---'."\n".
  904. $stat[1].' packets transmitted, '.$stat[2].' packets received, '.trim($stat[3]).' packet loss'."\n";
  905. if (preg_match('/min-rtt=([0-9]+)ms avg-rtt=([0-9]+)ms/', $output, $stat2))
  906. {
  907. $return .= 'round-trip min/avg/max = '.$stat2[1].'/'.$stat2[2];
  908. }
  909. if (preg_match('/max-rtt=([0-9]+)ms/', $output, $stat3))
  910. {
  911. $return .= '/'.$stat3[1];
  912. }
  913. return $return;
  914. }
  915. // Пинг больше 99, отдача max-rtt отдельной строкой
  916. if (preg_match('/max-rtt=([0-9]+)ms/', $output, $stat3))
  917. {
  918. $return = '/'.$stat3[1];
  919. return $return;
  920. }
  921. $index++;
  922. return $exp[1].' bytes from '.$exp[0].': icmp_seq='.intval($index-1).' ttl='.$exp[2].' time='.$exp[3]."\n";
  923. }
  924. }
  925. // MikroTik
  926. if (preg_match("/^\/tool trace/i", $exec) AND $os == 'mikrotik' AND $ros >= 6)
  927. {
  928. if (preg_match('/^ # ADDRESS/', $output) AND $index == 0)
  929. {
  930. $index++;
  931. return 'traceroute to '.$query.' ('.get_ptr($query).'), 64 hops max, 60 byte packets'."\n";
  932. }
  933. if ($index > 0)
  934. {
  935. $exp = explode(' ', preg_replace('/[\s\t]+/', ' ', trim($output)));
  936. $exp = array_slice($exp, 0, 9);
  937. if (!$exp OR empty($exp[0]) OR (sizeof($exp) == 4 AND isset($exp[3]) AND $exp[3] != 'timeout'))
  938. {
  939. return;
  940. }
  941. $new_exp[0] = (strlen($exp[0]) < 2) ? ' '.$exp[0] : $exp[0];
  942. if ($exp[3] == 'timeout')
  943. {
  944. $new_exp[1] = '* * *';
  945. $new_exp = array_slice($new_exp, 0, 2);
  946. }
  947. else
  948. {
  949. $radb = get_radb($exp[1]);
  950. $new_exp[1] = get_ptr($exp[1]);
  951. $new_exp[2] = '('.$exp[1].')';
  952. $new_exp[3] = '['.(isset($radb['origin']) ? 'AS '.link_as($radb['origin']) : '').']';
  953. $new_exp[4] = $exp[5].'ms';
  954. $new_exp[5] = $exp[6].'ms';
  955. $new_exp[6] = $exp[7].'ms';
  956. }
  957. if ($index >= 64)
  958. {
  959. return TRUE;
  960. }
  961. $str = implode(' ', $new_exp)."\r\n";
  962. if (@in_array($str, $str_in))
  963. {
  964. return;
  965. }
  966. $str_in[] = $str;
  967. $index++;
  968. return $str;
  969. }
  970. }
  971. // MikroTik
  972. if (preg_match("/^\/tool trace/i", $exec) AND $os == 'mikrotik' AND $ros <= 5)
  973. {
  974. if (preg_match('/^ # ADDRESS/', $output) AND $index == 0)
  975. {
  976. $index++;
  977. return 'traceroute to '.$query.' ('.get_ptr($query).'), 64 hops max, 60 byte packets'."\n";
  978. }
  979. if ($index > 0)
  980. {
  981. $exp = explode(' ', preg_replace('/[\s\t]+/', ' ', trim($output)));
  982. $exp = array_slice($exp, 0, 5);
  983. if (!$exp OR empty($exp[0]))
  984. {
  985. return;
  986. }
  987. $exp[0] = (strlen($exp[0]) < 2) ? ' '.$exp[0] : $exp[0];
  988. if ($exp[1] == '0.0.0.0' OR $exp[1] = '::')
  989. {
  990. $exp[1] = '* * *';
  991. $exp = array_slice($exp, 0, 2);
  992. }
  993. else
  994. {
  995. $radb = get_radb($exp[1]);
  996. $exp[6] = $exp[4];
  997. $exp[5] = $exp[3];
  998. $exp[4] = $exp[2];
  999. $exp[3] = '['.(isset($radb['origin']) ? 'AS '.link_as($radb['origin']) : '').']';
  1000. $exp[2] = '('.$exp[1].')';
  1001. $exp[1] = get_ptr($exp[1]);
  1002. }
  1003. if ($index >= 64)
  1004. {
  1005. return TRUE;
  1006. }
  1007. $index++;
  1008. return implode(' ', $exp)."\r\n";
  1009. }
  1010. }
  1011. // Other OS parsers
  1012. if (preg_match('/^show bgp ipv6 unicast/i', $exec) AND $os = 'ios')
  1013. {
  1014. $output = str_replace('% Incomplete command.', '<p class="error">Please use network prefix in Parameter.</p>', $output);
  1015. }
  1016. if ($exec == 'show ip bgp summary')
  1017. {
  1018. $output = preg_replace_callback(
  1019. "/( local AS number )(\d+)/",
  1020. function ($matches) {
  1021. return $matches[1].link_as($matches[2]);
  1022. },
  1023. $output
  1024. );
  1025. // JunOS
  1026. if ($os == 'junos')
  1027. {
  1028. $output = preg_replace_callback(
  1029. "/^([\dA-Fa-f:\.]+\s+)(\d+)/",
  1030. function ($matches) {
  1031. return $matches[1].link_as($matches[2]);
  1032. },
  1033. $output
  1034. );
  1035. }
  1036. else
  1037. {
  1038. $output = preg_replace_callback(
  1039. "/^([\d\.]+\s+\d+\s+)(\d+)/",
  1040. function ($matches) {
  1041. return $matches[1].link_as($matches[2]);
  1042. },
  1043. $output
  1044. );
  1045. }
  1046. $output = preg_replace_callback(
  1047. "/^(\d+\.\d+\.\d+\.\d+)(\s+.*\s+)([1-9]\d*)\n$/",
  1048. function ($matches) {
  1049. return $matches[1].$matches[2].link_command("routes", $matches[1], $matches[3])."\n";
  1050. },
  1051. $output
  1052. );
  1053. $output = preg_replace_callback(
  1054. "/^(\d+\.\d+\.\d+\.\d+)(\s+)/",
  1055. function ($matches) {
  1056. return link_command("bgp", "neighbors+".$matches[1], $matches[1]).$matches[2];
  1057. },
  1058. $output
  1059. );
  1060. // IPv6 neighbours
  1061. $output = preg_replace_callback(
  1062. "/^(.{15} 4\s+)(\d+)/",
  1063. function ($matches) {
  1064. return $matches[1].link_as($matches[2]);
  1065. },
  1066. $output
  1067. );
  1068. $output = preg_replace_callback(
  1069. "/^([\dA-Fa-f]*:[\dA-Fa-f:]*)(\s+)/",
  1070. function ($matches) {
  1071. return link_command("bgp", "neighbors+".$matches[1], $matches[1]).$matches[2];
  1072. },
  1073. $output
  1074. );
  1075. $output = preg_replace_callback(
  1076. "/^([\dA-Fa-f]*:[\dA-Fa-f:]*)\n$/",
  1077. function ($matches) {
  1078. return link_command("bgp", "neighbors+".$matches[1], $matches[1])."\n";
  1079. },
  1080. $output
  1081. );
  1082. return $output;
  1083. }
  1084. if ($exec == 'show ipv6 bgp summary' OR $exec == 'show bgp ipv6 unicast summary')
  1085. {
  1086. $output = preg_replace_callback(
  1087. "/^(.{15} 4\s+)(\d+)/",
  1088. function ($matches) {
  1089. return $matches[1].link_as($matches[2]);
  1090. },
  1091. $output
  1092. );
  1093. if (preg_match("/^([\dA-Fa-f]*:[\dA-Fa-f:]*)\s+4\s+/", $output, $lastip_exp))
  1094. {
  1095. $lastip = $lastip_exp[1];
  1096. $output = preg_replace_callback(
  1097. "/^(.{15} 4\s+)(\d+)/",
  1098. function ($matches) {
  1099. return $matches[1].link_as($matches[2]);
  1100. },
  1101. $output
  1102. );
  1103. $output = preg_replace_callback(
  1104. "/^([\dA-Fa-f:]+)(\s+.*\s+)([1-9]\d*)$/",
  1105. function ($matches) {
  1106. global $lastip;
  1107. return link_command("routes", $lastip, $matches[3]);
  1108. },
  1109. $output
  1110. );
  1111. $output = preg_replace_callback(
  1112. "/^([\dA-Fa-f:]+)(\s+)/",
  1113. function ($matches) {
  1114. return link_command("routes", $matches[1]).$matches[2];
  1115. },
  1116. $output
  1117. );
  1118. unset($lastip);
  1119. }
  1120. if (preg_match("/^([\dA-Fa-f:]+)\n$/", $output, $lastip_exp))
  1121. {
  1122. $lastip = $lastip_exp[1];
  1123. $output = preg_replace_callback(
  1124. "/^([\dA-Fa-f:]+)/",
  1125. function ($matches) {
  1126. return link_command("routes", $matches[1]);
  1127. },
  1128. $output
  1129. );
  1130. }
  1131. if (isset($lastip) AND preg_match("/^(\s+.*\s+)([1-9]\d*)\n$/", $output))
  1132. {
  1133. $output = preg_replace_callback(
  1134. "/^(\s+.*\s+)([1-9]\d*)\n$/",
  1135. function ($matches) {
  1136. global $lastip;
  1137. return $matches[1].link_command("routes", $lastip, $matches[2])."\n";
  1138. },
  1139. $output
  1140. );
  1141. unset($lastip);
  1142. }
  1143. return $output;
  1144. }
  1145. if ($exec == 'show bgp summary')
  1146. {
  1147. // JunOS
  1148. if (preg_match("/^([\dA-Fa-f:][\d\.A-Fa-f:]+)\s+/", $output, $lastip_exp))
  1149. {
  1150. $lastip = $lastip_exp[1];
  1151. // IPv4
  1152. $output = preg_replace_callback(
  1153. "/^(\d+\.\d+\.\d+\.\d+)(\s+.*\s+)([1-9]\d*)(\s+\d+\s+\d+\s+\d+\s+\d+\s+[\d:]+\s+)(\d+)\/(\d+)\/(\d+)(\s+)/",
  1154. function ($matches) {
  1155. return $matches[1].$matches[2].
  1156. link_command("routes", $matches[1], $matches[3]).$matches[4].
  1157. link_command("routes", $matches[1], $matches[5])."/".
  1158. link_command("routes", $matches[1], $matches[6])."/".
  1159. link_command("routes", $matches[1], $matches[7]).$matches[8];
  1160. },
  1161. $output
  1162. );
  1163. // IPv4/IPv6
  1164. $output = preg_replace_callback(
  1165. "/^([\dA-Fa-f:][\d\.A-Fa-f:]+\s+)(\d+)(\s+)/",
  1166. function ($matches) {
  1167. return $matches[1].link_as($matches[2]).$matches[3];
  1168. },
  1169. $output
  1170. );
  1171. $output = preg_replace_callback(
  1172. "/^([\dA-Fa-f:][\d\.A-Fa-f:]+)(\s+)/",
  1173. function ($matches) {
  1174. return link_command("bgp", "neighbors+".$matches[1], $matches[1]).$matches[2];
  1175. },
  1176. $output
  1177. );
  1178. }
  1179. if (isset($lastip) AND preg_match("/( [^:]+: )(\d+)\/(\d+)\/(\d+)$/", $output))
  1180. {
  1181. $output = preg_replace_callback(
  1182. "/^( [^:]+: )(\d+)\/(\d+)\/(\d+)\n$/",
  1183. function ($matches) {
  1184. global $lastip;
  1185. return "\\1".
  1186. link_command("routes", $lastip, $matches[2])."/".
  1187. link_command("bgp", "neighbors+".$lastip."+routes+all", $matches[3])."/".
  1188. link_command("bgp", "neighbors+".$lastip."+routes+damping+suppressed", $matches[4])."\n";
  1189. },
  1190. $output
  1191. );
  1192. unset($lastip);
  1193. }
  1194. return $output;
  1195. }
  1196. if (preg_match("/^show ip bgp\s+n\w*\s+[\d\.]+\s+(ro|re|a)/i", $exec) OR
  1197. preg_match("/^show (ipv6 bgp|bgp ipv6)\s+n\w*\s+[\dA-Fa-f:]+\s+(ro|re|a)/i", $exec) OR
  1198. preg_match("/^show (ipv6 bgp|bgp ipv6)\s+re/i", $exec) OR
  1199. preg_match("/^show ip bgp\s+[\d\.]+\s+[\d\.]+\s+(l|s)/i", $exec) OR
  1200. preg_match("/^show (ip bgp|bgp ipv6) prefix-list/i", $exec) OR
  1201. preg_match("/^show (ip bgp|bgp ipv6) route-map/i", $exec))
  1202. {
  1203. $output = preg_replace_callback(
  1204. "/^([\*r ](>|d|h| ).{59})([\d\s,\{\}]+)([ie\?])\n$/",
  1205. function ($matches) {
  1206. return $matches[1].link_as($matches[3]).$matches[4]."\n";
  1207. },
  1208. $output
  1209. );
  1210. $output = preg_replace_callback(
  1211. "/^([\*r ](>|d|h| )[i ])([\d\.A-Fa-f:\/]+)(\s+)/",
  1212. function ($matches) {
  1213. return $matches[1].link_command("bgp", $matches[3]).$matches[4];
  1214. },
  1215. $output
  1216. );
  1217. $output = preg_replace_callback(
  1218. "/^([\*r ](>|d|h| )[i ])([\d\.A-Fa-f:\/]+)\n$/",
  1219. function ($matches) {
  1220. return $matches[1].link_command("bgp", $matches[3])."\n";
  1221. },
  1222. $output
  1223. );
  1224. $output = preg_replace_callback(
  1225. "/^(( ){20}.{41})([\d\s,\{\}]+)([ie\?])\n$/",
  1226. function ($matches) {
  1227. return $matches[1].link_as($matches[3]).$matches[4]."\n";
  1228. },
  1229. $output
  1230. );
  1231. $output = preg_replace_callback(
  1232. "/(, remote AS )(\d+)(,)/",
  1233. function ($matches) {
  1234. return $matches[1].link_as($matches[2]).$matches[3]."\n";
  1235. },
  1236. $output
  1237. );
  1238. return $output;
  1239. }
  1240. // JunOS
  1241. if (preg_match("/^show route receive-protocol bgp\s+[\d\.A-Fa-f:]+/i", $exec))
  1242. {
  1243. $output = preg_replace_callback(
  1244. "/(Community: )([\d: ]+)/",
  1245. function ($matches) {
  1246. return $matches[1].link_community($matches[2]);
  1247. },
  1248. $output
  1249. );
  1250. $output = preg_replace_callback(
  1251. "/(Communities: )([\d: ]+)/",
  1252. function ($matches) {
  1253. return $matches[1].link_community($matches[2]);
  1254. },
  1255. $output
  1256. );
  1257. $output = preg_replace_callback(
  1258. "/(^\s+AS path: )([\d ]+)/",
  1259. function ($matches) {
  1260. return $matches[1].link_as($matches[2]);
  1261. },
  1262. $output
  1263. );
  1264. $output = preg_replace_callback(
  1265. "/^([\d\.\s].{24})([\d\.]+)(\s+)/",
  1266. function ($matches) {
  1267. return $matches[1].link_command("bgp", "neighbors+".$matches[2], $matches[2]);
  1268. },
  1269. $output
  1270. );
  1271. $output = preg_replace_callback(
  1272. "/^([\d\.\/]+)(\s+)/",
  1273. function ($matches) {
  1274. return link_command("bgp", $matches[1]).$matches[2];
  1275. },
  1276. $output
  1277. );
  1278. $output = preg_replace_callback(
  1279. "/^([\d\.A-Fa-f:\/]+)(\s+)/",
  1280. function ($matches) {
  1281. return link_command("bgp", $matches[1]."+exact", $matches[1]).$matches[2];
  1282. },
  1283. $output
  1284. );
  1285. $output = preg_replace_callback(
  1286. "/^([\d\.A-Fa-f:\/]+)\s*\n$/",
  1287. function ($matches) {
  1288. return link_command("bgp", $matches[1]."+exact", $matches[1]);
  1289. },
  1290. $output
  1291. );
  1292. $output = preg_replace_callback(
  1293. "/^([ \*] )([\d\.A-Fa-f:\/]+)(\s+)/",
  1294. function ($matches) {
  1295. return $matches[1].link_command("bgp", $matches[2]."+exact", $matches[2]).$matches[3];
  1296. },
  1297. $output
  1298. );
  1299. return $output;
  1300. }
  1301. // JunOS
  1302. if (preg_match("/^show route advertising-protocol bgp\s+([\d\.A-Fa-f:]+)$/i", $exec, $ip_exp))
  1303. {
  1304. $ip = $ip_exp[1];
  1305. $output = preg_replace_callback(
  1306. "/^([\d\.\s].{64})([\d\s,\{\}]+)([I\?])\n$/",
  1307. function ($matches) {
  1308. return $matches[1].link_as($matches[2]).$matches[3]."\n";
  1309. },
  1310. $output
  1311. );
  1312. $output = preg_replace_callback(
  1313. "/^([\d\.\s].{24})([\d\.]+)(\s+)/",
  1314. function ($matches) {
  1315. return $matches[1].link_command("bgp", "neighbors+".$matches[2], $matches[2]).$matches[3];
  1316. },
  1317. $output
  1318. );
  1319. $output = preg_replace_callback(
  1320. "/^([\d\.\/]+)(\s+)/",
  1321. function ($matches) {
  1322. return link_command("bgp", $matches[1]).$matches[2];
  1323. },
  1324. $output
  1325. );
  1326. $output = preg_replace_callback(
  1327. "/^([\d\.A-Fa-f:\/]+)(\s+)/",
  1328. function ($matches) {
  1329. return link_command("bgp", $matches[1]."+exact", $matches[1]).$matches[2];
  1330. },
  1331. $output
  1332. );
  1333. $output = preg_replace_callback(
  1334. "/^([\d\.A-Fa-f:\/]+)\s*\n$/",
  1335. function ($matches) {
  1336. return link_command("bgp", $matches[1]."+exact", $matches[1])."\n";
  1337. },
  1338. $output
  1339. );
  1340. $output = preg_replace_callback(
  1341. "/^([ \*] )([\d\.A-Fa-f:\/]+)(\s+)/",
  1342. function ($matches) {
  1343. global $ip;
  1344. return $matches[1].link_command("bgp", "neighbors+".$ip."+advertised-routes+".$matches[2], $matches[2]).$matches[3];
  1345. },
  1346. $output
  1347. );
  1348. return $output;
  1349. }
  1350. if (preg_match("/^show ip bgp n\w*\s+([\d\.]+)/i", $exec) OR
  1351. preg_match("/^show ip bgp n\w*$/i", $exec))
  1352. {
  1353. if (!isset($lastip) AND preg_match("/^BGP neighbor is ([\d\.]+),/", $output, $lastip_exp))
  1354. {
  1355. $lastip = $lastip_exp[1];
  1356. }
  1357. $output = preg_replace_callback(
  1358. "/(Prefix )(advertised)( [1-9]\d*)/",
  1359. function ($matches) {
  1360. global $lastip;
  1361. return $matches[1].link_command("advertised-routes", $lastip, $matches[2]).$matches[3];
  1362. },
  1363. $output
  1364. );
  1365. $output = preg_replace_callback(
  1366. "/( Prefixes Total: )(\d+)( )/",
  1367. function ($matches) {
  1368. global $lastip;
  1369. return $matches[1].link_command("advertised-routes", $lastip, $matches[2]).$matches[3];
  1370. },
  1371. $output
  1372. );
  1373. $output = preg_replace_callback(
  1374. "/(prefixes )(received)( [1-9]\d*)/",
  1375. function ($matches) {
  1376. global $lastip;
  1377. return $matches[1].link_command("routes", $lastip, $matches[2]).$matches[3];
  1378. },
  1379. $output
  1380. );
  1381. $output = preg_replace_callback(
  1382. "/( Prefixes Current: \s+)(\d+)(\s+)(\d+)/",
  1383. function ($matches) {
  1384. global $lastip;
  1385. return $matches[1].
  1386. link_command("advertised-routes", $lastip, $matches[2]).$matches[3].
  1387. link_command("routes", $lastip, $matches[4]);
  1388. },
  1389. $output
  1390. );
  1391. $output = preg_replace_callback(
  1392. "/(\s+)(Received)( prefixes:\s+[1-9]\d*)/",
  1393. function ($matches) {
  1394. global $lastip;
  1395. return $matches[1].link_command("routes", $lastip, $matches[2]).$matches[3];
  1396. },
  1397. $output
  1398. );
  1399. $output = preg_replace_callback(
  1400. "/( Saved \(soft-reconfig\):\s+)(\d+|n\/a)(\s+)(\d+)/",
  1401. function ($matches) {
  1402. global $lastip;
  1403. return $matches[1].$matches[2].$matches[3].link_command("received-routes", $lastip, $matches[4]);
  1404. },
  1405. $output
  1406. );
  1407. $output = preg_replace_callback(
  1408. "/( [1-9]\d* )(accepted)( prefixes)/",
  1409. function ($matches) {
  1410. global $lastip;
  1411. return $matches[1].link_command("routes", $lastip, $matches[2]).$matches[3];
  1412. },
  1413. $output
  1414. );
  1415. $output = preg_replace_callback(
  1416. "/^( [1-9]\d* )(accepted|denied but saved)( prefixes consume \d+ bytes)/",
  1417. function ($matches) {
  1418. global $lastip;
  1419. return $matches[1].link_command("received-routes", $lastip, $matches[2]).$matches[3];
  1420. },
  1421. $output
  1422. );
  1423. $output = preg_replace_callback(
  1424. "/^(BGP neighbor is )(\d+\.\d+\.\d+\.\d+)(,)/",
  1425. function ($matches) {
  1426. return $matches[1].link_whois($matches[2]).$matches[3];
  1427. },
  1428. $output
  1429. );
  1430. $output = preg_replace("/^( Description: )(.*)$/", '\\1<b>\\2</b>', $output);
  1431. $output = preg_replace_callback(
  1432. "/(,\s+remote AS )(\d+)(,)/",
  1433. function ($matches) {
  1434. return $matches[1].link_as($matches[2]).$matches[3];
  1435. },
  1436. $output
  1437. );
  1438. $output = preg_replace_callback(
  1439. "/(, local AS )(\d+)(,)/",
  1440. function ($matches) {
  1441. return $matches[1].link_as($matches[2]).$matches[3];
  1442. },
  1443. $output
  1444. );
  1445. $output = preg_replace_callback(
  1446. "/( update prefix filter list is )(\S+)/",
  1447. function ($matches) {
  1448. return $matches[1].link_command("bgp", "prefix-list+".$matches[2], $matches[2]);
  1449. },
  1450. $output
  1451. );
  1452. $output = preg_replace_callback(
  1453. "/(Route map for \S+ advertisements is\s+:?\*?)(\S+)/",
  1454. function ($matches) {
  1455. return $matches[1].link_command("bgp", "route-map+".$matches[2], $matches[2]);
  1456. },
  1457. $output
  1458. );
  1459. return $output;
  1460. }
  1461. if (preg_match("/^show (ipv6 bgp|bgp ipv6) n\w*\s+([\dA-Fa-f:]+)/i", $exec, $ip_exp))
  1462. {
  1463. $ip = $ip_exp[1];
  1464. $output = preg_replace_callback(
  1465. "/(Prefix )(advertised)( [1-9]\d*)/",
  1466. function ($matches) {
  1467. global $ip;
  1468. return $matches[1].link_command("advertised-routes", "'.$ip.'", $matches[2]).$matches[3];
  1469. },
  1470. $output
  1471. );
  1472. $output = preg_replace_callback(
  1473. "/( [1-9]\d* )(accepted)( prefixes)/",
  1474. function ($matches) {
  1475. global $ip;
  1476. return $matches[1].link_command("routes", $ip, $matches[2]).$matches[3];
  1477. },
  1478. $output
  1479. );
  1480. $output = preg_replace("/^( Description: )(.*)$/", '\\1<b>\\2</b>', $output);
  1481. $output = preg_replace_callback(
  1482. "/(\s+remote AS )(\d+)(,)/",
  1483. function ($matches) {
  1484. return $matches[1].link_as($matches[2]).$matches[3];
  1485. },
  1486. $output
  1487. );
  1488. $output = preg_replace_callback(
  1489. "/(\s+local AS )(\d+)(,)/",
  1490. function ($matches) {
  1491. return $matches[1].link_as($matches[2]).$matches[3];
  1492. },
  1493. $output
  1494. );
  1495. $output = preg_replace_callback(
  1496. "/( update prefix filter list is )(\S+)/",
  1497. function ($matches) {
  1498. return $matches[1].link_command("bgp", "prefix-list+".$matches[2], $matches[2]);
  1499. },
  1500. $output
  1501. );
  1502. $output = preg_replace_callback(
  1503. "/(Route map for \S+ advertisements is\s+:?\*?)(\S+)/",
  1504. function ($matches) {
  1505. return $matches[1].link_command("bgp", "route-map+".$matches[2], $matches[2]);
  1506. },
  1507. $output
  1508. );
  1509. return $output;
  1510. }
  1511. if (preg_match("/^show bgp n\w*\s+([\d\.A-Fa-f:]+)/i", $exec, $ip_exp))
  1512. {
  1513. $ip = $ip_exp[1];
  1514. $output = preg_replace_callback(
  1515. "/(\s+AS )(\d+)/",
  1516. function ($matches) {
  1517. return $matches[1].link_as($matches[2]);
  1518. },
  1519. $output
  1520. );
  1521. $output = preg_replace_callback(
  1522. "/(\s+AS: )(\d+)/",
  1523. function ($matches) {
  1524. return $matches[1].link_as($matches[2]);
  1525. },
  1526. $output
  1527. );
  1528. $output = preg_replace_callback(
  1529. "/^( Active prefixes:\s+)(\d+)/",
  1530. function ($matches) {
  1531. global $ip;
  1532. return $matches[1].link_command("routes", $ip, $matches[2]);
  1533. },
  1534. $output
  1535. );
  1536. $output = preg_replace_callback(
  1537. "/^( Received prefixes:\s+)(\d+)/",
  1538. function ($matches) {
  1539. global $ip;
  1540. return $matches[1].link_command("bgp", "neighbors+".$ip."+routes+all", $matches[2]);
  1541. },
  1542. $output
  1543. );
  1544. $output = preg_replace_callback(
  1545. "/^( Suppressed due to damping:\s+)(\d+)/",
  1546. function ($matches) {
  1547. global $ip;
  1548. return $matches[1].link_command("bgp", "neighbors+".$ip."+routes+damping+suppressed", $matches[2]);
  1549. },
  1550. $output
  1551. );
  1552. $output = preg_replace_callback(
  1553. "/^( )(Export)(: )/",
  1554. function ($matches) {
  1555. global $ip;
  1556. return $matches[1].link_command("advertised-routes", $ip, $matches[2]).$matches[3];
  1557. },
  1558. $output
  1559. );
  1560. $output = preg_replace_callback(
  1561. "/( )(Import)(: )/",
  1562. function ($matches) {
  1563. global $ip;
  1564. return $matches[1].link_command("bgp", "neighbors+".$ip."+routes+all", $matches[2]).$matches[3];
  1565. },
  1566. $output
  1567. );
  1568. return $output;
  1569. }
  1570. // JunOS
  1571. if (preg_match("/^show route protocol bgp .* terse/i", $exec))
  1572. {
  1573. $output = preg_replace_callback(
  1574. "/^([\* ] )([\d\.A-Fa-f:\/]+)(\s+)/",
  1575. function ($matches) {
  1576. return $matches[1].link_command("bgp", $matches[2]."+exact", $matches[2]).$matches[3];
  1577. },
  1578. $output
  1579. );
  1580. return $output;
  1581. }
  1582. // JunOS
  1583. if (preg_match("/^show route protocol bgp /i", $exec) OR preg_match("/^show route aspath-regex /i", $exec))
  1584. {
  1585. if (preg_match("/^ (.)BGP /", $output, $exp))
  1586. {
  1587. if ($exp[1] == '*')
  1588. {
  1589. $best = '#FF0000';
  1590. }
  1591. else
  1592. {
  1593. $best = '';
  1594. }
  1595. }
  1596. else if (preg_match("/^:?\n?[\n\d\.A-Fa-f:\/\s]{19}([\*\+\- ]{1})\[BGP\//", $output, $exp))
  1597. {
  1598. if ($exp[1] == '*' OR $exp[1] == '+')
  1599. {
  1600. $best = "#ff0000";
  1601. }
  1602. else if ($exp[1] == '-')
  1603. {
  1604. $best = "#008800";
  1605. }
  1606. else
  1607. {
  1608. $best = '';
  1609. }
  1610. }
  1611. else if (preg_match("/^[\n]+$/", $output))
  1612. {
  1613. $best = '';
  1614. }
  1615. $output = preg_replace_callback(
  1616. "/( from )([0-9\.A-Fa-f:]+)/",
  1617. function ($matches) {
  1618. return $matches[1].link_command("bgp", "neighbors+".$matches[2], $matches[2]);
  1619. },
  1620. $output
  1621. );
  1622. $output = preg_replace_callback(
  1623. "/( Source: )([0-9\.A-Fa-f:]+)/",
  1624. function ($matches) {
  1625. return $matches[1].link_command("bgp", "neighbors+".$matches[2], $matches[2]);
  1626. },
  1627. $output
  1628. );
  1629. $output = preg_replace_callback(
  1630. "/(\s+AS: )([\d ]+)/",
  1631. function ($matches) {
  1632. return link_as($matches[2]);
  1633. },
  1634. $output
  1635. );
  1636. $output = preg_replace_callback(
  1637. "/(Community: )([\d: ]+)/",
  1638. function ($matches) {
  1639. return $matches[1].link_community($matches[2]);
  1640. },
  1641. $output
  1642. );
  1643. $output = preg_replace_callback(
  1644. "/(Communities: )([\d: ]+)/",
  1645. function ($matches) {
  1646. return $matches[1].link_community($matches[2]);
  1647. },
  1648. $output
  1649. );
  1650. $output = preg_replace_callback(
  1651. "/(^\s+AS path: )([\d ]+)/",
  1652. function ($matches) {
  1653. return $matches[1].link_as($matches[2]);
  1654. },
  1655. $output
  1656. );
  1657. $output = preg_replace_callback(
  1658. "/^(:?\n?[\dA-Fa-f:]+[\d\.A-Fa-f:\/]+)(\s*)/",
  1659. function ($matches) {
  1660. return "<b>".link_command("bgp", $matches[1]."+exact", $matches[1])."</b>".$matches[2];
  1661. },
  1662. $output
  1663. );
  1664. if (isset($best) AND $best != '')
  1665. {
  1666. $output = '<span style="color:'.$best.'">'.$output.'</span>';
  1667. }
  1668. return $output;
  1669. }
  1670. if (preg_match("/bgp/", $exec))
  1671. {
  1672. $output = preg_replace("|^(BGP routing table entry for) (\S+)|", "\\1 <b>\\2</b>", $output);
  1673. if (preg_match("|^(Paths: .*) best #(\d+)|", $output, $best_exp))
  1674. {
  1675. $best = $best_exp[2];
  1676. $output = preg_replace("|^(Paths: .*) best #(\d+)|", '\\1 <span style="color:#ff0000">best #\\2</span>', $output);
  1677. }
  1678. // Fix for IPv6 route output where there are no addional 3 spaces before addresses
  1679. if (preg_match("/ Advertised to non peer-group peers:/", $output) AND preg_match("/ ipv6 /", $exec))
  1680. {
  1681. $count--;
  1682. }
  1683. if (( preg_match("/^ (\d+.*)/", $output) AND
  1684. !preg_match("/^ \d+\./", $output) AND
  1685. !preg_match("/[a-z\:\.]+/", $output)
  1686. ) OR
  1687. preg_match("/^ Local/", $output) OR
  1688. preg_match("/, \(aggregated by /", $output))
  1689. {
  1690. $count++;
  1691. $output = preg_replace_callback(
  1692. "/^([^\(A-z\n]+)/",
  1693. function ($matches) {
  1694. return link_as($matches[1]);
  1695. },
  1696. $output
  1697. );
  1698. $output = preg_replace_callback(
  1699. "/(, \(aggregated by )(\d+) ([^\)]+)/",
  1700. function ($matches) {
  1701. return $matches[1].link_as($matches[2])." ".link_whois($matches[3]);
  1702. },
  1703. $output
  1704. );
  1705. }
  1706. if (isset($best) AND $best == $count)
  1707. {
  1708. $output = '<span style="color:#ff0000">'.$output.'</span>';
  1709. }
  1710. $output = preg_replace_callback(
  1711. "/( from )([0-9\.A-Fa-f:]+)( )/",
  1712. function ($matches) {
  1713. return $matches[1].link_command("bgp", "neighbors+".$matches[2], $matches[2]).$matches[3];
  1714. },
  1715. $output
  1716. );
  1717. $output = preg_replace_callback(
  1718. "/(Community: )([\d: ]+)/",
  1719. function ($matches) {
  1720. return $matches[1].link_community($matches[2]);
  1721. },
  1722. $output
  1723. );
  1724. $output = preg_replace_callback(
  1725. "/(Communities: )([\d: ]+)/",
  1726. function ($matches) {
  1727. return $matches[1].link_community($matches[2]);
  1728. },
  1729. $output
  1730. );
  1731. $output = preg_replace_callback(
  1732. "/(^\s+AS path: )([\d ]+)/",
  1733. function ($matches) {
  1734. return link_as($matches[2]);
  1735. },
  1736. $output
  1737. );
  1738. return $output;
  1739. }
  1740. if (preg_match("/^trace/", $exec))
  1741. {
  1742. $output = preg_replace("/\[AS0\]\s(.*)/", "\\1", $output);
  1743. // IPv4
  1744. $output = preg_replace_callback(
  1745. "/(\[AS)(\d+)(\])\s(.*)(\))(.*)/",
  1746. function ($matches) {
  1747. return $matches[4].$matches[5]." ".$matches[1]." ".link_as($matches[2]).$matches[3].$matches[6];
  1748. },
  1749. $output
  1750. );
  1751. // IPv6
  1752. $output = preg_replace_callback(
  1753. "/(\[AS)(\d+)(\])\s([^\s]+)\s(.*)/",
  1754. function ($matches) {
  1755. return $matches[4]." ".$matches[1]." ".link_as($matches[2]).$matches[3].$matches[5];
  1756. },
  1757. $output
  1758. );
  1759. $output = preg_replace_callback(
  1760. "/\((.*)\) (\[AS\s+)(\d+)(\])/",
  1761. function ($matches) {
  1762. return get_as($matches[1], $matches[3]);
  1763. },
  1764. $output
  1765. );
  1766. return $output;
  1767. }
  1768. return $output;
  1769. }
  1770. /**
  1771. * Parse `bgp' output contents and return AS pathes
  1772. */
  1773. function parse_bgp_path($output)
  1774. {
  1775. global $os, $exec, $query, $count;
  1776. $best = FALSE;
  1777. $pathes = array();
  1778. $output = str_replace("\r\n", "\n", $output);
  1779. // MikroTik
  1780. if (preg_match("/^\/(ip|ipv6) route print detail/i", $exec) AND $os == 'mikrotik')
  1781. {
  1782. $output_parts = explode("\n" , trim($output), 4);
  1783. if (!isset($output_parts[3]))
  1784. {
  1785. return FALSE;
  1786. }
  1787. $summary_parts = explode("\n\n" , $output_parts[3]);
  1788. foreach ($summary_parts as $i => $summary_part)
  1789. {
  1790. $data_exp = explode(' ', trim($summary_part), 3);
  1791. if (strpos($data_exp[1], 'A') !== FALSE)
  1792. {
  1793. $best = $i;
  1794. }
  1795. if (preg_match("/bgp-as-path=\"([^\"]+)\"/", $summary_part, $exp))
  1796. {
  1797. if ($path = parse_as_path($exp[1]))
  1798. {
  1799. $pathes[] = $path;
  1800. }
  1801. }
  1802. }
  1803. return array
  1804. (
  1805. 'best' => $best,
  1806. 'pathes' => $pathes
  1807. );
  1808. }
  1809. // JunOS
  1810. if (preg_match("/^show route protocol bgp /i", $exec) OR preg_match("/^show route aspath-regex /i", $exec))
  1811. {
  1812. $lines = explode("\n", $output);
  1813. foreach ($lines as $line)
  1814. {
  1815. if (preg_match("/^ (.)BGP /", $line, $exp))
  1816. {
  1817. if ($exp[1] == '*')
  1818. {
  1819. if ($count == 0)
  1820. {
  1821. $count++;
  1822. }
  1823. }
  1824. }
  1825. else if (preg_match("/^:?\n?[\n\d\.A-Fa-f:\/\s]{19}([\*\+\- ]{1})\[BGP\//", $line, $exp))
  1826. {
  1827. if ($exp[1] == '*' OR $exp[1] == '+')
  1828. {
  1829. if ($count == 0)
  1830. {
  1831. $count++;
  1832. }
  1833. }
  1834. }
  1835. if (preg_match("/^\s+AS path: ([\d ]+)/", $line, $exp))
  1836. {
  1837. if ($path = parse_as_path($exp[1]))
  1838. {
  1839. $pathes[] = $path;
  1840. }
  1841. }
  1842. }
  1843. $best = $count - 1;
  1844. return array
  1845. (
  1846. 'best' => $best,
  1847. 'pathes' => $pathes
  1848. );
  1849. }
  1850. // Other OS
  1851. if (preg_match("/bgp/", $exec))
  1852. {
  1853. if (preg_match("|(Paths: .*) best #(\d+)|", $output, $best_exp))
  1854. {
  1855. $best = $best_exp[2] - 1;
  1856. }
  1857. $lines = explode("\n", $output);
  1858. foreach ($lines as $line)
  1859. {
  1860. if (( preg_match("/^ (\d+.*)/", $line) AND
  1861. !preg_match("/^ \d+\./", $line) AND
  1862. !preg_match("/[a-z\:\.]+/", $line)
  1863. ) OR
  1864. preg_match("/^ Local/", $line) OR
  1865. preg_match("/, \(aggregated by /", $line))
  1866. {
  1867. $line = preg_replace("/^([^\(A-z\n]+).*/", "\\1", $line);
  1868. if ($path = parse_as_path($line))
  1869. {
  1870. $pathes[] = $path;
  1871. }
  1872. }
  1873. }
  1874. return array
  1875. (
  1876. 'best' => $best,
  1877. 'pathes' => $pathes
  1878. );
  1879. }
  1880. return FALSE;
  1881. }
  1882. /**
  1883. * Parse list separated by spaces
  1884. */
  1885. function parse_list($array)
  1886. {
  1887. preg_match_all('/([^=]+)=([^\s]+)[\n\s]/is', $array, $out);
  1888. $array = array();
  1889. if (isset($out[1]))
  1890. {
  1891. foreach ($out[1] as $key => $val)
  1892. {
  1893. $array[trim($out[1][$key])] = trim($out[2][$key], '"');
  1894. }
  1895. }
  1896. return $array;
  1897. }
  1898. /**
  1899. * Parse AS path
  1900. */
  1901. function parse_as_path($line)
  1902. {
  1903. $path = preg_split('/([^\d]+)/', trim($line));
  1904. $return = array();
  1905. foreach ($path as $asn)
  1906. {
  1907. if ($asn != '')
  1908. {
  1909. $return[] = 'AS'.$asn;
  1910. }
  1911. }
  1912. return array_unique($return);
  1913. }
  1914. /**
  1915. * Build list table
  1916. */
  1917. function build_table($table_array)
  1918. {
  1919. $size_array = array();
  1920. foreach ($table_array as $priority => $data)
  1921. {
  1922. foreach ($data as $field => $value)
  1923. {
  1924. $size_array[$field][$priority] = $value;
  1925. }
  1926. }
  1927. $size_max = array();
  1928. foreach ($size_array as $field => $value)
  1929. {
  1930. $size_max[$field] = array_map('mb_strlen', array_map('strip_tags', $value));
  1931. }
  1932. $max = array_map('max', $size_max);
  1933. $return = '';
  1934. foreach ($table_array as $index => $data)
  1935. {
  1936. $line = array();
  1937. foreach ($data as $field => $value)
  1938. {
  1939. if (reset(array_keys($data)) == $field)
  1940. {
  1941. $line[] = $value.str_repeat(' ', $max[$field] - mb_strlen(strip_tags($value)));
  1942. }
  1943. else
  1944. {
  1945. $line[] = str_repeat(' ', $max[$field] - mb_strlen(strip_tags($value))).$value;
  1946. }
  1947. }
  1948. $return .= implode(' ', $line)."\n";
  1949. }
  1950. return $return;
  1951. }
  1952. /**
  1953. * Link to execute command
  1954. */
  1955. function link_command($command, $query, $name = '', $return_uri = FALSE)
  1956. {
  1957. global $router, $protocol;
  1958. if ($name == '')
  1959. {
  1960. $name = $query;
  1961. }
  1962. $uri = '?router='.$router.
  1963. '&amp;protocol='.$protocol.
  1964. '&amp;command='.$command.
  1965. '&amp;query='.$query;
  1966. if ($return_uri)
  1967. {
  1968. return $uri;
  1969. }
  1970. return '<a href="'.$uri.'">'.$name.'</a>';
  1971. }
  1972. /**
  1973. * Link to AS community
  1974. */
  1975. function link_community($line)
  1976. {
  1977. global $_CONFIG;
  1978. $communitylist = preg_split('/[^\d:]+/', $line);
  1979. foreach ($communitylist as $i => $community)
  1980. {
  1981. $communitylist[$i] = '<i>'.$community.'</i>';
  1982. }
  1983. return implode(' ', $communitylist);
  1984. }
  1985. /**
  1986. * Link to AS whois
  1987. */
  1988. function link_as($line, $word = FALSE)
  1989. {
  1990. global $_CONFIG;
  1991. //print_r($line);
  1992. return preg_replace("/(?:AS)?([\d]+)/is",
  1993. "<a href=\"".htmlspecialchars($_CONFIG['aswhois'])."AS\\1\" target=\"_blank\">".($word ? 'AS' : '')."\\1</a>", $line);
  1994. }
  1995. function get_as($ip, $original_as)
  1996. {
  1997. if ($original_as == 15835)
  1998. {
  1999. $as = 65535;
  2000. if($conn = fsockopen ('whois.cymru.com', 43))
  2001. {
  2002. fputs($conn, $ip."\r\n");
  2003. $output = '';
  2004. while(!feof($conn))
  2005. {
  2006. $output .= fgets($conn,128);
  2007. }
  2008. $output = explode("\n", $output);
  2009. if (isset($output[1]))
  2010. {
  2011. $_as = explode("|", $output[1]);
  2012. }
  2013. if (isset($_as[0]))
  2014. {
  2015. $as = trim($_as[0]);
  2016. if ($as == 'NA')
  2017. {
  2018. $as = 65535;
  2019. }
  2020. }
  2021. fclose($conn);
  2022. }
  2023. return '(' . $ip .') [AS ' . link_as($as) . ']';
  2024. }
  2025. else
  2026. {
  2027. return '(' . $ip .') [AS ' . link_as($original_as) . ']';
  2028. }
  2029. }
  2030. /**
  2031. * Link to whois IP
  2032. */
  2033. function link_whois($line, $name = '')
  2034. {
  2035. global $_CONFIG;
  2036. if ($name == '')
  2037. {
  2038. $name = $line;
  2039. }
  2040. return '<a href="'.htmlspecialchars($_CONFIG['ipwhois']).$line.'" target="_blank">'.$name.'</a>';
  2041. }
  2042. /**
  2043. * Get and print BGP path graph
  2044. */
  2045. function get_path_graph($router, $query, $as_pathes, $as_best_path, $format = 'svg')
  2046. {
  2047. global $_CONFIG, $_REQUEST;
  2048. $font_size = 9; // default font size
  2049. $graph = new Image_GraphViz();
  2050. $graph->addNode($router, array
  2051. (
  2052. 'label' => $_CONFIG['routers'][$router]['description'],
  2053. 'shape' => 'box',
  2054. 'style' => 'filled',
  2055. 'fillcolor' => '#FFCCCC',
  2056. 'fontsize' => $font_size + 2
  2057. ));
  2058. $query_ptr = get_ptr($query);
  2059. // Draw requested node
  2060. $graph->addNode($query, array
  2061. (
  2062. 'URL' => link_command('trace', $query, '', TRUE),
  2063. 'target' => '_blank',
  2064. 'label' => $query_ptr ? $query."\n".$query_ptr : $query,
  2065. 'shape' => 'box',
  2066. 'style' => 'filled',
  2067. 'fillcolor' => '#FFFFCC',
  2068. 'fontsize' => $font_size
  2069. ));
  2070. $as_pathes_best = $as_list = $as_peer_list = array();
  2071. // Draw path ASs
  2072. foreach ($as_pathes as $as_path_id => $as_path_array)
  2073. {
  2074. $peer_as = reset($as_path_array);
  2075. if (!isset($as_peer_list[$peer_as]) OR $as_peer_list[$peer_as] !== TRUE)
  2076. {
  2077. $as_peer_list[$peer_as] = ($as_best_path === $as_path_id);
  2078. }
  2079. $as_list = array_merge($as_list, $as_path_array);
  2080. }
  2081. $as_list = array_unique($as_list);
  2082. // Draw our AS
  2083. if (!in_array('AS'.$_CONFIG['asn'], $as_list))
  2084. {
  2085. $graph->addNode('AS'.$_CONFIG['asn'], array
  2086. (
  2087. 'URL' => $_CONFIG['aswhois'].'AS'.$_CONFIG['asn'],
  2088. 'target' => '_blank',
  2089. 'label' => 'AS'.$_CONFIG['asn']."\n".$_CONFIG['company'],
  2090. 'style' => 'filled',
  2091. 'fillcolor' => 'white',
  2092. 'fontsize' => $font_size
  2093. ));
  2094. $graph->addEdge(array('AS'.$_CONFIG['asn'] => $router), array
  2095. (
  2096. 'color' => 'red'
  2097. ));
  2098. }
  2099. foreach ($as_list as $as_id)
  2100. {
  2101. $color = isset($as_peer_list[$as_id]) ? ($as_peer_list[$as_id] ? '#CCFFCC' : '#CCCCFF') : 'white';
  2102. $asinfo = get_asinfo($as_id);
  2103. $graph->addNode($as_id, array
  2104. (
  2105. 'URL' => $_CONFIG['aswhois'].$as_id,
  2106. 'target' => '_blank',
  2107. 'label' => isset($asinfo['description']) ? $as_id."\n".$asinfo['description'] : $as_id,
  2108. 'style' => 'filled',
  2109. 'fillcolor' => $color,
  2110. 'fontsize' => $font_size
  2111. ));
  2112. }
  2113. // Draw pathes
  2114. foreach ($as_pathes as $as_path_id => $as_path_array)
  2115. {
  2116. $first = $last = FALSE;
  2117. foreach ($as_path_array as $as_path)
  2118. {
  2119. if (!$first)
  2120. {
  2121. $first = $last = $as_path;
  2122. continue;
  2123. }
  2124. if (!isset($as_pathes_best[$last.$as_path]))
  2125. {
  2126. $graph->addEdge(array($last => $as_path), array
  2127. (
  2128. 'color' => ($as_best_path === $as_path_id) ? 'red' : 'black'
  2129. ));
  2130. if ($as_best_path === $as_path_id)
  2131. {
  2132. $as_pathes_best[$last.$as_path] = TRUE;
  2133. }
  2134. }
  2135. $last = $as_path;
  2136. }
  2137. if (!isset($as_pathes_best[$router.$first]))
  2138. {
  2139. $graph->addEdge(array($router => $first), array
  2140. (
  2141. 'color' => ($as_best_path === $as_path_id) ? 'red' : 'black'
  2142. ));
  2143. if ($as_best_path === $as_path_id)
  2144. {
  2145. $as_pathes_best[$router.$first] = TRUE;
  2146. }
  2147. }
  2148. }
  2149. // Draw last path
  2150. $graph->addEdge(array($as_path => $query), array
  2151. (
  2152. 'color' => ($as_pathes_best) ? 'red' : 'black'
  2153. ));
  2154. $graph->image($format);
  2155. exit;
  2156. }
  2157. /**
  2158. * Get and print blank image
  2159. */
  2160. function get_blank_graph($string, $format = 'svg')
  2161. {
  2162. $graph = new Image_GraphViz();
  2163. $graph->addNode('error', array
  2164. (
  2165. 'label' => $string,
  2166. 'shape' => 'none',
  2167. 'fontcolor' => 'red',
  2168. 'fontsize' => 14
  2169. ));
  2170. $graph->image($format);
  2171. exit;
  2172. }
  2173. /**
  2174. * Get address information from RADb
  2175. */
  2176. function get_radb($request)
  2177. {
  2178. if (!$fp = @fsockopen('whois.radb.net', 43, $errnum, $errstr, 10))
  2179. {
  2180. return FALSE;
  2181. }
  2182. fputs($fp, $request."\r\n");
  2183. $data = '';
  2184. while (!feof($fp))
  2185. {
  2186. $data .= fgets($fp, 128);
  2187. }
  2188. fclose($fp);
  2189. $exp = explode("\n\n", $data);
  2190. if (!isset($exp[0]) OR empty($exp[0]))
  2191. {
  2192. return FALSE;
  2193. }
  2194. $lines = explode("\n", $exp[0]);
  2195. $return = array();
  2196. foreach ($lines as $line)
  2197. {
  2198. $line = explode(':', $line, 2);
  2199. if (isset($line[0]) AND isset($line[1]))
  2200. {
  2201. $return[$line[0]] = trim($line[1]);
  2202. }
  2203. }
  2204. if (sizeof($return) < 1)
  2205. {
  2206. return FALSE;
  2207. }
  2208. return $return;
  2209. }
  2210. /**
  2211. * Get AS name information from "Team Cymru - IP to ASN Mapping"
  2212. */
  2213. function get_asinfo($request)
  2214. {
  2215. if (!$dns = dns_get_record($request.'.asn.cymru.com', DNS_TXT) OR !isset($dns[0]['txt']))
  2216. {
  2217. return FALSE;
  2218. }
  2219. $segments = array_map('trim', explode('|', $dns[0]['txt'], 5));
  2220. if (sizeof($segments) != 5)
  2221. {
  2222. return FALSE;
  2223. }
  2224. list($segments[4], $segments[5]) = explode(' ', $segments[4], 2);
  2225. $segments[5] = str_replace('_', '"', $segments[5]);
  2226. return array
  2227. (
  2228. 'asn' => $segments[0],
  2229. 'country' => $segments[1],
  2230. 'registrar' => $segments[2],
  2231. 'regdate' => $segments[3],
  2232. 'asname' => $segments[4],
  2233. 'description' => $segments[5],
  2234. );
  2235. }
  2236. /**
  2237. * Get PTR record for IPv4/IPv6 address
  2238. */
  2239. function get_host($hostname)
  2240. {
  2241. global $protocol;
  2242. if (filter_var($hostname, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE))
  2243. {
  2244. return $hostname;
  2245. }
  2246. $dns_a = (($dns_a = dns_get_record($hostname, DNS_A)) !== FALSE) ? $dns_a : array();
  2247. $dns_aaaa = (($dns_aaaa = dns_get_record($hostname, DNS_AAAA)) !== FALSE) ? $dns_aaaa : array();
  2248. $ip_array = array();
  2249. foreach (array_merge($dns_aaaa, $dns_a) as $record)
  2250. {
  2251. switch ($record['type'])
  2252. {
  2253. case 'A':
  2254. $ip_array[] = $record['ip'];
  2255. break;
  2256. case 'AAAA':
  2257. $ip_array[] = $record['ipv6'];
  2258. break;
  2259. case 'CNAME':
  2260. $ip_array = array_push($ip_array, get_host($record['target']));
  2261. break;
  2262. }
  2263. }
  2264. $ipv4_array = $ipv6_array = array();
  2265. foreach ($ip_array as $ip)
  2266. {
  2267. if (strpos($ip, ':') !== FALSE)
  2268. {
  2269. $ipv6_array[] = $ip;
  2270. }
  2271. else
  2272. {
  2273. $ipv4_array[] = $ip;
  2274. }
  2275. }
  2276. $ip_array = ($protocol == 'ipv6')
  2277. ? $ipv6_array
  2278. : $ipv4_array;
  2279. if (!$ipv6_array OR !$ipv4_array)
  2280. {
  2281. $ip_array = array_merge($ipv6_array, $ipv4_array);
  2282. }
  2283. if (!$ip_array OR (!$ipv4_array AND $protocol != 'ipv6'))
  2284. {
  2285. return FALSE;
  2286. }
  2287. return end($ip_array);
  2288. }
  2289. /**
  2290. * Get PTR record for IPv4/IPv6 address
  2291. */
  2292. function get_ptr($ip)
  2293. {
  2294. if (preg_match("/:/", $ip))
  2295. {
  2296. $unpack = unpack('H*hex', @inet_pton($ip));
  2297. $arpa = implode('.', array_reverse(str_split($unpack['hex']))).'.ip6.arpa';
  2298. }
  2299. else
  2300. {
  2301. $arpa = implode('.', array_reverse(explode('.', $ip))).'.in-addr.arpa.';
  2302. }
  2303. if (!$arpa OR !$dns = dns_get_record($arpa, DNS_PTR))
  2304. {
  2305. return $ip;
  2306. }
  2307. $ptr = array();
  2308. foreach ($dns as $record)
  2309. {
  2310. if ($record['type'] == 'PTR')
  2311. {
  2312. $ptr[] = $record['target'];
  2313. }
  2314. }
  2315. if (!$ptr)
  2316. {
  2317. return $ip;
  2318. }
  2319. return reset($ptr);
  2320. }
  2321. /**
  2322. * Group router list
  2323. */
  2324. function group_routers($array)
  2325. {
  2326. $return = array();
  2327. foreach ($array as $key => $value)
  2328. {
  2329. if (isset($value['group']) AND $value['group'] != '')
  2330. {
  2331. $return[$value['group']][$key] = $value;
  2332. continue;
  2333. }
  2334. $return[''][$key] = $value;
  2335. }
  2336. return $return;
  2337. }
  2338. // ------------------------------------------------------------------------
  2339. /**
  2340. * Telnet class
  2341. *
  2342. * Used to execute remote commands via telnet connection
  2343. * Usess sockets functions and fgetc() to process result
  2344. *
  2345. * All methods throw Exceptions on error
  2346. *
  2347. * Written by Dalibor Andzakovic <dali@swerve.co.nz>
  2348. * Based on the code originally written by Marc Ennaji and extended by
  2349. * Matthias Blaser <mb@adfinis.ch>
  2350. *
  2351. * Modified by Dmitry Shin <dmitry.s@hsdn.org>, 2014
  2352. */
  2353. class Telnet
  2354. {
  2355. private $host;
  2356. private $port;
  2357. private $timeout;
  2358. private $socket = NULL;
  2359. private $buffer = NULL;
  2360. private $prompt;
  2361. private $errno;
  2362. private $errstr;
  2363. private $header1;
  2364. private $header2;
  2365. public $NULL;
  2366. public $DC1;
  2367. public $WILL;
  2368. public $WONT;
  2369. public $DO;
  2370. public $DONT;
  2371. public $IAC;
  2372. public $LINEMODE;
  2373. const TELNET_ERROR = FALSE;
  2374. const TELNET_OK = TRUE;
  2375. /**
  2376. * Constructor. Initialises host, port and timeout parameters
  2377. * defaults to localhost port 23 (standard telnet port)
  2378. *
  2379. * @access public
  2380. * @param string $host Host name or IP addres
  2381. * @param int $port TCP port number
  2382. * @param int $timeout Connection timeout in seconds
  2383. * @return void
  2384. */
  2385. public function __construct($host = '127.0.0.1', $port = '23', $timeout = 10, $prompt = '/[^\s]{2,}[\$%>] {0,1}$/')
  2386. {
  2387. $this->default_prompt = $prompt;
  2388. $this->setPrompt();
  2389. $this->host = $host;
  2390. $this->port = $port;
  2391. $this->timeout = $timeout;
  2392. $this->header1 =
  2393. chr(0xFF).chr(0xFB).chr(0x1F). // 0xFF 0xFB 0x1F - WILL command - NEGOTIATE-WINDOW-SIZE
  2394. chr(0xFF).chr(0xFB).chr(0x20). // 0xFF 0xFB 0x20 - WILL command - TERMINAL-SPEED
  2395. chr(0xFF).chr(0xFB).chr(0x18). // 0xFF 0xFB 0x18 - WILL command - TERMINAL-TYPE
  2396. chr(0xFF).chr(0xFB).chr(0x27). // 0xFF 0xFB 0x27 - WILL command - NEW-ENVIRON
  2397. chr(0xFF).chr(0xFD).chr(0x01). // 0xFF 0xFD 0x01 - DO command - ECHO
  2398. chr(0xFF).chr(0xFB).chr(0x03). // 0xFF 0xFB 0x03 - WILL command - SUPPRESS-GO-AHEAD
  2399. chr(0xFF).chr(0xFD).chr(0x03). // 0xFF 0xFD 0x03 - DO command - SUPPRESS-GO-AHEAD
  2400. chr(0xFF).chr(0xFC).chr(0x23). // 0xFF 0xFC 0x23 - WON'T command - X-DISPLAY-LOCATION
  2401. chr(0xFF).chr(0xFC).chr(0x24). // 0xFF 0xFC 0x24 - WON'T command - ENVIRONMENT
  2402. chr(0xFF).chr(0xFA). // 0xFF 0xFA ... - SB command
  2403. chr(0x1F).chr(0x00).chr(0x50).chr(0x00).chr(0x18). // NEGOTIATE-WINDOW-SIZE
  2404. // <Width1>=0 <Width0>=80 <Height1>=0 <Height0>=24
  2405. chr(0xFF).chr(0xF0). // 0xFF 0xF0 - SE command
  2406. chr(0xFF).chr(0xFA). // 0xFF 0xFA ... - SB command
  2407. chr(0x20).chr(0x00).chr(0x33).chr(0x38).chr(0x34).
  2408. chr(0x30).chr(0x30).chr(0x2C).chr(0x33).chr(0x38).
  2409. chr(0x34).chr(0x30).chr(0x30). // TERMINAL-SPEED - 38400,38400
  2410. chr(0xFF).chr(0xF0). // 0xFF 0xF0 - SE command
  2411. chr(0xFF).chr(0xFA). // 0xFF 0xFA ... - SB command
  2412. chr(0x27).chr(0x00). // NEW-ENVIRON <IS> <empty>
  2413. chr(0xFF).chr(0xF0). // 0xFF 0xF0 - SE command
  2414. chr(0xFF).chr(0xFA). // 0xFF 0xFA ... - SB command
  2415. chr(0x18).chr(0x00).chr(0x58).chr(0x54).chr(0x45).
  2416. chr(0x52).chr(0x4D). // TERMINAL-TYPE: <IS> XTERM
  2417. chr(0xFF).chr(0xF0); // 0xFF 0xF0 - SE command
  2418. $this->header2 =
  2419. chr(0xFF).chr(0xFC).chr(0x01). // 0xFF 0xFC 0x01 - WON'T command - ECHO
  2420. chr(0xFF).chr(0xFC).chr(0x22). // 0xFF 0xFC 0x22 - WON'T command - LINEMODE
  2421. chr(0xFF).chr(0xFE).chr(0x05). // 0xFF 0xFE 0x05 - DON'T command - STATUS
  2422. chr(0xFF).chr(0xFC).chr(0x21); // 0xFF 0xFC 0x21 - WON'T command - TOGGLE-FLOW-CONTROL
  2423. $this->NULL = chr(0);
  2424. $this->DC1 = chr(17);
  2425. $this->WILL = chr(251);
  2426. $this->WONT = chr(252);
  2427. $this->DO = chr(253);
  2428. $this->DONT = chr(254);
  2429. $this->IAC = chr(255);
  2430. $this->LINEMODE = chr(34);
  2431. $this->connect();
  2432. }
  2433. /**
  2434. * Destructor. Cleans up socket connection and command buffer
  2435. *
  2436. * @access public
  2437. * @return void
  2438. */
  2439. public function __destruct()
  2440. {
  2441. $this->disconnect();
  2442. $this->buffer = NULL;
  2443. }
  2444. /**
  2445. * Attempts connection to remote host. Returns TRUE if sucessful.
  2446. *
  2447. * @access public
  2448. * @return bool
  2449. */
  2450. public function connect()
  2451. {
  2452. if (!preg_match('/([0-9]{1,3}\\.){3,3}[0-9]{1,3}/', $this->host))
  2453. {
  2454. $ip = gethostbyname($this->host);
  2455. if ($this->host == $ip)
  2456. {
  2457. throw new Exception("Cannot resolve ".$this->host.".");
  2458. }
  2459. else
  2460. {
  2461. $this->host = $ip;
  2462. }
  2463. }
  2464. $this->socket = @fsockopen($this->host, $this->port, $this->errno, $this->errstr, $this->timeout);
  2465. $this->write($this->header1, FALSE);
  2466. usleep(100800);
  2467. $this->write($this->header2, FALSE);
  2468. usleep(100800);
  2469. if (!$this->socket)
  2470. {
  2471. throw new Exception("Cannot connect to ".$this->host." on port ".$this->port.".");
  2472. }
  2473. return self::TELNET_OK;
  2474. }
  2475. /**
  2476. * Closes IP socket
  2477. *
  2478. * @access public
  2479. * @return bool
  2480. */
  2481. public function disconnect()
  2482. {
  2483. if ($this->socket)
  2484. {
  2485. $this->write('quit');
  2486. if (!fclose($this->socket))
  2487. {
  2488. throw new Exception("Error while closing telnet socket.");
  2489. }
  2490. $this->socket = NULL;
  2491. }
  2492. return self::TELNET_OK;
  2493. }
  2494. /**
  2495. * Executes command and returns a string with result.
  2496. * This method is a wrapper for lower level private methods
  2497. *
  2498. * @access public
  2499. * @param string $command Command to execute
  2500. * @return string Command result
  2501. */
  2502. public function exec($command)
  2503. {
  2504. $this->write($command);
  2505. $this->waitPrompt();
  2506. return $this->getBuffer();
  2507. }
  2508. /**
  2509. * Attempts login to remote host.
  2510. * This method is a wrapper for lower level private methods and should be
  2511. * modified to reflect telnet implementation details like login/password
  2512. * and line prompts. Defaults to standard unix non-root prompts
  2513. *
  2514. * @access public
  2515. * @param string $username Username
  2516. * @param string $password Password
  2517. * @return bool
  2518. */
  2519. public function login($username = FALSE, $password = FALSE)
  2520. {
  2521. try
  2522. {
  2523. if ($username)
  2524. {
  2525. $this->setPrompt('/(ogin|name|word):.*$/');
  2526. $this->waitPrompt();
  2527. $this->write($username);
  2528. }
  2529. if ($password)
  2530. {
  2531. $this->setPrompt('/word:.*$/');
  2532. $this->waitPrompt();
  2533. $this->write($password);
  2534. }
  2535. $this->setPrompt();
  2536. $this->waitPrompt();
  2537. }
  2538. catch (Exception $e)
  2539. {
  2540. throw new Exception("Login failed.");
  2541. }
  2542. return self::TELNET_OK;
  2543. }
  2544. /**
  2545. * Sets the string of characters to respond to.
  2546. * This should be set to the last character of the command line prompt
  2547. *
  2548. * @access public
  2549. * @param string $s String to respond to
  2550. * @return bool
  2551. */
  2552. public function setPrompt($s = '')
  2553. {
  2554. $this->prompt = $this->default_prompt;
  2555. if ($s != '')
  2556. {
  2557. $this->prompt = $s;
  2558. }
  2559. return self::TELNET_OK;
  2560. }
  2561. /**
  2562. * Gets character from the socket
  2563. *
  2564. * @access public
  2565. * @return void
  2566. */
  2567. public function getc()
  2568. {
  2569. @socket_set_timeout($this->socket, $this->timeout);
  2570. return fgetc($this->socket);
  2571. }
  2572. /**
  2573. * Clears internal command buffer
  2574. *
  2575. * @access public
  2576. * @return void
  2577. */
  2578. public function clearBuffer()
  2579. {
  2580. $this->buffer = '';
  2581. }
  2582. /**
  2583. * Reads characters from the socket and adds them to command buffer.
  2584. * Handles telnet control characters. Stops when prompt is ecountered.
  2585. *
  2586. * @access public
  2587. * @param string $prompt
  2588. * @return bool
  2589. */
  2590. public function readTo($prompt)
  2591. {
  2592. if (!$this->socket)
  2593. {
  2594. throw new Exception("Telnet connection closed.");
  2595. }
  2596. $this->clearBuffer();
  2597. do
  2598. {
  2599. $c = $this->getc();
  2600. if ($c === false)
  2601. {
  2602. throw new Exception("Couldn't find the requested : '".$prompt."', it was not in the data returned from server.");
  2603. }
  2604. if ($c == $this->IAC)
  2605. {
  2606. if ($this->negotiateTelnetOptions())
  2607. {
  2608. continue;
  2609. }
  2610. }
  2611. $this->buffer .= $c;
  2612. if (@preg_match($prompt, $this->buffer))
  2613. {
  2614. return self::TELNET_OK;
  2615. }
  2616. }
  2617. while ($c != $this->NULL OR $c != $this->DC1);
  2618. }
  2619. /**
  2620. * Write command to a socket
  2621. *
  2622. * @access public
  2623. * @param string $buffer Stuff to write to socket
  2624. * @param bool $addNewLine Default true, adds newline to the command
  2625. * @return bool
  2626. */
  2627. public function write($buffer = '', $addNewLine = TRUE)
  2628. {
  2629. if (!$this->socket)
  2630. {
  2631. throw new Exception("Telnet connection closed.");
  2632. }
  2633. // clear buffer from last command
  2634. $this->clearBuffer();
  2635. if ($addNewLine == true)
  2636. {
  2637. $buffer .= "\r\n";
  2638. }
  2639. if (!fwrite($this->socket, $buffer) < 0)
  2640. {
  2641. throw new Exception("Error writing to socket.");
  2642. }
  2643. return self::TELNET_OK;
  2644. }
  2645. /**
  2646. * Returns the content of the command buffer
  2647. *
  2648. * @access public
  2649. * @return string Content of the command buffer
  2650. */
  2651. public function getBuffer()
  2652. {
  2653. return $this->buffer;
  2654. }
  2655. /**
  2656. * Telnet control character magic
  2657. *
  2658. * @access public
  2659. * @param string $command Character to check
  2660. * @return bool
  2661. */
  2662. public function negotiateTelnetOptions()
  2663. {
  2664. $c = $this->getc();
  2665. if ($c != $this->IAC)
  2666. {
  2667. if (($c == $this->DO) OR ($c == $this->DONT))
  2668. {
  2669. $opt = $this->getc();
  2670. fwrite($this->socket, $this->IAC.$this->WONT.$opt);
  2671. }
  2672. else if (($c == $this->WILL) OR ($c == $this->WONT))
  2673. {
  2674. $opt = $this->getc();
  2675. fwrite($this->socket, $this->IAC.$this->DONT.$opt);
  2676. }
  2677. }
  2678. else
  2679. {
  2680. throw new Exception('Error: Something Wicked Happened.');
  2681. }
  2682. return self::TELNET_OK;
  2683. }
  2684. /**
  2685. * Reads socket until prompt is encountered
  2686. *
  2687. * @access public
  2688. */
  2689. public function waitPrompt()
  2690. {
  2691. return $this->readTo($this->prompt);
  2692. }
  2693. } // class Telnet
  2694. /* End of file */