How to make a "same response" server with bash? - linux

I'm trying to set up a simple server that returns always the same response.
Based on this question I've tried to use
ncat -l 2000 --keep-open --exec "/bin/echo 234"
but on the client it shows only once.
Ncat: Broken pipe.
If I use the UDP option (-u), it works as intended. So I'm guessing it's EOF's fault.
Is there a way to make it work as a reponse to the client's messages in TCP?

For something as simple you could:
ncat -l 2000 --keep-open --exec "xargs -I{} echo 234"
I find also the following works:
ncat -l 2000 --keep-open --sh-exec "while read line; do echo 234; done"
or like:
ncat -l 2000 --keep-open --sh-exec "echo 234; cat >/dev/null"

Related

ncat echo server, write incoming message to shell?

Is there a way to get ncat to print what it receives to the terminal before it echos back on the port with cat, Looks like the executed code doesn't have access to stdio?
I think tee is supposed copy stdout to cat with something like this, but I don't see any output.
ncat -e '/usr/bin/tee > (/bin/cat)' -l -p 2048

is it possible to run bash commands over shell running on telnet?

So we have Embedded Linux board running Linux.
We can connect to that board using telnet and that spawns shell and gives access to it.
Now I am writing a bash script where I want to run commands on that shell and get its output.
e.g. My commands are something like below command over telnet and see if that was successful or not.
test -c /dev/null
When I run it like below I always get 1 as exit status
{ test -c /dev/null; sleep 1;} | telnet <board ip addr>
If possible I don't want to use expect,
Any suggestion/pointers ?
With SSH could trivially and robustly have done:
ssh yourhost 'test -c /dev/null'
With a simple shell on a TCP port, you could somewhat robustly but annoyingly have used:
echo 'test -c /dev/null; echo $?' | nc -q 1 yourhost 1234
telnet is instead notoriously timing sensitive and tricky to script, so since you don't want to do it robustly with expect, you can try to kludge it:
{ sleep 1; echo 'test -c /dev/null; echo $?'; sleep 1; } | telnet somehost

Automated telnet using shell with output logging

I would like to write a automated script to open telnet session and run some commands. The thing is, that this will be some kind of "logging", so i have to open pipe, and send some commands, and store outputs. I know, how to do this in a while loop like:
(while true
do
echo ${user}
sleep 1
echo ${pass}
sleep 1
echo ${something}
.
.
done)|telnet ${IP}
The problem here is that the telnet pipe is opened/closed in every loop and i want to achieve to open it at the beginning, and then send commands in a loop until some conditions are true.
NOTE: i am limited with commands as i am working with emb.system (such as spawn, expect, etc...)
Thanks for your help ! :)
BR.
Does this work for you?
(echo ${user}
sleep 1
echo ${pass}
sleep 1
while true; do
echo ${something} | tee -a /tmp/logfile.txt
.
.
done
echo "exit") | telnet ${IP} | tee -a /tmp/logfile.txt
you can use sshpass soft.
http://sourceforge.net/projects/sshpass/
tar -zxvf sshpass-1.05.tar.gz
cd sshpass-1.05
./configure
make && make install
.............

run a command conditionally with netcat and grep

I need netcat to listen on incomming HTTP requests, and depending on the request, I need to run a script.
So far I have this;
netcat -lk 12345 | grep "Keep-Alive"
So every time netcat recieves a package that contains a "keep-alive", I need to fire a script.
It needs to run in the crontab...
Thanks for your help!
How about this?
#!/bin/bash
netcat -lk -p 12345 | while read line
do
match=$(echo $line | grep -c 'Keep-Alive')
if [ $match -eq 1 ]; then
echo "Here run whatever you want..."
fi
done
Replace the "echo" command with the script you want to execute.
How about:
#!/bin/bash
netcat -lk -p 12345 | grep 'Keep-Alive' | while read unused; do
echo "Here run whatever you want..."
done
Or
#!/bin/bash
netcat -lk -p 12345 | sed -n '/Keep-Alive/s/.*/found/p' | xargs -n 1 -I {} do_something
# replace do_something by your command.
Depending on what you have on the client side, it might sit there waiting for netcat/the server to respond.
I did a similar thing to the above but used
while true do
netcat -l 1234 < 404file.txt
Where 404file.txt has HTTP/1.1 404 FILE NOT FOUND
This disconnects the client and since netcat has no 'k' it terminates and restarts because of the while true, all ready to receive and send the 404 again.

Using named pipes to create a 'loop'

I'm very new to shell scripting and I am trying to get to grips with piping. I could be heading in completely the wrong direction here...
What I have is a shell script that contains a simple while true loop, within this loop I am getting netcat to listen on a specified port and piping input to a binary file that is awaiting for commands through stdin. This is Script-A
I have a second shell script that accepts input as arguments, it then echos those arguments to the port that netcat is listening on. This is Script-B
My aim is to get the returning output from the binary file located in Script-A into Script-B via Netcat so that it can be returned via stdout. The binary file has to be initialized and awaiting input.
This is what I have:
Script-A
while true; do
nc -kl 1234 | /binarylocation/ --readargumentsfromstdinflag
done
Script-B
foo=$(echo "$*" | nc localhost 1234)
echo "$foo"
With this setup, the output of the binary file is done via Script-A
After doing some research I got to this point, I am trying to use a named pipe to create a sort of loop from the binary file back to netcat, it's still not working -
Script-A
mkfifo foobar
while true; do
nc -kl 1234 < foobar | /binarylocation/ --readargumentsfromstdinflag > foobar
done
Script-B hasn't changed.
Bear in mind my shell scripting experience stems over a period of about a single day, thank you.
The problem is in your script B.. netcat reads from STDIN and exits immediately when STDIN is closed, not waiting for the response.
you will realize when you do this:
foo=$( ( echo -e "$*"; sleep 2 ) | nc localhost 1234)
echo "$foo"
nc has a parameter for the stdin behaviour..
-q after EOF on stdin, wait the specified number of seconds and
then quit. If seconds is negative, wait forever.`
So you should do:
foo=$( echo -e "$*" | nc -q5 localhost 1234)
echo "$foo"

Resources