I have an embedded linux application with a simple interactive command line interface.
I'd like to access the command line from telnet (or network, in general).
However, the process should be started when the board turns on, and in a unique instance. So, the following netcat command is not an option:
nc -l -p 4000 -e myapp
I can do
nc -l -p 4000 | myapp
to send remote commands to myapp, but this way I can't see myapp output.
Is there any way to redirect both stdin and stdout to netcat?
Thanks.
Is there a way to redirect both stdin and stdout to netcat
There is socat, which is a more advanced netcat. You can redirect both stdin and stdout with it. E.g.:
socat TCP4-LISTEN:5556,reuseaddr,fork EXEC:"cat - /etc/redhat-release"
In the above cat reads stdin and /etc/redhat-release and outputs them into stdout.
And then try using that:
$ echo "hello" | nc 127.0.0.1 5556
hello
Fedora release 22 (Twenty Two)
$ echo "hello 2" | nc 127.0.0.1 5556
hello 2
Fedora release 22 (Twenty Two)
I found that by using bash v. >= 4.0 I can use coproc:
#!/bin/bash
coproc myapp
nc -kl -p 4000 <&"${COPROC[0]}" >&"${COPROC[1]}"
EDIT
I eventually incorporated a telnet server in my cli library. You can find the result on GitHub: https://github.com/daniele77/cli
You can use ncat (from nmap package: apt install nmap) for that as well as follow:
ncat -lnvp 443 -e myapp
don't forget to fflush(stdout); after each printf("%s",str); in your app
Just found this trick reading man nc.
One could redirect both stdin and stdout to the same app using named pipes.
Example:
# create named pipe
rm -f /tmp/f; mkfifo /tmp/f
# launch interactive 'sh' session redirected to/from listening nc
cat /tmp/f | /bin/sh -i 2>&1 | nc -l 1234 > /tmp/f
From another terminal:
nc localhost 1234 # connect to the listening nc
$
$
$ ls -lah
total 16K
drwxrwxr-x 43 ky ky 12K Jun 17 01:31 .
drwxr-xr-x 123 ky ky 12K Jun 17 01:35 ..
Oh my, it really works. And if you don't mind keeping all the sent data in a file then this approach could also be using a file instead of a named pipe. Like this:
# create an empty file
echo -n '' > /tmp/session
# launch interactive 'sh' session redirected to/from listening nc
tail -f /tmp/session | /bin/sh -i 2>&1 | nc -l 1234 > /tmp/session
Related
I have an embedded linux application with a simple interactive command line interface.
I'd like to access the command line from telnet (or network, in general).
However, the process should be started when the board turns on, and in a unique instance. So, the following netcat command is not an option:
nc -l -p 4000 -e myapp
I can do
nc -l -p 4000 | myapp
to send remote commands to myapp, but this way I can't see myapp output.
Is there any way to redirect both stdin and stdout to netcat?
Thanks.
Is there a way to redirect both stdin and stdout to netcat
There is socat, which is a more advanced netcat. You can redirect both stdin and stdout with it. E.g.:
socat TCP4-LISTEN:5556,reuseaddr,fork EXEC:"cat - /etc/redhat-release"
In the above cat reads stdin and /etc/redhat-release and outputs them into stdout.
And then try using that:
$ echo "hello" | nc 127.0.0.1 5556
hello
Fedora release 22 (Twenty Two)
$ echo "hello 2" | nc 127.0.0.1 5556
hello 2
Fedora release 22 (Twenty Two)
I found that by using bash v. >= 4.0 I can use coproc:
#!/bin/bash
coproc myapp
nc -kl -p 4000 <&"${COPROC[0]}" >&"${COPROC[1]}"
EDIT
I eventually incorporated a telnet server in my cli library. You can find the result on GitHub: https://github.com/daniele77/cli
You can use ncat (from nmap package: apt install nmap) for that as well as follow:
ncat -lnvp 443 -e myapp
don't forget to fflush(stdout); after each printf("%s",str); in your app
Just found this trick reading man nc.
One could redirect both stdin and stdout to the same app using named pipes.
Example:
# create named pipe
rm -f /tmp/f; mkfifo /tmp/f
# launch interactive 'sh' session redirected to/from listening nc
cat /tmp/f | /bin/sh -i 2>&1 | nc -l 1234 > /tmp/f
From another terminal:
nc localhost 1234 # connect to the listening nc
$
$
$ ls -lah
total 16K
drwxrwxr-x 43 ky ky 12K Jun 17 01:31 .
drwxr-xr-x 123 ky ky 12K Jun 17 01:35 ..
Oh my, it really works. And if you don't mind keeping all the sent data in a file then this approach could also be using a file instead of a named pipe. Like this:
# create an empty file
echo -n '' > /tmp/session
# launch interactive 'sh' session redirected to/from listening nc
tail -f /tmp/session | /bin/sh -i 2>&1 | nc -l 1234 > /tmp/session
I put netcat -l -p 80 into terminal. Then open http://localhost/ in web browser. Now netcat shows the header of the request in stdout. All fine.
But if I use netcat with the -c option netcat does not print anymore to stdout.
How can I get netcat -l -p 80 -c uname to print to stdout?
Maybe via the -o file option via file descriptor?
After this
Redirect process stdin and stdout to netcat
and that
How to print text while netcat is listening at a port?
I installed sudo apt install ncat
and this solves my problem:
ncat -l -p 80 <<< "hello"
I would like my script to run in the backround, SSH into another computer, run tcpdump, produce a pcap file and save it to my local computer. I have all of this working save for the running in the background portion.
I have looked at several solutions on Stack Overflow (example) but they don't seem to work for me. Admittedly I am a novice with bash however so it is entirely possible that I am reading them incorrectly.
ssh root#ipaddress "tcpdump -c 400000 -s 0 -U -n -w - -i eth0 not arp" &>/dev/null &disown \ > /root/Destop/BashPcap/01Bash.pcap
Check your quotation endings maybe that's the problem...
Or you can save the file remotely and download back using scp (SecureCoPy).
Eg:
scp root#ipaddress:/path/to/file ~/Documents/path-where you-want-to-save.pcap
As far as I understood your task this is what you want:
nohup ssh root#ipaddress "tcpdump -c 400000 -s 0 -U -n -w - -i eth0 not arp" &> /root/Destop/BashPcap/01Bash.pcap &
In simple words:
nohup - it will allow you to close your terminal and the script will continue to run
ssh ... - this is the command to execute
&> - redirect both stdout and stderr to file (Bash 4)
& - sends command to the background
Note: &> will send to the file both stdout and stderr, you need this if you want to have in your file the summary lines from tcpdump. They are written to stderr:
N packets captured
X packets received by filter
Y packets dropped by kernel
If you do not want to have these lines, then send stderr to /dev/null
nohup ssh root#ipaddress "tcpdump -c 400000 -s 0 -U -n -w - -i eth0 not arp" 2>/dev/null > /root/Destop/BashPcap/01Bash.pcap &
I tried this command to try to find unused ports. What I want it to do is run netcat on a range of ports, find the ones that are not running services, grep the first one of those lines of output, and then use sed to output the port number only.
nc -z <my url> 5000-5010 | grep -m 1 succeeded | sed 's/[^0-9]//g'
But when I try and launch a service using the port that is returned, I get a message saying the port is currently in use.
I found out netcat success means a service is running on the port, but when I try this instead
nc -zv <my url> 5000-5010 | grep -m 1 failed | sed 's/[^0-9]//g'
I get nothing, even though most lines of output contain the word failed.
Going through the man pages revealed that netcat -z only returns output for successful results, though why line after line of failed connection appears on my terminal window is still beyond me.
How can I use netcat to view the first port a connection failed on?
To get a list of closed (unused) ports on a linux system you can use:
Realtime Output:
#!/bin/bash
remoteHost=stackoverflow.com
for i in {80..100}
do
nc -v -z -w 1 $remoteHost $i &> /dev/null && echo "Port $i Open" || echo "Port $i Closed"
done
You can change the timeout, currently set to 1 sec (-w 1), to a higher value if needed.
Output:
Port 80 Open
Port 81 Closed
Port 82 Closed
Port 83 Closed
Port 84 Closed
Port 85 Closed
etc..
nc arguments:
-v Have nc give more verbose output.
-z Specifies that nc should just scan for listening daemons, without sending any data to them. It is an error to use this option in conjunction with the -l option.
-w timeout
If a connection and stdin are idle for more than timeout seconds, then the connection is silently closed. The -w flag has no effect on the -l option, i.e. nc will listen forever for a connection, with or without the -w flag. The default is no timeout.
Resources
nc man
The nc -v command writes the failed/succeeded messages on standard error, not the standard output. You can redirect stderr to stdout using 2>&1.
nc -zv <my url> 5000-5010 2>&1 | grep -m 1 failed
to get the failed line.
See http://www.cyberciti.biz/faq/redirecting-stderr-to-stdout/
By the way, I suggest you use awk to get the port number from the output line:
nc -zv <my url> 5000-5010 2>&1 | grep -m 1 failed | awk '{print $6}'
which prints the value in the 6th column of the output line.
How can i have a logs on TCP port available, so that it can be remotely tested by someone else ? for example:
MAINSERVER> tail -f /etc/httpd/logs/access_log | grep -e fruit_Python -e fruit_BASH -e fruit_C | .... TCP 9999 ... make this available ....??
NOW, from my Laptop remotely i want to do this temporary:
MYLAPTOP> tail -f http://MAINSERVER:9999 | grep -e grab_BASH
Any idea please?
You can use netcat (nc) to do this:
Server side (listen for connection):
tail -f /foo/bar |nc -l -k -p 9999
-l listen
-k listen for another connection after current completed
Client side (connecting):
nc MAINSERVER 9999 | grep whatever_you_like
You can use bash as well to connect to /dev/tcp/host/port but sometimes it's not suported (compiled in to Bash) for security reasons.
Client:
grep whatever_you_like < /dev/tcp/MAINSERVER/9999