(you might have to install the package ip
on openwrt (v12 / attitude adjustment)
ifconfig/netstat etc. are considered deprecated, so you should use (as root)
ss -nlput | grep sshd
to show the TCP/UDP sockets on which a running program which contains the string sshd
is listening to
-n
no port to name resolution
-l
only listening sockets
-p
show processes listening
-u
show udp sockets
-t
show tcp sockets
Then you geht a list like this one:
tcp LISTEN 0 128 *:22 *:* users:(("sshd",3907,4))
tcp LISTEN 0 128 :::22 :::* users:(("sshd",3907,3))
tcp LISTEN 0 128 127.0.0.1:6010 *:* users:(("sshd",4818,9))
tcp LISTEN 0 128 ::1:6010 :::* users:(("sshd",4818,8))
the interesting thing is the 5th column which shows a combination of IP address and port:
*:22
listen on port 22 on every available IPv4 address
:::22
listen on port 22 on every available IP address (i do not write IPv6, as IP is IPv6 per RFC 6540)
127.0.0.1:6010
listen on IPv4 address 127.0.0.1 (localhost/loopback) and port 6010
::1:6010
listen on IP address ::1 (0:0:0:0:0:0:0:1 in full notation, also localhost/loopback) and port 6010
You then want to know which interfaces has an IPv4 address (to cover 1.)
ip -4 a
# or "ip -4 address"
# or "ip -4 address show"
or an IP address (to cover 2.)
ip -6 a
# or "ip -6 address
# or "ip -6 address show
(if you do not add the option for IP (-6
) or IPv4 (-4
) both are shown)
You can also have an look that output and search for e.g. 127.0.0.1
or any other IP/IPv4-address
# here a demo where i show all addresses of the device "lo" (loopback)
ip a show dev lo
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
The lines beginning with inet
and inet6
show that these IPs are bound to this interface, you may have many of these lines per interface:
he-ipv6: <POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1480 qdisc noqueue state UNKNOWN
link/sit 192.0.2.1 peer 192.0.2.3
inet6 2001:db8:12::1/64 scope global
valid_lft forever preferred_lft forever
inet6 2001:db8::2/64 scope global
valid_lft forever preferred_lft forever
inet6 fe80::1111:1111/128 scope link
valid_lft forever preferred_lft forever
and in a script:
address="127.0.0.1"
for i in $(grep ':' /proc/net/dev | cut -d ':' -f 1 | tr -d ' ') ; do
if $(ip address show dev $i | grep -q "${address}") ; then
echo "${address} found on interface ${i}"
fi
done
(replace "127.0.0.1")