cat & telnet

cat & telnet
hello,

I need some help on using a file with the cat command. I want to set up a telnet connection with a network device with the ip-adress 10.3.0.1. Just executing the command 'telnet 10.3.0.1' gives a menu. For example, to show the help of the menu, you need to type '?'.

Now I want to go through the menu with commands (like '?') that are in a file. For this I want to use the command:
cat filename | telnet 10.3.0.1

When I try this, the telnet connection is established, but then it closes immediately afterwards. So I suppose something's wrong with the file. But I have no idea what the file should look like. For example, do you need to mention the enters after a command in a certain way?

just to be clear: the command in the menu that I want to execute are:
"?" -> enter
"projector" -> enter
"exit" -> enter

thanks in advance,

Micha�l

It may be inputting the data and finding EOF before it even gets to the menu. The expect tool is often used to automate interactive prompts like this, since it can be told to wait for responses or prompts before sending data.

Also see useless use of cat.

To make a shell script which can telnet automatically and run commands on the other machine and end the telnet session without any interaction from the user, you need to install EXPECT and TCL packages on your unix system.

If you are done with above mentioned then, you can write a script similar to following (as per your requirements) :

#!/usr/local/bin/expect -f ####/usr/local/bin/expect is the directory where expect was installed 
log_user 0 ####this command is used to hide the conversationn between the script and the other machine 
set address [lindex $argv 0] ###assign the first passed parameter while calling the script to $address 
set username [lindex $argv 1] ###assign the 2nd passed parameter while calling the script to $username 
set password [lindex $argv 2] ###assign the 3rd passed parameter while calling the script to $password 
spawn telnet ${address} ###start the telnet session to machine with IP=$address 
###start conversation with the machine: 
expect "login:" 
send -- "${username}\r" 
expect "Password:" 
send -- "${password}\r" 
expect "$ " 
send -- "#!/bin/ksh\r" ###declare the shell to be used (optional) 
expect "$ " 
send -- "###type here any command you want to execute" 
expect "$ " 
send -- "exit\r" ###end the telnet session and exit the script 

to execute this script, you can type:

$ expect_script "address" "username" "password" ###the script name is "expect_script"

but before u call the expect script, you should make the expect script executable, and this can be done by typing:

$ chmod +x expect_script

You can also call the above script from any other script....:b::b: