Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script Telnet Session Keeping Alive

Status
Not open for further replies.

aoroca

IS-IT--Management
Oct 3, 2003
39
CO
Hey Guys,

I need your timely help!

I need establish a telnet session in one device, and keep alive this connection and performe some commands over this seesion.

How I can develop this script in order to connect to my 2device?
 
You may consider to use expect(/b]

Hope This Help
PH.
 
HI PHV, Do You can help me with this issue?

In directory DIR1 I have a FILE1.txt which is being updated in diferent times. And when this file is new, MySession execute all commands included in this FILE1.txt.

I need keep alive this sesion to this specific port. How I can get it?

Thanks a lot.
 
The script must wait some arguments or parameter according this telnet session over TARGET.

For example my script in PHP:
*******************************************

cat Dispatch_pre.php
#!/usr/local/bin/php -q
<?php

function traer_archivo()
{
$ftp_server=&quot;10.2.15.80&quot;;
$conn_id = ftp_connect($ftp_server);
$user=&quot;ftpd&quot;;
$pass=&quot;ftpd&quot;;

$camino=&quot;/var/activa_desactiva/&quot;;
$archivo=&quot;dispatch.txt&quot;;
$local=&quot;/var/prepago/&quot;.$archivo;
echo &quot;archivo es $local &quot;;
$fp=fopen($local,&quot;w&quot;);
$siga=0;

$login_result = ftp_login($conn_id, $user, $pass);
if((!$conn_id) || (!$login_result))
{
$siga=1;
echo &quot;Fallo Conexion SYPLEX&quot;;
$comando=&quot; \&quot;Problemas Conexion SYPLEX Aplicacion &quot;.
&quot; Prepago DISPATCH\&quot; \n&quot; ;
# $cadena=&quot;./singlemsg 5850532 &quot;.$comando;
system($cadena);
echo &quot;cadena $cadena&quot;;
# $cadena=&quot;./singlemsg 162101 &quot;.$comando;
system($cadena);
}
else
{
if (ftp_chdir($conn_id,$camino))
$fget_result=ftp_fget($conn_id, $fp, $archivo,FTP_ASCII);
if (!$fget_result)
{
echo &quot;No existe el archivo para traerlo &quot;.$archivo.&quot; &quot;.date(&quot;YmdHi&quot;);
$siga=1;
}
$result=ftp_delete($conn_id,$archivo);
fclose($fp);
ftp_quit($conn_id);
}
return $siga;
}



function recibir_respuesta($veces,$apunta)
{
for ($i=0;$i<$veces;$i++)
{
fgets($apunta,128);
}
}

function enviar_comando($apunta,$comando,$tiempo)
{
usleep($tiempo);
fputs ($apunta,$comando);
usleep($tiempo);
}

function ingresar($apunta)
{
$continua=0;
$devuelve=&quot;&quot;;
while ($continua==0)
{
fputs ($apunta, &quot;\r\n&quot;);
if (!strstr($devuelve,&quot;SYNTAX ERROR&quot;)) {
fputs ($apunta, &quot;\r\n&quot;);
}
$devuelve= fgets ($apunta,128);
echo &quot;$devuelve\n&quot;;
if (strstr($devuelve,&quot;login:&quot;)) {
usleep(500000);
fputs ($apunta, &quot;adc\r\n&quot;);
usleep(500000);
$devuelve= fgets ($apunta,128);
}
if (strstr($devuelve,&quot;Password:&quot;))
{
usleep(500000);
fputs ($apunta, &quot;Nmb9tn\r\n&quot;);
usleep(500000);
$devuelve=fgets ($apunta,128);
}
if (strstr($devuelve,&quot;Starting AD1T ........&quot;)) $continua=1;
} //while
echo &quot; logro ingresar $continua &quot;;
}

if (traer_archivo()==0)
{
echo &quot;Tiene archivo para procesar ...&quot;;
$Filename=&quot;dispatch.txt&quot;;
$archivo=&quot;pre&quot;.date(&quot;YmdHi&quot;);
$cadena=&quot;mv &quot;.$Filename.&quot; &quot;.$archivo;
$renombre=system($cadena);
$Filename=$archivo;
$handle = fopen($Filename, &quot;r&quot;);
if ($handle){
$fp = fsockopen (&quot;10.2.2.12&quot;, 3000, $errno, $errstr, 30);
if (!$fp) {
echo &quot;$errstr ($errno)<br>\n&quot;;}
else{
ingresar($fp);
enviar_comando($fp,&quot;\r\n&quot;,0);
while (!feof ($handle)) {
$buffer=fgets($handle, 4096);
echo $buffer;
$buffer=$buffer.&quot;\r\n&quot;;
enviar_comando($fp,$buffer,&quot;50000&quot;);
if (strstr($buffer,&quot;T&quot;)) recibir_respuesta(3,$fp);
if (strstr($buffer,&quot;POS&quot;)) recibir_respuesta(2,$fp);
if (strstr($buffer,&quot;CHA&quot;)) recibir_respuesta(1,$fp);
}//while
echo &quot;salio del wh va a cerrar sesion&quot;;
fputs ($fp,&quot;Q \r\n&quot;);
fputs ($fp,&quot;\r\n&quot;);
}//else
fclose ($fp);
fclose ($handle);
echo &quot; cerro puerto y archivo &quot;;

$cadena=&quot;mv &quot;.$Filename.&quot; logs/&quot;;
system($cadena);
echo $cadena;

}//if handle
else
echo &quot;No existe el archivo &quot;,$Filename.&quot;\n&quot;;
}

?>

****************************************
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top