Skip to content

Instantly share code, notes, and snippets.

@jt0in3e
Forked from harmonbc/GetPeers.py
Last active December 17, 2018 05:53
Show Gist options
  • Save jt0in3e/867a2380bb64d9596bf704f7a5d47ef7 to your computer and use it in GitHub Desktop.
Save jt0in3e/867a2380bb64d9596bf704f7a5d47ef7 to your computer and use it in GitHub Desktop.
Print out all Torrent IP Addresses
#!/usr/bin/python
#Heavily based on libtorrent example
#Did this to print examples of what information is being processed at different parts of the bittorrent protocol
#added saving to file > jt0in3e
import libtorrent as lt
import time
import sys
class SmallClient:
def __init__(self):
global ses
ses = lt.session()
ses.listen_on(6881, 6891)
def getPeers(self,arg):
#Use bencode to read the contents of the file
e = lt.bdecode(open(arg, 'rb').read())
#Build torrent info into libtorrent
info = lt.torrent_info(e)
#Add the torrent info to the current session
h = ses.add_torrent({'ti':info,'save_path':'./'})
#Print the torrent information
print "File: "+h.name()
print "Hash: "+str(h.info_hash())
print "Trackers: "
t = h.trackers()
for s in t:
print str(s['url'])
raw_input("Press Enter to Continue...")
print "-----PEERS-----"
if len(sys.argv)>2:
filename = sys.argv[2]
else:
filename = "torrent_ip_list_" + time.strftime("%Y%m%d-%H%M%S")
hs = open(filename, "a")
count = 0
while (not h.is_seed()):
#Get peer information from the torrent
p = h.get_peer_info();
#for every item i in the peer list
for i in p:
count+=1
#print their IP address
u, v = i.ip
#print str(u)
hs.write("{}\n".format(u))
print str(count)+") "+str(u)
time.sleep(5)
print "----END---"
hs.close()
#Build the client object
client = SmallClient()
#Run the client so we can see the IP addresses
client.getPeers(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment