javascript - How to output script file from remote sever on my php page using ajax -
i working on trying run script remote server , output html page using ajax user can see. have tried on local server up.sh , down.sh , works. however, when put 2 scripts on remote server, start ssh session , try output remote server, not show anything, nor give me errors. missing anything? can see hard coded '$gateway' remoteserver, later value textbox field in form.php user input. best way this?
form.php:
<div id="gatewayinput"> <form> <input type="text" id="gateway" name="gateway" placeholder="gateway name"><br><br> </form> </div> <div class="box1"> <form method="post"> <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"/> </span> </form> </div> <script src ="../../../jquerydir/jquery-3.2.1.min.js"></script> <script type="text/javascript"> $(".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) { //console.log("radio button length value: " + document.queryselectorall('input[type="radio"]:checked').length); alert('please choose up/down value!'); return false; } else { alert('sucess!'); event.preventdefault(); $.ajax({ url:"testexe.php", type: "post", data: {option: $('input[type=radio]:checked').val()}, datatype: "text", success:function(result){ $('#div1').html(result) } }); return true; } });
testexe.php
<?php include 'res/php/functions.php'; $gateway = 'remoteserver'; $user = 'user'; $pass = 'pass'; function cleaninput($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } //when submit button clicked if ( $_server['request_method'] == 'post'){ //$gateway = cleaninput($_post['gateway']); //create ssh connection if ($connection = @ssh2_connect($gateway, 22)) { ssh2_auth_password($connection, $user, $pass); if(isset($_post['option']) && $_post['option'] == 1) { $output = shell_exec("/tmp/testscripts/up.sh"); } if(isset($_post['option']) && $_post['option'] == 2) { $output = shell_exec("/tmp/testscripts/down.sh"); } //remove if want see $output in file echo "<pre>$output</pre>"; } } ?>
i fixed following:
if ( $_server['request_method'] == 'post'){ //$gateway = cleaninput($_post['gateway']); //create ssh connection if ($connection = @ssh2_connect($gateway, 22)) { ssh2_auth_password($connection, $gwuser, $gwpwd); //mkdir(/tmp/moslehpour/testscripts, 0777, true); //ssh2_scp_recv($connection, './aust/conf/hosts', $workdir . '/hosts') if(isset($_post['option']) && $_post['option'] == 1) { $stream = ssh2_exec($connection, "/tmp/user/testscripts/up.sh"); stream_set_blocking($stream, true); $stream_out = ssh2_fetch_stream($stream, ssh2_stream_stdio); echo stream_get_contents($stream_out); } if(isset($_post['option']) && $_post['option'] == 2) { $stream = ssh2_exec($connection, "/tmp/user/testscripts/down.sh"); stream_set_blocking($stream, true); $stream_out = ssh2_fetch_stream($stream, ssh2_stream_stdio); echo stream_get_contents($stream_out); } } }
Comments
Post a Comment