Telnet using shell Scripting

i have written a script as shown below :

telnet 10.161.240.2 8100
<?xml version="1.0"?> <login_command> <sequence id="1" /> <access  id="1"/> <user name = "user1" password = "pass"/> </login_command> 
<?xml version="1.0"?> <RegistrationDescriptor> <sequence id="1" /> <access    id="1"/> <attributeDescriptors> <action>Register</action> </attributeDescriptors> </RegistrationDescriptor> 

when i directly run this commands separately raw data is coming so i want to store that raw data in a text file for which i have this above commands and saved as .sh extension and then tried to run as :
"sh myscript.sh >> mydata.txt"

but then to my data is not getting stored error is coming as:

"Connection closed by foreign host"

telnet cannot read xml.
try:

telnet 10.10.101.1 <<EOF
username
password
[command you want execute like ls -l]
[another command here]
exit
EOF

That is called a here document - the EOF .... EOF bit

Hello Jim,

The above logic does work for "ftp" and not "telnet" I guess!

Regards
Ravi

@akrati1
Can you show us a manual session which works? What you post looks like a connection to a website or program which expects you to be using a browser ... but I could be wrong.

@panyam
I agree. You cannot use typeahead to telnet in that manner but there are other Shell techniques which work.

Let's find out if the O/P should be using telnet in the first place.

Is this for some sort of web-based service on an odd port? wget would be a more appropriate tool for that than raw telnet.