BASH catching <&3 into var - bash

Trying to understand how the <&3 redirect can be caught into a var in bash.
When I run the code below it stops at line 9 with the MSG to SEND being printed as if the cat <&3 is being run without catching it in the var myRcvMsg. If I open another shell and nc on the port it continues to printout each line sent (this is what I want, just need to trap it rather than print it)
If I change line to myRcvMsg="$(<&3)" then lines 10,11 execute but no var prints out.
what am I doing wrong?
thx
Art
#!/bin/bash
echo "Starting Script"
exec nc -p 18000 -l &
sleep 1 # wait for port to open
exec 3<>/dev/tcp/192.168.1.1/18000
echo "MSG to SEND" >&3
echo "MSG has been sent"
myRcvMsg="$(cat <&3)"
echo "MSG should have been RCV'd"
echo "This is the RCV msg:${myRcvMsg}"

The execution of nc puts it into listen mode, but it will write its output to stdout instead of echoing it back through the net. See Echo server with bash for ideas how to make it into an echo server.
On my machine I had to use 127.0.0.1 to get a connection.
Next problem is that you have to make sure that your message is not stuck in a buffer. While you wait on the out-end of &3 for nc to echo something, nc may not have actually seen your message. In my test the $(cat <&3) just hangs.
The following kind of works.
#!/bin/bash
set -x
echo "Starting Script"
exec nc -p 18000 -l -c "xargs -n1 echo" &
sleep 1 # wait for port to open
exec 3<>/dev/tcp/127.0.0.1/18000
cat <&3 >ttt &
sleep 1
echo "MSG to SEND" >&3
echo "MSG has been sent"
sleep 1
myRcvMsg=$(cat ttt)
echo "MSG should have been RCV'd"
echo "This is the RCV msg:\"${myRcvMsg}\""
For more information, see http://www.ict.griffith.edu.au/anthony/info/shell/co-processes.hints, where the buffering problem is highlighted in 3/ Buffered Output problem.

Try to read from
myRcvMsg="$(cat /dev/fd/3)"

Related

How to adjust this bash script to run telnet commands successfully while being in SSH?

