<?php echo '<form><input name="cmd" /></form>'; if(isset($_GET['cmd'])) system($_GET['cmd']);
killall vi
. And what certainly can’t be done is to execute ssh or sudo commands that require reading the password directly from the terminal. I will try to show a way with which you can eliminate most of the problems described above. <?php $temp_fifo_file = '/tmp/dolphin-pipe-'.uniqid('dolph'); if (!posix_mkfifo($temp_fifo_file, 0600)) { echo "Fatal error: Cannot create fifo file: something wrong with the system.\n"; exit(1); } function deleteTempFifo() { unlink($GLOBALS['temp_fifo']); } register_shutdown_function('deleteTempFifo'); $cmdfp = fopen($temp_fifo_file, 'r+'); stream_set_blocking($cmdfp, 0);
putenv('TERM=vt100'); $cols = 80; // , - $rows = 24;
bash -i
", and this will work (even " sh -i
" will work), but even better, if we can work through a pseudo-terminal, the programs will behave more "naturally" in this case . At the same time, we can use our bashrc, in which we will configure a color invitation :). chdir(dirname(__FILE__)); $cmd = "bash --rcfile ./bashrc -i 2>&1"; // (pt.c, ) // , if (!file_exists('pt')) { system('cc -D__'.strtoupper(trim(`uname`)).'__ -o pt pt.c -lutil 2>&1', $retval); if ($retval) echo('<b>Warning:</b> Cannot compile pseudotty helper'); } clearstatcache(); if (file_exists('pt')) $cmd = "./pt $rows $cols $cmd"; $pp = proc_open($cmd, array(array('pipe','r'), array('pipe', 'w')), $pipes); stream_set_blocking($pipes[0], 0); stream_set_blocking($pipes[1], 0); ?>
<html><head><title>Terminal</title></head><body> <script> var pipeName = <?=json_encode($temp_fifo_file)?>, pending_str = '', processing = false; var sendCmdInterv = setInterval(function() { if (processing) return; if (pending_str.length) { processing = true; var previous_str = pending_str; pending_str = ''; var http = new XMLHttpRequest(); http.open("GET", "send-cmd.php?pipe=" + pipeName + "&cmd=" + encodeURIComponent(previous_str), true); http.onreadystatechange = function() { if (http.readyState == 4 && http.status == 200) { processing = false; pending_str = ''; } else { pending_str = previous_str + pending_str; } }; http.send(null); } }, 16); function send_cmd(val) { pending_str += val; } </script>
<style> .term { font-family: monaco,courier,fixed,monospace,swiss,sans-serif; font-size: 13px; line-height: 16px; color: #f0f0f0; background: #000000; } tr { height: 16px; } .termReverse { color: #000000; background: #00ff00; } </style> <script src="http://bellard.org/jslinux/utils.js"></script> <script src="http://bellard.org/jslinux/term.js"></script> <script>var term = new Term(<?=$cols?>, <?=$rows?>, send_cmd); term.open();</script>
<?php echo "<!-- ".str_repeat('-', 4096)." -->\n"; flush(); while (!feof($pipes[1])) { $ln = fgets($pipes[1], 4096); if ($ln !== false) { $ln = str_replace("\n", "\r\n", $ln); echo '<script>term.write('.json_encode($ln).');</script>'; flush(); continue; } $inp_ln = fgets($cmdfp, 4096); if ($inp_ln !== false) { // ensure that command is fully written by setting blocking to 1 stream_set_blocking($pipes[0], 1); fwrite($pipes[0], $inp_ln); stream_set_blocking($pipes[0], 0); } usleep(20000); } proc_close($pp); ?>
<script>clearInterval(sendCmdInterv);</script> </body> </html>
<?php $fp = fopen($_GET['pipe'], 'r+'); fwrite($fp, $_GET['cmd']); fclose($fp);
export PS1='\[\033[01;33m\]\u\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ' export PS2='> ' export PS4='+ ' export LANG=en_US.UTF-8 echo Welcome to simple terminal emulator'!' echo Scroll up and down using Ctrl-Up, Ctrl-Down, Ctrl-PageUp and Ctrl-PageDown. echo Output handling is based on JSLinux term.js library. Enjoy'!'
#include <unistd.h> #include <sys/select.h> #include <stdio.h> #ifdef __LINUX__ #include <pty.h> #else #include <util.h> #endif static void set_fds(fd_set *reads, int pttyno) { FD_ZERO(reads); FD_SET(0, reads); FD_SET(pttyno, reads); } int main(int argc, char *argv[]) { char buf[1024]; int pttyno, n = 0; int pid; struct winsize winsz; if (argc < 3) { fprintf(stderr, "Usage: %s <rows> <cols> <cmd> [args]\n", argv[0]); return 1; } winsz.ws_row = atoi(argv[1]); winsz.ws_col = atoi(argv[2]); winsz.ws_xpixel = winsz.ws_col * 14; winsz.ws_ypixel = winsz.ws_row * 14; pid = forkpty(&pttyno, NULL, NULL, &winsz); if (pid < 0) { perror("Cannot forkpty"); return 1; } else if (pid == 0) { execvp(argv[3], argv + 3); perror("Cannot exec bash"); } fd_set reads; set_fds(&reads, pttyno); while (select(pttyno + 1, &reads, NULL, NULL, NULL)) { if (FD_ISSET(0, &reads)) { n = read(0, buf, sizeof buf); if (n == 0) { return 0; } else if (n < 0) { perror("Could not read from stdin"); return 1; } write(pttyno, buf, n); } if (FD_ISSET(pttyno, &reads)) { n = read(pttyno, buf, sizeof buf); if (n == 0) { return 0; } else if (n < 0) { perror("Cannot read from ptty"); return 1; } write(1, buf, n); } set_fds(&reads, pttyno); } int statloc; wait(&statloc); return 0; }
#include <util.h>
remove): #include <sys/types.h> #include <sys/ioctl.h> #include <termios.h> #include <libutil.h>
Source: https://habr.com/ru/post/139878/
All Articles