Linux script to write data from telnet message into a file while a period - linux

I am connecting to a telnet listener. Telnet server sends some data for every second. I want to read the messages while X seconds and write it into a file (we'll take 6 seconds for the example).
Note: The IP address has been changed to 'IP' for the example. Same for 'Port'.
I already tried some things:
#!/bin/bash
#myScript.sh
telnet IP Port >> myFile.txt
sleep 6
pkill myScript.sh
This solution write in my file but my script never ends.
Here my 2nd proposition:
#!/bin/bash
#myScript.sh
timeout 6 telnet IP Port >> myFile.txt
Here, it's another issue, timeout is respected, the script ends after 6 seconds but in 'myFile.txt', I have
Trying IP...
Connected to IP.
Escape character is '^]'
How can I make this script right?
Note: I must use Telnet.

In your first solution you could try try:
telnet IP Port 2>&1 | tee myFile.txt &
sleep 6
exit
This will send the telnet command to a background process and then exit after 6 seconds.
In your second solution you could try:
timeout 6 telnet IP Port 2>&1 | tee myFile.txt
This sends stderr and stdoutt to myFile.txt
https://www.gnu.org/software/libc/manual/html_node/Standard-Streams.html
Or, as others have suggested, use netcat:
timeout 6 nc -vz IP Port 2>&1 | tee myFile.txt
http://netcat.sourceforge.net/

Related

How can I modify the netcat command line to send many requests at once

Let's suppose there is a TCP server listening to port 8080.
The client connects and sends, for example, a "hello" message after using the Netcat command:
nc localhost:8080
How can I send the "hello" message 1000 times without me being typing? Can I access the Netcat file and add a 'for' or 'while' loop?
No need to modify nc; just pipe your input to it: yes hello | head -n 1000 | nc localhost:8080.

redirect the ouput of command into a socket on linux

I'm using netcat to connect to a server.
the problem is that i want to send somme non printable to the server caracters.
I wanted to achieve this with a command redirection in linux.
lets say this is the command: nc hostname port
so when i checked the file descriptors of the command nc in the folder: cd /proc/$(pidof nc)/fd is saw the there was another fd with number 3 that conserns the socket. 3 -> socket:[1675643]
the problem is that i wanted to redirect the output of let's say echo -ne '\xff\x0f\xab\xde' > ./3 to the socket.
I couldn't do so and the ouput is: bash: ./3: No such device or address
One cannot output something to a socket which is opened only by another process.
In order to first use interactive input/output and afterwards send the echo string, you can do:
(cat; echo -ne '\xff\x0f\xab\xde')|nc hostname port
(press the EOF character Ctrl-D to end your input and start the echo).

Shell script to find out if a port is being listened to using netstat?

I have a server where a certain port (9999) is being listened to by a PHP socket server. What happens is that devices can connect to the socket and send messages. The code works fine right now, however, I noticed that the socket would sometimes close or die off, and I need to be able to put it back up online automatically without me having to log in and run it again.
What I'm thinking of is writing a Shell script that would check via netstat if there's a process running on port 9999, and if there's none, the script would trigger the PHP socket server to go online again. This Shell script would then be called by Cron every 1 or 2 minutes to check if the PHP socket is running.
I have bare minimum knowledge about Shell scripting, and so far, this was the only other thing I wrote in Shell:
#!/bin/sh
if pidof "my process name here" >/dev/null; then
echo "Process already running"
else
echo "Process NOT running!"
sh /fasterthancron.sh
fi
I think I should be able to reuse this code to some degree but I'm not sure what to replace the if condition with.
I have the idea that I'm supposed to use netstat -tulpn to figure out what processes are running, but I'm not sure how to filter through that list to find if a specific process is running on port 9999.
If you use netstat -tlpn (or its replacement ss -tpln), you can grep for 9999 and look for processes listening on it under "Local Address".
ss -tpln | awk '{ print $4 }' | grep ':9999'
Alternatively, if you can, use netcat or telnet instead e.g. nc -v localhost 9999.
if echo -n "\cD" | telnet ${host} ${port} 2>/dev/null; then
...
fi
I wrote something similar a while back: docker-wait
This was forked from aanand's docker-wait
You can use famous netstat -tupln with a simple if/else logic to do this.
if [ -z "$(sudo netstat -tupln | grep 9999)" ];
then
echo notinuse;
else
echo inuse;
fi

Linux shell: why is "open" required in this telnet command

I tried to process telnet output in bash and i stumbled upon this syntax to send telnet commands to a server
( echo open $host $port
sleep 1
echo $cmd1
sleep 1
) | telnet
What i would like to know is why the "open" command is required and why
( echo $host $port
...
) | telnet
results in a "?Invalid command" error.
...because a hostname is not a valid command name? There is a big difference between
$ telnet host port
and
$ telnet
telnet> host port
Where the latter is what your echo command is effectively doing.
The one-liner automatically runs an open command, so it is basically equivalent to this:
$ telnet
telnet> open host port
But I'm not at all sure why you wouldn't just run telnet host port in the first place.

How to delay pipe netcat to connect on first input

Running in bash under Ubuntu:
I have a source that generates me some output, but not straight away. Let's assume it is a first netcat listening on a socket: netcat -l 12345.
And I would like to pipe it to an outgoing netcat (connecting over TCP), e.g. netcat -l 12345 | netcat localhost 54321. But the tricky bit is, that I know there is nothing listening for that incoming connection on localhost 54321 when I run the command, but I know there will be one when the first actual character arrives through the pipe.
So my question is: is there a way either:
to delay the execution of the outgoing netcat until the first character arrives into the pipe, or
to delay the outgoing netcat from trying to establish the TCP connection until it receives the first character on its standard input? (no straight option for that in man, switching to UDP is not acceptable)
Thanks in advance!
Edit: In reality, the source is more complex than a netcat, namely it is a listening netcat piped through all sort of stream modification.
Using the research you already did and that I commented to (by not knowing it was an answer to your own question), here is the full delayed_netcat.sh:
#!/bin/bash
read line
netcat "${#}" < <(echo $line ; cat)
This first waits for a line of input and later prepends that line using a simple echo to the "newly generated" input to the actual netcat. The rest of stdin is just redirected using cat which slurps it from stdin and adds it to the input of netcat. It also supports passing commandline options and arguments to the "real" netcat.
The usage is as follows:
netcat -l 12345 | cmd1 | cmd2 | ... | ./delayed_netcat.sh localhost 54321
The netcat is delayed till the first line is read. If you really want to start it after the first character is read the parts with read and echo need some rewrite.
Port Forwarding or Port Mapping with netcat:
ncat -l -p 12345 -c 'ncat localhost 54321'
Using socat:
socat TCP4-LISTEN:12345 TCP4:localhost:54321
This command exits after the first connection is done.
I have found an answer to my question, but it is awful... so still looking for something better.
netcat -l 12345 | gawk '(NR==1){print""}{print;fflush()}' | ./delayed_netcat.sh
where ./delayed_netcat.sh:
#!/bin/sh
read line
netcat localhost 12345
So the read line delays the netcat localhost 12345 by waiting for and consuming the first input line, and I use gawk '(NR==1){print""}{print;fflush()}' to insert an empty line just before the first record... I'm sure there is room for much improvement to that!

Resources