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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Auto TELNET 2

Status
Not open for further replies.

malpa

Technical User
Feb 8, 2004
122
CO
I have this perl-script to access the server auto, I would like to do the same with unix script.


#!/usr/bin/perl
#script: atutotelnet.pl

$filename1= "log1.txt";
$hostname = "10.57.56.161";
$username = "";
$passwd = "";
$path = "/ / ";

## Conexion y autenticaciòn.
use Net::Telnet ();
$host = new Net::Telnet (Timeout => 10,
Dump_Log => $filename1,
Prompt => '/>/');
$host->open($hostname);
$host->login($username,$passwd);

$host->cmd("cd $path");
@lines = $host->cmd("ls -ltr");
print @lines;
print "\n";
print "Digite el mes";
print "\n";
$mes=<STDIN>;
chop($mes);
@lines1 = $host->cmd("ls -ltr $mes");
print @lines1;
print "\n";
$host->close;

My unix-script is:


#! /bin/bash

(sleep 1;
echo "user";
sleep 1;
echo "password";
sleep 1;
echo "ls -ltr";
sleep 1;
) | telnet 10.57.56.161

Malpa
Thanks for your help.
 
Do a keyword search in this forum for telnet expect.
Expect is the key for you as most telnet client doesn't use their standard input as you want.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I use this on a Sun Unix box:

#!/bin/sh
tpipe()
{
sleep 3
echo "login\r"
sleep 1
echo "password\r"
sleep 1
echo "commands\r"
sleep 1
echo "exit\r"
sleep 5
}
tpipe|telnet ip.address

Substitute your info above. Hope this helps!
 
Here is your info:

#!/bin/sh
tpipe()
{
sleep 3
echo "user\r"
sleep 1
echo "password\r"
sleep 1
echo "ls -ltr\r"
sleep 1
echo "exit\r"
sleep 5
}
tpipe|telnet 10.57.56.161
 
What would be the best way to throw in an exception where if telnet can't connect to the machine within 15 seconds, it breaks and returns an exit code?
 
Code:
         [ $# -lt 1 ] && set -- cat /var/adm/messages      # default command

         user=USER
         passwd=PASSWORD
         host=HOST

         (
             sleep 2     # give "telnet" time to connect
             echo "$user";   sleep 2
             echo "$passwd"; sleep 2
             echo "$*"   # execute command!
             sleep 2     # give time to print results
         ) | telnet "$host"

Even though other suggestions have been given, this is what I do to run a command remotely on hosts.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top