Ver Mensaje Individual
  #13 (permalink)  
Antiguo 28/01/2009, 15:40
Avatar de ZiTAL
ZiTAL
 
Fecha de Ingreso: marzo-2004
Ubicación: Bermio (Bizkaia)
Mensajes: 1.545
Antigüedad: 20 años, 2 meses
Puntos: 62
De acuerdo Respuesta: Una duda con fgets

por si a alguien le interesa he creado un script que simula una consola:
Código PHP:
<?php
$host 
'localhost';
$port 22;
$user 'user';
$password 'password';
$conn = @ssh2_connect($host$port);
if(
$conn)
{
    if(@
ssh2_auth_password($conn$user$password))
    {
        if(
$_POST)
        {
            
$command $_POST['command'];    
            
$stream ssh2_exec($conn$command);
            
stream_set_blocking($streamtrue);
            while(
$o fgets($stream))
            {
                echo 
$o."\n";
            }
            exit();
        }
    }    
}
else
{
    echo 
"Conecction failed";
    exit();
}
?>
Código HTML:
<html>
	<head>
		<script type="text/javascript" src="zajax.js"></script>
		<script type="text/javascript">
			function send()
			{
				
				var a = new zajax();
				a.query = "command="+document.getElementById('command').value;
				a.request();
				a.reqComplete = function(t)
				{
					var p = document.getElementById('shell');
					var pre = document.createElement('pre');
					pre.appendChild(document.createTextNode(t));
					p.appendChild(pre);
				};
			}
		</script>
	</head>
	<body>
		<p>
			<label>Command: </label><input id="command" type="text" /><input type="button" value="send" onclick="send()" />
		</p>
		<p id="shell">
		</p>
	</body>
</html> 
zajax.js
Código javascript:
Ver original
  1. /**
  2.  * @author zital
  3.  *
  4.  * zajax: ajax library
  5.  * license: GPL 3.0
  6.  * version: 1.0.1
  7.  */
  8.  
  9. var zajax = function()
  10. {
  11.     this.page; // page to request the petition, default: the same page (window.location)
  12.     this.method; // ajax method: post or get, post by default
  13.     this.charset; // charset utf-8 by default
  14.     this.response; // response method, XML or Text, Text by default
  15.     this.query; // GET or POST query separated by & blank for default
  16.     this.async; // ajax connection method, true by default (firefox needs true to work)
  17.     this.getSeparator; // pagename and parameters separator ? by default
  18.  
  19.     // request public method
  20.     zajax.prototype.request = function()
  21.     {
  22.         var a = new xmlhttp(); // get xmlhttp object
  23.         if(a) // if browser support ajax
  24.         {
  25.             setDefault.call(this); // set default properties
  26.  
  27.             var b = headers(this.method, this.charset); // set get/post different headers
  28.             petitionConstruct.call(this); // construct get/post different properties
  29.  
  30.             this.beforeReq(); // Method to do before all process
  31.  
  32.             a.open(this.method,this.page,this.async); // open ajax petition
  33.             a.setRequestHeader('Content-Type', b); // set headers ALWAYS after OPEN
  34.             a.send(this.query); // send get/post query
  35.             state(this, a); // ajax reponse state
  36.         }
  37.     };
  38.  
  39.     // public methods to redefine: w3schools.com
  40.     zajax.prototype.beforeReq = function(){}; // method to do before all process
  41.     zajax.prototype.reqSetUp = function(){};     // method to do when The request has been set up
  42.     zajax.prototype.reqSend = function(){}; // method to do when The request has been sent
  43.     zajax.prototype.reqInProcess = function(){}; // method to do when The request is in process
  44.     zajax.prototype.reqComplete = function(t){}; // method to do when The request is complete
  45.     zajax.prototype.reqError = function(){};    // method to do when The request return error
  46.  
  47.     // private method to set default properties
  48.     var setDefault = function()
  49.     {
  50.         if(!this.method || this.method.toLowerCase()!='get') this.method='post';
  51.         if(!this.page) this.page=window.location;
  52.         if(this.async!=false) this.async=true;
  53.         if(!this.charset) this.charset='utf-8';
  54.         if(!this.response || this.response.toLowerCase()!='xml') this.response='txt';
  55.         if(!this.query) this.query='';
  56.         if(typeof(this.getSeparator)=='undefined') this.getSeparator='?';
  57.     };
  58.  
  59.     // private method to set get/post headers
  60.     var headers = function(m, c)
  61.     {
  62.         var a = '';
  63.         if(m.toLowerCase()=='post')
  64.             a = a + "application/x-www-form-urlencoded;"; // post Header
  65.         a = a + "charset="+c;
  66.         return a;
  67.     };
  68.  
  69.     // private method set get/post petition properties
  70.     var petitionConstruct = function()
  71.     {
  72.         // DEFAULT POST example page = index.php and query = a=hello, php -> $_POST['hello']
  73.         if(this.method!='post')// GET example page = index.php?a=hello and query = '', php -> $_GET['hello']
  74.         {
  75.             this.page = this.page+this.getSeparator+this.query;
  76.             this.query = '';
  77.         }
  78.     };
  79.    
  80.     // private method to set ajax petition state
  81.     var state = function(t, a)
  82.     {
  83.         if (t.async)
  84.             a.onreadystatechange = function() // get petition changestates
  85.             {
  86.                 switch(a.readyState)
  87.                 {
  88.                     case 1: // if readystate is 1 The request has been set up
  89.                         t.reqSetUp();
  90.                         break;
  91.                     case 2: // if readystate is 2 The request has been sent
  92.                         t.reqSend();
  93.                         break;
  94.                     case 3: // if readystate is 3 The request is in process
  95.                         t.reqInProcess();
  96.                         break;
  97.                     case 4: // if readystate is 4 the request is complete
  98.                         reqResponse.call(t, a);
  99.                         break;
  100.                 }
  101.             };
  102.         else
  103.             reqResponse.call(t, a);
  104.     };
  105.    
  106.     // private method to get ajax petition response
  107.     var reqResponse = function(a)
  108.     {
  109.         if(a.status==200) // if status is 200 petition OK      
  110.             if (this.response == 'txt') // if get plain text
  111.                 this.reqComplete(a.responseText); // execute complete method
  112.             else // else get XML
  113.                 this.reqComplete(a.responseXML); // execute complete method    
  114.         else
  115.             this.reqError(); // if error occurred execute error method
  116.     };
  117.    
  118.     // private method get xmlhttp object
  119.     var xmlhttp = function()
  120.     {
  121.         var a;try{a = new XMLHttpRequest();}
  122.         catch(e){try{a = new ActiveXObject('Msxml2.XMLHTTP');}
  123.         catch(e){try{a = new ActiveXObject('Microsoft.XMLHTTP');}
  124.         catch(e){alert('Your browser doesn\'t support ajax');a=false;}
  125.         }}return a;
  126.     };
  127. };
__________________
http://zital.no-ip.org
____________________

Euskerie ahuen eta bijotzan