Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- </head>
- <body>
- <div id="gatewayInput">
- <form method="post">
- <input type="text" id="gateway" name="gateway" placeholder="Gateway Name"><br><br>
- <?php
- include("search.php");
- ?>
- </div>
- <div class="box1">
- <label class="col">Up/Down</label>
- <span class="col">
- <input type="radio" name="option" id="r1" value="1" />
- <label for="r1">Up</label>
- <input type="radio" name="option" id="r2" value="2" />
- <label for="r2">Down</label>
- </span>
- <span class="col">
- <input type="submit" class="button" name="submit"/>
- </span>
- </form>
- </div>
- <script src ="../../../jqueryDir/jquery-3.2.1.min.js"></script>
- <script src ="../../../jqueryDir/jquery-ui.min.js"></script>
- <script type="text/javascript">
- //auto seatch function
- $(function() {
- $( "#gateway" ).autocomplete({
- source: 'search.php'
- });
- });
- //button click function
- $(".button").click(function(event){
- if ((document.getElementsByName("gateway")[0].value == '')) {
- alert('Gateway Required!');
- return false;
- }
- else if (document.querySelectorAll('input[type="radio"]:checked').length < 1) {
- alert('Please Choose Up/Down Value!');
- return false;
- }
- else {
- //alert('Sucess!');
- event.preventDefault();
- var checkInterval = 1; // check interval, in seconds
- var fileToCheck = "file.txt";
- var lastData;
- function checkFile() {
- $.get(fileToCheck, function (data) {
- // Update the text if it has changed
- if (lastData !== data) {
- $( "#target" ).val( data );
- $( "#target" ).animate({
- scrollTop: $( "#target" )[0].scrollHeight - $( "#target" ).height()
- }, 'slow');
- lastData = data;
- }
- });
- }
- $.ajax({
- url:"testexe.php",
- type: "POST",
- data: {
- gateway: $("#gateway").val(),
- option: $('input[type=radio]:checked').val()
- },
- dataType: "text",
- success:function(result){
- $('#div1').html(result);
- clearInterval(interval);
- console.log("done!");
- }
- });
- var interval = null;
- $(document).ready(function () {
- interval = setInterval(checkFile, 1000 * checkInterval);
- });
- return true;
- }
- });
- </script>
- <br><br>
- <div id="div0">
- <textarea id="target" cols="80" rows="20">Loading...</textarea>
- </div>
- <div id="div1">
- </div>
- </body>
- </html>
- <?php
- header("Content-type: text/plain");
- $user = 'user';
- $password = 'pwd';
- $airPath = '/home/user/up.sh';
- $groundPath = '/home/user/down.sh';
- //when the submit button is clicked
- if ($_SERVER['REQUEST_METHOD'] == 'POST'){
- //get the gateway value from page
- $gateway = $_POST['gateway'];
- //create the ssh connection
- if ($connection = @ssh2_connect($gateway, 22)) {
- ssh2_auth_password($connection, $user, $password);
- $path = $_SERVER['DOCUMENT_ROOT'] . "/mypath/file.txt";
- if(isset($_POST['option']) && $_POST['option'] == 1) {
- $stream = ssh2_exec($connection, $airPath);
- stream_set_blocking($stream, true);
- $stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
- $stream_err = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
- $fopenText = fopen($path, "w");
- while(($line = fgets($stream_out)) !== false) {
- fwrite($fopenText, $line);
- fflush($fopenText);
- }
- echo '<pre>' . "------------------------n" . '</pre>';
- while($line = fgets($stream_err)) {flush(); echo '<pre>' . $line . '</pre>';}
- fclose($stream);
- fclose($fopenText);
- }
- if(isset($_POST['option']) && $_POST['option'] == 2) {
- $stream = ssh2_exec($connection, $groundPath);
- stream_set_blocking($stream, true);
- $stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
- $stream_err = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
- //open the file and set to "write"
- $fopenText = fopen($path, "w");
- //while the stream is still valid write stream to file
- while(($line = fgets($stream_out)) !== false) {
- fwrite($fopenText, $line);
- fflush($fopenText);
- }
- echo '<pre>' . "------------------------n" . '</pre>';
- while($line = fgets($stream_err)) {flush(); echo '<pre>' . $line . '</pre>';}
- fclose($stream);
- fclose($fopenText);
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement