Set source port when sending udp message with echo? - bash

I'm using
echo "This is my data" > /dev/udp/192.168.0.92/7891
in a bash script to test udp communication with a device. The device then returns the message. I've seen via wireshark that my source port is always changing.
Anyway, I want to set the source port, can I do that?

Use netcat nc and its -p option to set the source port.
As said in the netcat man page
-p source_port
Specify the source port nc should use, subject to privilege restrictions and availability.
Then use it like this:
echo "This is my data" | nc -u -p 50000 192.168.0.92 7291

Related

netcat windows listen for password on UDP

I want to use netcat to open a listener on an UDP port using netcat (any other tool is welcome as well, but it's the one I have found to be useful for now), which is listening for a password to be entered to execute another action.
Here is what I have so far, but it doesn't work as desired:
ncat.exe -kluvp 4444 -c "set /P pwd=pwd: & if %pwd%==pwd (echo yes) else (echo no)
But when I connect from the client, ncat -4u -w1 localhost 4444, with whatever I write as input I get yes, but it should only print yes when I write pwd.

Connecting to an open port with bash

Is there a possible way to connect to any open port with bash ? i.e without calling any external command. :)
I wanted to connect to an open port with bash without using nc or telnet.
It depends on the shell; bash, for instance, uses I/O redirection to attach to arbitrary TCP and UDP sockets. From the man page:
Bash handles several filenames specially when they are used in redirections, as
described in the following table:
[...]
/dev/tcp/host/port
If host is a valid hostname or Internet address, and port is
an integer port number or service name, bash attempts to open a
TCP connection to the corresponding socket.
/dev/udp/host/port
If host is a valid hostname or Internet address, and port is
an integer port number or service name, bash attempts to open a
UDP connection to the corresponding socket.
For example, to get the current time:
cat < /dev/tcp/time-a.nist.gov/13
Note that it must be a redirection; cat /dev/tcp/time-a.nist.gov/13 would only work if your file system implemented some sort of sockets-on-demand.
A very basic HTTP client:
$ exec 3<>/dev/tcp/www.example.com/80
$ echo "GET /" >&3
$ cat <&3

Telnet - Connect and send message in one line

I'm looking for a one-line command that will connect to a telnet server (no authentication needed, it will accept any connection) and send text to the server after connection.
If this is not possible, is it possible to write a script that will do the same thing?
With bash: replace "servername" by your telnet server:
echo "Hello" > /dev/tcp/servername/23
Sounds like you can use Netcat on the telnet port.
An example would go like so:
echo -n 'some string to telnet' | nc <server> 23

vi over netcat session

Is it possible to use vi over netcat?
server:
mkfifo tun; sh tun | netcat -l 4444 > tun
client:
netcat SERVER_IP 4444
Will gave me remote shell, but it's a problem to send special hot-keys, for example I can't push ESC to enter "normal mode" in vi.
Or best choice will be sed ?
This command runs input from nc as a script, and it will fail for the same reason why this script won't edit a file:
#!/bin/sh
vi file
42G
dd
:wq
You can instead, ironically, use script to avoid running it as a script, and instead get a terminal session to interact with:
server$ mkfifo tun; script -q < tun | netcat -l 4444 > tun
(some netcats require -p before the port above)
Additionally, you should disable local echo and line buffering so that keys pass through the connection immediately rather than when pressing enter:
client$ stty -icanon -echo; nc localhost 4444
You should now be able to edit files in vi.
This is obviously a neat proof of concept only. Non-root users who want to provide robust shell access over the network should use sshd.

hot to get memcached stats without nc?

this is how I'm getting the stats now:
echo -e "stats\nquit" | nc 127.0.0.1 11211
I can't use expect as it's not part of a default installation.
Is there a way to get memcached stats without nc?
Your question doesn't specify why you're looking for an alternative to netcat, so it's hard to to tell what you're looking for. You could do it in bash like this:
exec 3<>/dev/tcp/127.0.0.1/11211
echo -e "stats\nquit" >&3
cat <&3
You could do it using telnet:
(echo -e 'stats\nquit'; sleep 1) | telnet localhost 11211
The sleep is to precent telnet from exiting before receiving a response from memcached.
You could also write something simple in python or perl or some other high level scripting language. Or brush up on your c. There are lots of options.
Another, possibly simpler way, is with the memcached-tool script. It came installed with my installation of memcached 1.4.5 via yum, but under apt and ubuntu I didn't get it. I found it here and put it on my system: https://raw.githubusercontent.com/memcached/memcached/master/scripts/memcached-tool
on the server, type the following to get memcached stats:
memcached-tool 127.0.0.1:11211 stats
or the following to get slabs:
memcached-tool 127.0.0.1:11211
assuming your server is listening on port 11211 and IP 127.0.0.1 (set config options at /etc/sysconfic/memcached)
article: http://www.cyberciti.biz/faq/rhel-fedora-linux-install-memcached-caching-system-rpm/

Resources