Writing to and reading from a socket from bash script.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Writing to and reading from a socket from bash script.
Hello all.
Here the description of the issue I am having.
I am writing a bash test script which reads lines from a file, builds ISO messages, sends them to a server,
reads the response with response code and reports the result of the test to a file or on the screen.
The message that I need to send is 94 characters long.
Here's the portion of a code that I initially wrote:
###############################################
#~ Open socket.
exec 3<>/dev/tcp/172.26.0.25/9991
#~ Send msg.
echo "$msg_out" >&3 # OK but adds EOL character which confuses the server.
It works OK with the "echoserver".
However, it does not on a real server.
The symptom is that the program is blocked on "read" statement.
One of the facts that is confirmed by server people is that the message received by a server is actually 95 bytes long.
So, based on this information I have 2 questions:
1. Is there any other way to do what I do in line "echo "$msg_out" >&3" to avoid End Of Line character?
2. Am I using the correct way to read from socket? "read -r msg_in <&3".
Never tried using your method of using sockets in a shell script. When confronted by such tasks, I typically turn to netcat (nc, on most installations). Perhaps your newline is coming from the echo command, and can be supressed with the -n switch.
Rod:
thanks a lot for your post.
It took care of my question #1.
With "-n" switch the server people confirmed that they get exactly 94 bytes.
I still have a problem reading from the server, which is my question #2.
What's interesting is that with "-n" switch even "echoserver" can't read the msg back (it could before).
Looks like I need a better way to read.
Thanks again,
vouser.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.