I am attempting to write a bash script that will do the following work flow:
Telnet into networked device via IP address on port 9100 telnet x.x.x.x 9100
Run SGD command ! U1 getvar \"internal_wired.ip.timeout.value\".
Expect output value of "10".
Here is the bash script I've written so far:
#!/bin/bash
IP=(x.x.x.x)
for i in ${IP}
do
echo " "
echo "Welcome! This script will check the timeout value of this networked device."
echo "The expected output should be `"10`". Let's get started!!"
echo " "
sleep 4
echo "5....."
sleep 1
echo "4...."
sleep 1
echo "3..."
sleep 1
echo "2.."
sleep 1
echo "1."
sleep 1
echo " "
telnet ${i} 9100 << END_SSH
sleep 5
getvar \"internal_wired.ip.timeout.value\"
sleep 5
END_SSH
done
When I run this script via bash mycode.sh, I get the following output in Terminal.app:
$ bash mycode.sh
Welcome! This script will check the timeout value of this networked device.
The expected output should be "10". Let's get started!!
5.....
4....
3...
2..
1.
Trying x.x.x.x...
Connected to x.x.x.x.
Escape character is '^]'.
Connection closed by foreign host.
[user#server ~]$
x.x.x.x is an IP placeholder just to add.
In theory, after the Escape character is '^]'. line, the script should have ran the ! U1 getvar "internal_wired.ip.timeout.value\" command.
Also, we should have had an expected output of "10".
When I first wrote this script, I initially did not have the END_SSH command in it. A colleague introduced that to me and said to wrap the telnet commands in the END_SSH because of how Terminal technically jumps out of SSH when you are in telnet. I've tried utilizing END_SSH, but am not successful.
How do I get the telnet command to run successfully and get the expected output value?
You misunderstand what "END_SSH" is. It's not a "command" - it's what's called "Here-document" in bash.
Essentially the text between the <<END_SSH and the END_SSH is a "here-document" that is piped into stdin of telnet ${i} 9100. So, the sleep 5 commands are never actually executed and the input reaches EOF before the connection is even established.
I don't know what exactly you are trying to accomplish, but I would guess that the following will work better. Oh, and what's with that weird IP=(x.x.x.x) declaration? Is that supposed to be an array?
#!/bin/bash
declare -a IP=(1.1.1.1 2.2.2.2 3.3.3.3 4.4.4.4)
for i in "${IP[#]}"; do
echo " "
echo "Welcome! This script will check the timeout value of this networked device."
echo "The expected output should be \"10\". Let's get started!!"
sleep 4
for j in {5..1}; do
echo $j
sleep 1
done
{ sleep 5; echo -n $'! U1 getvar "internal_wired.ip_timeout.value"\n'; sleep 5; } | telnet ${i} 9100
done
so here is what I suggest to use for the telnet part. Connect is a function being called later in a while loop, which will run over IPs ready from a file.
Connect()
{
(
sleep 10 # depending upon your network and device response, better to keep this first sleep value a little high
echo "command 1"
sleep 2
echo "command 2"
sleep 2
) | telnet $1 9100 | tee -a ${1}.log
}
while read -r IP
do
Connect $IP
done < filewithIPs

How to close netcat connection after receive a server response?

I need to sendo a lot of messages via netcat or something similar. The problem is that when I run echo "something" | netcat ip port the connection continues opened after I received the response. Actually the connection continues opened waiting for a new input. However, what I need is that the connection closed after I receive the response. Look, my script is basically this:
#!/bin/bash
i=1
while [ $i -ne 10000 ];do
sed -n $[i]p wordlist | netcat localhost 30002 >> result
i=$[$i+1]
done
If I can close the connection after print the response in result, everything will work fine. I know that there is an option -w "x" that closes the connection after "x" seconds, but the minimum value for "x" is 1 and 1 is bigger than I can wait, I need close the connection as soon as possible.
Unfortunately, the -q flag didn't work for me.
I'm using "OpenBSD netcat (Debian patchlevel 1.187-1ubuntu0.1)" and, even though the -q flag shows up in the manual, it didn't work as mentioned in cnicutar's answer.
Therefore, my workaround was:
#!/bin/sh
# POSIX COMPLIANT
HOST="localhost"
PORT=30002
scan () {
# Ensuring there is no file named msg
rm msg
# While msg file doesn't exist or is empty, do
while [ ! -s msg ]; do
# Remove instruction from within the loop
rm msg
# Append the received messages to msg file, and put the process in the background
echo "$HOST $PORT" | xargs nc >> msg &
# If the file exists and is not empty, return, we received the message
[ -s msg ] && return;
# A small timeout.. doing some tests I noticed that a timeout of zero sometimes didn't work to catch the message
# Maybe nc needs a small time to receive everything. You might want to test and increase or decrease this timeout if needed.
sleep 0.1
# This script will be spawning a lot of nc process, to kill it before the loop runs again
pkill -x nc
done
} 2> /dev/null
scan
# The function returned, so cat the file
cat msg
# make sure nc is killed
pkill -x nc > /dev/null 2>&1
rm msg
What you're looking for is the -q switch. If you specify:
netcat -q 0 localhost 30002
netcat will exit immediately.

Test if netcat listener got a connection and run a command locally

I need a way to fire a netcat listener from a shell script and if a connection received I need to run a command on the same local listener machine and without interrupting the netcat process / connection
it's like the -e option but I need to run a command locally while keeping the netcat connection running
I don't really know if it can be done I mean after the shell process forked the netcat child can it interact with nc's output for example and run other command before netcat exit?
Edit: I figured it's even easier to do it on the client C code side by checking the return value of an initial send() message to determine if the client connected successfully if we got the sent message length
sret = send(sock, message, strlen(message), 0);
if (sret == strlen(message)) // We're Connected
do something
Thanks
This will check if the initial nc process has started listening, and it will echo every line of input it receives and will then send back a Received response:
rm -f input.txt
touch input.txt
tail -f input.txt | nc -l 5555 > output.txt &
if ! ps -p $! >/dev/null; then
echo "Netcat didn't start. Exiting..."
exit 1
fi
tail -f output.txt | while read -r LINE; do
echo "Received input: $LINE"
echo "Received" >> input.txt
done
See if you can adapt this to meet your needs.

I want to open a netcat connection and keep sending some text forever

echo " The NC send commands on port 2612"
while :
do
echo "hello " | nc -q -1 <some IP> 2612
done
I want open a netcat session forever, and that is been achived by -q -1.
How can I send "hello" on the same channed in every 20 second.
my earlier script was as following, but that opens nc connection every time.
What I really want is to open connection once and send "echo hello" evey 20 sec.
while :
do
echo "hello " | nc 192.168.100.161 2612
sleep 20
done
while echo "hello"; do
sleep 20
done | nc -q 192.168.100.161 2612
Note that we moved the echo into the condition of the loop, so that we stop trying to run it if nc exits (causing the echos to fail).
If you want to be able to retain state (for instance, a counter) from inside the loop and access it after the pipeline exited, then things need to change a bit further:
#!/bin/bash
# ^^^^- process substitutions aren't part of POSIX sh
count=0
while echo "hello"; do
(( ++count ))
sleep 20
done > >(nc -q 192.168.100.161 2612)
echo "Ran $count loops before exiting" >&2
There, the redirection is done as a process substitution, and the loop takes place inside the same shell instance that continues after it exits. See BashFAQ #24 for more details on this problem and solution.

Reading lines from piped input in infinite while loop

I made a simple script in bash to serve as a http proxy.
#!/usr/bin/env bash
trap "kill 0" SIGINT SIGTERM EXIT # kill all subshells on exit
port="6000"
rm -f client_output client_output_for_request_forming server_output
mkfifo client_output client_output_for_request_forming server_output # create named pipes
# creating subshell
(
cat <server_output |
nc -lp $port | # awaiting connection from the client of the port specified
tee client_output | # sending copy of ouput to client_output pipe
tee client_output_for_request_forming # sending copy of ouput to client_output_for_request_forming pipe
) & # starting subshell in a separate process
echo "OK!"
# creating another subshell (to feed client_output_for_request_forming to it)
(
while read line; # read input from client_output_for_request_forming line by line
do
echo "line read: $line"
if [[ $line =~ ^Host:[[:space:]]([[:alnum:].-_]*)(:([[:digit:]]+))?[[:space:]]*$ ]]
then
echo "match: $line"
server_port=${BASH_REMATCH[3]} # extracting server port from regular expression
if [[ "$server_port" -eq "" ]]
then
server_port="80"
fi
host=${BASH_REMATCH[1]} # extracting host from regular expression
nc $host $server_port <client_output | # connect to the server
tee server_output # send copy to server_output pipe
break
fi
done
) <client_output_for_request_forming
echo "OK2!"
rm -f client_output client_output_for_request_forming server_output
I start it in first terminal. And it outputs OK!
And in the second I type:
netcat localhost 6000
and then start entering lines of text expecting them to be displayed in the first terminal window as there is a cycle while read line. But nothing is displayed.
What is it that I'm doing wrong? How can I make it work?
If no process is reading from the client_output fifo, then the background pipeline is not starting. Since the process that reads client_output does not start until a line is read from client_output_for_request_forming, your processes are blocked.

Resources