Using telnet in my shell script

Hello Guys,

I have a linux server where I already logged in, once logged in, i telnet to local host using some dedicated port and do some action. This I can easily do manually.

Since I need to fire a lot requests so I would like to optimize it using a shell script to avoid telnetting each time and do the arduous manual actions, can some body assist me, how to fire a request after triggering telnet in the script. Please note after i telnet to local host i don't need to give any user password.

Below is what I am doing manually which i want to optimize.

telnet localhost <port>

<I will paste my xml Request here>
<Response will come>

I.e:

telnet localhost 1111
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

POST /Air HTTP/1.1
Connection: close
User-Agent: UGw Server/4.3/1.0
Authorization: Basic ZmRzdXNlcjpmZHN1c2Vy
Host: 1.1.1.1:1111
Content-Length: 775
Content-Type: text/xml; charset=utf-8

<methodCall>
<methodName>GetBalanceAndDate</methodName>
<params>
<param>
<value>
<struct>
<member>
<name>originNodeType</name>
<value>
<string>EXT</string>
</value>
</member>
<member>
<name>originHostName</name>
<value>
<string>xyz</string>
</value>
</member>
<member>
<name>originTransactionID</name>
<value>
<string>20130315174659200</string>
</value>
</member>
<member>
<name>originTimeStamp</name>
<value>
<dateTime.iso8601>20130315T17:46:59+0000</dateTime.iso8601>
</value>
</member>
<member>
<name>subscriberNumberNAI</name>
<value>
<i4>1</i4>
</value>
</member>
<member>
<name>subscriberNumber</name>
<value>
<string>0101010101</string>
</value>
</member>
</struct>
</value>
</param>
</params>
</methodCall>

Would be greatful if somebody help me to kick start my script.

THanks!!
Umar

You should use ssh, because a) it's encrypted while telnet is not, b) you can, when you have generated and exchanged keys (there is plenty of instructions for this here in the forum as well as on the www [check for "ssh passwordless" or "ssh without password"]) you can simply issue commands on remote machines.

Hi!
Of course zaxxon is right.

However... this below is a bash function I use to telnet my tplink router and reset its adsl connection to my provider.

adslreconnect () {
      ( echo "$myuser";
	sleep 1;
	echo "$mypassword";
	sleep 1;
	echo "ppp config 0.8.35 0 down";
	sleep 1;
	echo "ppp config 0.8.35 0 up";
	sleep 1;
	echo "exit") | telnet tplink >/dev/null 2>&1; }

I think you can pretty easily adapt the code above to do what _you_ wanna do instead: send _your_ requests and redirect stdout and stderr wherever you like (not to /dev/null, I guess).

Actually in my scenario I can't use ssh, reason being is that in my scenario I do not need to login to any remote node/server. I want to send an http/xml requests by telneting to local host of my server on specific port.

In that case function that LEM has sent would not work. Here is what I am trying to achieve through a script.

telnet localhost 1111
[I will paste my http/xml Request here]
[Will get http response from server]
[I will paste my http/xml Request]
[Will get http response from server]

Example of my scenario is mentioned in my opening post.

Regards,
Umar