Advertisement
Guest User

Untitled

a guest
Sep 6th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. </head>
  2. <body>
  3. <div id="gatewayInput">
  4. <form method="post">
  5. <input type="text" id="gateway" name="gateway" placeholder="Gateway Name"><br><br>
  6. <?php
  7. include("search.php");
  8. ?>
  9. </div>
  10. <div class="box1">
  11. <label class="col">Up/Down</label>
  12. <span class="col">
  13. <input type="radio" name="option" id="r1" value="1" />
  14. <label for="r1">Up</label>
  15. <input type="radio" name="option" id="r2" value="2" />
  16. <label for="r2">Down</label>
  17. </span>
  18. <span class="col">
  19. <input type="submit" class="button" name="submit"/>
  20. </span>
  21. </form>
  22. </div>
  23. <script src ="../../../jqueryDir/jquery-3.2.1.min.js"></script>
  24. <script src ="../../../jqueryDir/jquery-ui.min.js"></script>
  25. <script type="text/javascript">
  26.  
  27. //auto seatch function
  28. $(function() {
  29. $( "#gateway" ).autocomplete({
  30. source: 'search.php'
  31. });
  32. });
  33.  
  34. //button click function
  35. $(".button").click(function(event){
  36. if ((document.getElementsByName("gateway")[0].value == '')) {
  37. alert('Gateway Required!');
  38. return false;
  39. }
  40. else if (document.querySelectorAll('input[type="radio"]:checked').length < 1) {
  41. alert('Please Choose Up/Down Value!');
  42. return false;
  43. }
  44. else {
  45. //alert('Sucess!');
  46. event.preventDefault();
  47.  
  48. var checkInterval = 1; // check interval, in seconds
  49. var fileToCheck = "file.txt";
  50. var lastData;
  51. function checkFile() {
  52. $.get(fileToCheck, function (data) {
  53. // Update the text if it has changed
  54. if (lastData !== data) {
  55. $( "#target" ).val( data );
  56. $( "#target" ).animate({
  57. scrollTop: $( "#target" )[0].scrollHeight - $( "#target" ).height()
  58. }, 'slow');
  59. lastData = data;
  60. }
  61. });
  62. }
  63.  
  64. $.ajax({
  65. url:"testexe.php",
  66. type: "POST",
  67. data: {
  68. gateway: $("#gateway").val(),
  69. option: $('input[type=radio]:checked').val()
  70. },
  71. dataType: "text",
  72. success:function(result){
  73. $('#div1').html(result);
  74. clearInterval(interval);
  75. console.log("done!");
  76. }
  77. });
  78.  
  79. var interval = null;
  80. $(document).ready(function () {
  81. interval = setInterval(checkFile, 1000 * checkInterval);
  82. });
  83.  
  84. return true;
  85. }
  86. });
  87.  
  88. </script>
  89. <br><br>
  90. <div id="div0">
  91. <textarea id="target" cols="80" rows="20">Loading...</textarea>
  92. </div>
  93. <div id="div1">
  94. </div>
  95. </body>
  96. </html>
  97.  
  98. <?php
  99. header("Content-type: text/plain");
  100.  
  101. $user = 'user';
  102. $password = 'pwd';
  103. $airPath = '/home/user/up.sh';
  104. $groundPath = '/home/user/down.sh';
  105.  
  106. //when the submit button is clicked
  107. if ($_SERVER['REQUEST_METHOD'] == 'POST'){
  108.  
  109. //get the gateway value from page
  110. $gateway = $_POST['gateway'];
  111.  
  112. //create the ssh connection
  113. if ($connection = @ssh2_connect($gateway, 22)) {
  114. ssh2_auth_password($connection, $user, $password);
  115. $path = $_SERVER['DOCUMENT_ROOT'] . "/mypath/file.txt";
  116. if(isset($_POST['option']) && $_POST['option'] == 1) {
  117.  
  118. $stream = ssh2_exec($connection, $airPath);
  119. stream_set_blocking($stream, true);
  120. $stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
  121. $stream_err = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
  122.  
  123. $fopenText = fopen($path, "w");
  124. while(($line = fgets($stream_out)) !== false) {
  125. fwrite($fopenText, $line);
  126. fflush($fopenText);
  127. }
  128. echo '<pre>' . "------------------------n" . '</pre>';
  129. while($line = fgets($stream_err)) {flush(); echo '<pre>' . $line . '</pre>';}
  130. fclose($stream);
  131. fclose($fopenText);
  132.  
  133. }
  134.  
  135. if(isset($_POST['option']) && $_POST['option'] == 2) {
  136. $stream = ssh2_exec($connection, $groundPath);
  137. stream_set_blocking($stream, true);
  138. $stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
  139. $stream_err = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
  140.  
  141. //open the file and set to "write"
  142. $fopenText = fopen($path, "w");
  143.  
  144. //while the stream is still valid write stream to file
  145. while(($line = fgets($stream_out)) !== false) {
  146. fwrite($fopenText, $line);
  147. fflush($fopenText);
  148. }
  149. echo '<pre>' . "------------------------n" . '</pre>';
  150. while($line = fgets($stream_err)) {flush(); echo '<pre>' . $line . '</pre>';}
  151. fclose($stream);
  152. fclose($fopenText);
  153. }
  154. }
  155. }
  156. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement