I'm trying to code a live console/terminal. For that I am using phpseclib library, at first I include all stuff & login:
use phpseclib\Net\SSH2;
include('vendor/autoload.php');
$ssh = new SSH2('127.0.0.1');
if(!$ssh->login('user', 'pass')) {
exit('Login Failed');
}
$ssh->setTimeout(1);
after that I check for an Ajax request (read for display, write for execute):
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
if($_POST['request'] == 'read') {
echo $ssh->read();
} elseif($_POST['request'] == 'write') {
$ssh->write($_POST['command']."\n");
echo $ssh->read();
}
exit();
}
HTML markup:
<textarea readonly>...</textarea>
<div>
<input type="text" placeholder="Command">
<button>
Do it
</button>
</div>
Some jQuery:
$('div button').click(function(e) {
$('div button').text('Loading...');
e.preventDefault();
$.ajax({
type: 'POST',
url: 'index.php',
data: {
request: 'write',
command: $('div input').val()
},
success: function(data) {
$('textarea').append(data);
$('textarea').scrollTop($('textarea')[0].scrollHeight - $('textarea').height());
$('div input').val('');
$('div button').text('Do it');
}
});
});
The Problem(s) every time I request cmd with write() it will show the Debian login message (and if I change the current directory using cd and after that I use ls it resets - because the new login I think), also im not sure how to create an interval so I get the current output continually - e.x. if I ping a website. Tried smth. like this:
setInterval(function() {
$.ajax({
type: 'POST',
url: 'index.php',
data: {
request: 'read'
},
success: function(data) {
$('textarea').append(data);
$('textarea').scrollTop($('textarea')[0].scrollHeight - $('textarea').height());
}
});
}, 300);
Im thankful for any help!