Ver Mensaje Individual
  #3 (permalink)  
Antiguo 08/07/2010, 02:22
flipe_
 
Fecha de Ingreso: julio-2010
Mensajes: 3
Antigüedad: 14 años, 5 meses
Puntos: 0
Respuesta: Problema con telnet vía SSH

Estes es el programa principal:

<?php

require_once ('ssh_in_php.php');

$host = "10.236.253.131";
$port = 22;
$user = "usuario";
$password = "password";

try {
$ssh = new SSH_in_PHP($host,$port);
$ssh->connect($user,$password);
$cycle = true;
while ($cycle) {
$data = $ssh->read();
echo $data;
if (ereg('\$',$data)) {
$cycle = false;
}
}

//YA ESTOY CONECTADO AL SSH
//A PARTIR DE AQUI EMPIEZA EL TELNET


echo ("<br>");
$ssh->write("telnet 10.200.47.2\n");
$cycle = true;
while ($cycle) {
$data = $ssh->read();
echo $data;
if (eregi('USERNAME',$data)) {
$cycle = false;
}
}
echo "<br>";
//Aquí le mando el usuario y es donde me da el problema porque no devuelve nada y no me da opción a meter la password, es como si no le llegase el comando, igual no detecta el retorno de carro...
$ssh->write("SYSTEM\n");
$cycle = true;
while ($cycle) {

$data = $ssh->read();

echo $data;

if (eregi('PASSWORD',$data)) {
$cycle = false;
}
}


$ssh->disconnect();
} catch (SSHException $e) {
echo "An Exception Occured: {$e->getMessage()} ({$e->getCode()})\n";
echo "Trace: \n";
echo print_r($e->getTrace());
echo "\n";
}
?>


Y esta es la función para conectar al ssh:

public function connect ($login,$passwd) {
/**
* open connection to host
*/
$this->fd = fsockopen($this->host,$this->port,$errno,$errstr);
if (!$this->fd) {
/**
* if connection was unsuccesfull, throw an exception
*/
throw new SSHException($errstr,$errno);
}

/**
* remember login and password
*/
$this->login = $login;
$this->passwd = $passwd;

/**
* create our packet forge
*/
$this->packet_forge = new SSH_packet_forge($this->fd);
$this->packet_disolver = new SSH_packet_disolver($this->fd);

/**
* get info about ssh on the other side
*/
$this->connect_get_peer_info();
$this->connect_send_our_info();
// $this->negotiate_algo();
// $this->kex();
$this->ex_pub_key();
try {
$this->login();
} catch (Exception $e) {
$this->disconnect();
throw new SSHException('Unable to authenticate - disconnecting!');
}
try {
$this->req_pty_and_shell();
} catch (Exception $e) {
$this->disconnect();
throw new SSHException($e->getMessage());
}
/**
* set nonblocking mode for unlimited reading
*/
$this->set_non_blocking();
}


Esta para mandar comandos:

public function write($data) {
if (!feof($this->fd)) {
if ($this->logged) {
$content = chr(SSH_CMSG_STDIN_DATA);
$content.=integer2dword(strlen($data));
$content.=$data;
$this->packet_forge->fill_data($content);
$this->packet_forge->build_packet();
} else {
throw new SSHException('You need to login first before reading and writing data');
}
} else {
throw new SSHException('Disconnected!');
}
}


Y esta para leerlos:

public function read($max_len = null) {
if (!feof($this->fd)) {
if ($this->logged) {
$this->read_update_inbuff();
if ($max_len != null && $max_len < strlen($this->inbuffer)) {
$ret = substr($this->inbuffer,0,$max_len);
$this->inbuffer = substr($this->inbuffer,$max_len,strlen($this->inbuffer)-$max_len);
} else {
$ret = $this->inbuffer;
$this->inbuffer = "";
}
return $ret;

} else {
throw new SSHException('You need to login first before reading and writing data');
}
} else {
throw new SSHException('Disconnected!');
}
}

private function read_update_inbuff() {
$this->packet_disolver->read_packet();
if (($this->packet_disolver->get_packet_type() == SSH_SMSG_STDOUT_DATA) ||
($this->packet_disolver->get_packet_type() == SSH_SMSG_STDOUT_DATA)) {
$newdata = $this->packet_disolver->get_data();
if (strlen($newdata) > 0) {
$str_len = dword2integer(substr($newdata,0,4));
$this->inbuffer .= substr($newdata,4,$str_len);
}
} else {

//throw new SSHException('Unknown data packet type: '.$this->packet_disolver->get_packet_type());
}
}



Y va todo bien hasta que conecto al telnet y me pide el usario y en vez de pedirme la contraseña no me devuelve nada...

Mucas gracias y un saludo!

Última edición por flipe_; 08/07/2010 a las 03:14