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
Related
I’m looking for a solution to send the data from a file to the TCP port which is available on the specific server using the shell script/Unix command.
When I checking for this option I found the below one but it’s not working out? Is there any solution is available?
The command I tried from a shell script and find the response below.
Option 1: exec cat /home/data/load.txt > /dev/tcp/$host/$port
-bash: : Name or service not known
-bash: /dev/tcp//: Invalid argument
Option 2:
ncat -v -w 2 hostname 4053 < /home/data/test.txt
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to xx.xx.xx.xx:4053.
Ncat: Connection reset by peer.
It would be helpful if could someone help with this.
Thanks | SP
I wrote a bash script for a simple scanner. It asks the user for the first and last IP addresses as well as the port number then stores them into variables. It then uses nmap to scan the range of ip addresses for open MySQL ports.
Here is my code
#!/bin/bash
echo "What is the first IP address?"
read firstIP
echo "What is the last IP address?"
read lastIP
echo "What port number do you want to scan?"
read port
nmap -sT $firstIP-$lastIP -p $port >/dev/null -oG MySQLscan
cat MySQLscan | grep open MySQLscan2
cat MySQlscan2
For the first prompt, I entered 192.168.181.0. For the second prompt, I entered 192.168.181.255. For the port number, I entered 3306. However, the result I got was:
Failed to resolve "192.168.181.0-192.168.181.255"
WARNING: No targets were specified, so 0 hosts scanned.
Why is it failing to resolve the IP addresses?
In nmap, you should specify that range of IP addrs in one the of following forms:
192.168.181.0/24
192.168.181.0-255
Thus, the correct way is either <start_ip_addr>/<subnet_mask> (will scan all the IPs under that subnet, starting from the provided one) or <start_ip_addr>-<last_term_in_subnet_range> (will scan up to the last given term, starting from the provided IP).
I have a server that takes whatever data is sent to it and sends it back. With netcat on linux I am able to exectue the nc ip port command with an additional "argument" in a single line:
python -c print("Hello!") | nc ip port
This is equivalent to first running nc ip port to connect to the server, and later typing Hello! in the opened prompt to send the data.
While I know how to pass an output from a script to another script in windows (ex. here), I am fairly unsure how to achieve a similar result using telnet, as telnet ip port opens its own prompt (which means disabling windows commands).
I would like to know if it is at all possible with telnet to achieve what piping in linux does, or if there is a similar, minimalistic software that I could use on windows.
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
I have an app, on my iphone, which allows me to transfer songs too and from my linux desktop via ftp. The app indicates that the host address is 192.168.0.4 and the port to be used is 5021. I can connect fine to the device from filezilla but when I try to connect via the command line it fails with the error mentioned in the title.
The command i'm using is ftp 192.168.0.4:5021 but it fails. I'm totally stumped. When I search for help on the internet the answer is to simply do what I've done already.
Does anyone have an any idea on what to do?
The ftp command does not support the :5021 syntax for port numbers. Instead, there must be another way.
With some FTP clients, you can use a -P command line option:
ftp -P 5021 192.168.0.4
With others, you need to put the port number after the host address with a space:
ftp 192.168.0.4 5021
Use man ftp to see which yours supports.
add domain and ip to hosts file of you computer.