Telnet Session

{
sleep 2
echo "$user"
sleep 2
echo "$password"
sleep 2
echo " ls"
sleep 10
echo "exit"
}| telnet $server

I have a machine x and i have executed the above script on machine 'x'.

i entered the appropriate user and password in the script and gave the ipaddress of the server in telnet $server.

When i executed this script on machine x, telnet started to another machine, took the user name and password and got logged in. It then executed the "ls" and "exit" commands from the script.

But i removed the exit command and executed the script, I got the prompt on the other machine, but i am not able to execute any commands and the prompt hangs.

                            HELP ME OUT !!!!!!!!!!!!!!!!!!!!

your script --- not stdin --- is supplying input to the telnet process so it hangs when you take the exit comand out as telnet is waiting for more input ...

Please reply me with the modified code if possible !!!!!!!!!

what is the purpose of this script? is this just to help you login without putting in your password? if so, try rlogin (see "man rlogin") or ssh (see man "ssh") ...

Hi,

This is the purpose of this script.


Everyday, many times i need to log on to a local unix machine first, then use telnet to connect to remote unix machine using the desired username and password.

What my idea is whenever i execute a shell script(contains the ipaddress of remote machine, username, password) on a local unix machine, it gets connected automatically. May be the purpose is not worth but just got eager to know whether it can be done or not.

Thanks in advance if you can post the code or modify the script i have sent.

somebody else might be able to modify your code to do what you want but i think that is a big waste of time as you can definitely use rlogin or ssh to what you need ... good luck!

You could use expect, a bit like [post=67987]this[/post]. What you're trying to do is hard. In place of your
echo "exit"
you will need a loop that reads from stdin and writes to the telnet process. The next problem is terminated that loop and the remote shell at once. In the code below, I went with cntl-d. When the user types EOF (usually cntl-d), the loop terminates. Then the remote shell is sent an "exit". You can only send lines to the remote shell this way. No vi. No emacs. So I agree with No Ice, rch or ssh is the way to go. Still here is my code....

#! /usr/bin/ksh
HOST=xxxxxx
USER=yyyyy
PASSWD=zzzz

exec 4>&1
telnet  >&4 2>&4 |&

print -p open $HOST
sleep 3
print -p $USER
sleep 3
print -p $PASSWD
sleep 3
print -p df -k
sleep 3
while IFS="" read line ; do
        print -p "$line"
done
print -p exit

wait
exit 0