Skip to content

Instantly share code, notes, and snippets.

@tuxmartin
Last active May 30, 2016 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuxmartin/e951a52cdb6fff6e14b450e6625c0092 to your computer and use it in GitHub Desktop.
Save tuxmartin/e951a52cdb6fff6e14b450e6625c0092 to your computer and use it in GitHub Desktop.
Zjisteni IP pres telnet

Pouziti

nc (netcat)

martin@martin:~$ nc ip.vancl.eu 1234
77.78.90.200
martin@martin:~$

Busybox telnet client (OpenWrt, Ubiquity)

martin@martin:~$ /bin/busybox telnet ip.vancl.eu 1234
77.78.90.200
Connection closed by foreign host
martin@martin:~$ 

wget

martin@martin:~$ wget http://ip.vancl.eu:1234/ -qO -
77.78.90.200
martin@martin:~$

martin@martin:~$ wget ip.vancl.eu:1234 -qO -
77.78.90.200
martin@martin:~$

Ubuntu telnet client

martin@martin:~$ telnet ip.vancl.eu 1234
Trying ip.vancl.eu...
Connected to ip.vancl.eu.
Escape character is '^]'.
77.78.90.200
Connection closed by foreign host.
martin@martin:~$

Spusteni serveru/kompilace

Spusteni z pythonu

$ python getip.py

Kompilace

$ cython --embed getip.py 
$ gcc getip.c -I/usr/include/python2.7 -lpython2.7 -o getip
$ strip getip
$ du -sh getip
59K	getip
$ file getip
getip: ELF 64-bit LSB  executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=12166ba5208e49ff66eb73820025b860baedc78b, stripped
$
$ ./getip
Using TCP port 1234.
import socket
import thread
import sys
# --------------------
port = 1234
host = '0.0.0.0'
# --------------------
print("Using TCP port %s." % (port))
buf = 1024
addr = (host, port)
NL = '\r\n'
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
serversocket.bind(addr)
serversocket.listen(500)
clients = [serversocket]
def handler(clientsocket, clientaddr):
print("Accepted connection from: ", clientaddr)
clientsocket.settimeout(2)
try:
data = clientsocket.recv(buf)
if "GET" in data or "Host:" in data or "User-Agent" in data: # HTTP request
clientsocket.send("HTTP/1.0 200 OK" + NL)
clientsocket.send("Content-type: text/plain; charset=us-ascii" + NL)
clientsocket.send("Connection: close" + NL)
clientsocket.send(NL)
clientsocket.send(str(clientaddr[0]) + NL)
except socket.timeout: # Telnet request
clientsocket.send(str(clientaddr[0]) + NL)
except socket.error: # error
print("Send to %s error: " % clientaddr)
finally:
clients.remove(clientsocket)
clientsocket.close()
while True:
try:
#print("Server is listening for connections\n")
clientsocket, clientaddr = serversocket.accept()
clients.append(clientsocket)
thread.start_new_thread(handler, (clientsocket, clientaddr))
except KeyboardInterrupt: # Ctrl+C # FIXME
print("Closing server socket...")
serversocket.close()
#!/bin/bash
while true; do
/usr/local/bin/getip
sleep 2
done
# /usr/local/bin/getip_start
root@server:/home/martin# cat /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# ...
nohup su - XXX -c "/usr/local/bin/getip_start" &
# XXX je jmeno nejakeho ne-root uzivatele, pod kterym to pobezi
# ...
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment