Pinned topic Telnet using script


i m trying to run the below script and store the output of this script in the text file.
#!/bin/sh
telnet 10.161.240.2 8100
<?xml version=\"1.0\"?> <login_command><sequence id=\"1\" /> <access id=\"1\"/> <user name = \"ttmlcnms\" password = \"cnms#123\"/> </login_command>
<?xml version=\"1.0\"?> <RegistrationDescriptor><sequence id=\"1\" /> <access id=\"1\"/> <attributeDescriptors><action>Register</action> </attributeDescriptors> </RegistrationDescriptor>
but error is coming as connection closed by foreign host.
-
Re: Telnet using script
2013-07-29T14:44:58ZThis is the accepted answer. This is the accepted answer.
I don't think you can run telnet like that. What type of system is the telnet server you're trying to connect? The traditional method of automating telnet sessions is to use a .netrc file or an expect script. The connection is closed because after the telnet command.
-
Re: Telnet using script
2013-08-13T13:07:22ZThis is the accepted answer. This is the accepted answer.
I've used this silly method on occasion to keep the session open but as Joe says I think use "expect". I've also tried "empty.sourceforge.net" which perhaps has a smaller foot print than "expect".
mknod pipe p # create a named pipe
# sleep to keep the session open
exec 1> trace_file
( sleep 300 > pipe;
print -u2 "Exceeded time limit.\r;
kill -9 $$" ) &
PIPE_MASTER_PID=$!# telnet in the background
telnet -e vt100 hostname < pipe 2> error_file &
TELNET_PID=$!while :; do
sleep 1
egrep -q "login:" trace_file
if [[ $? -eq 0 ]]; then
print us > pipe
fi
egrep -q "password:" trace_file
if [[ $? -eq 0 ]]; then
print password > pipe
fi# When you're done
# kill -9 $PIPE_MASTER_PID
# kill -9 $TELNET_PID
done