Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- from scapy.all import *
- #a = raw_input("Enter ip\n")
- #packet=IP(dst=a)/ICMP(type=8)
- #res = sr(packet, timeout=10)
- #if res[0]:
- # print 'Host is alive'
- #else:
- # print 'Host is not alive'
- def ipFunction(ip):
- mas = ip.split('/')
- if len(mas) == 2:
- mas[1] = int(mas[1])
- if mas[1] <= 0:
- mas[1] = 1
- if mas[1] < 24:
- mas[1] = 24
- return mas[0], (2**(32-mas[1])-2)
- else:
- return mas[0], 1
- class Interpreter(object):
- def __init__(self, text):
- self.text = text
- self.tokens = []
- def error(self):
- raise Exception('Error parsing input')
- def expr(self):
- self.tokens = self.text.split(" ")
- #return self.tokens
- if self.tokens[0] == "discovery":
- if self.tokens[1] == "--icmp":
- ip, netMask = ipFunction(self.tokens[2])
- ipAddr = []
- i = 0
- while i < netMask:
- if netMask > 1:
- ipS = ip.split('.')
- ipS[3] = str(int(ipS[3])+1)
- ip='.'.join(ipS)
- packet=IP(dst=ip)/ICMP(type=8)
- res=sr(packet, timeout=1, verbose=0)
- if res[0]:
- print 'Host ' + ip + ' is alive'
- ipAddr.append(ip)
- else:
- print 'Host ' + ip + ' is not alive'
- i = i + 1
- return ipAddr
- if self.tokens[1] == "--tcp":
- if self.tokens[2] == "--ports":
- ports = self.tokens[3].split(",")
- ip = self.tokens[4]
- for i in ports:
- packet=IP(dst=ip)/TCP(dport=int(i),flags="A")
- res=sr(packet, timeout=5, verbose=0)
- if res[0]:
- print 'Host '+ ip + ' is alive'
- return
- print "Host "+ ip+ " is not alive"
- else:
- port = 80
- ip = self.tokens[2]
- packet=IP(dst=ip)/TCP(dport=port,flags="A")
- res=sr(packet, timeout=5, verbose = 0)
- if res[0]:
- print 'Host '+ ip + ' is alive'
- print "Host "+ ip+ " is not alive"
- if self.tokens[1] == "--udp":
- if self.tokens[2] == "--ports":
- ports = self.tokens[3].split(",")
- ip = self.tokens[4]
- for i in ports:
- packet=IP(dst=ip)/UDP(dport=int(i))
- res=sr(packet, timeout=5, verbose = 0)
- if res[0]:
- print 'Host '+ ip + ' is alive'
- return
- print "Host "+ ip+ " is not alive"
- else:
- port = 40125
- ip = self.tokens[2]
- packet=IP(dst=ip)/UDP(dport=port)
- res=sr(packet, timeout=10, verbose = 0)
- if res[0]:
- print 'Host '+ ip + ' is alive'
- return
- print "Host "+ ip+ " is not alive"
- # if self.tokens[1] == "--arp":
- # ip = self.tokens[2]
- # packet=IP(dst=ip)/ARP()
- # res=sr(packet, timeout=5)
- # if res[0]:
- # return 'Host ' + ip + ' is alive'
- # return 'Host ' + ip + ' is not alive'
- if self.tokens[1] == "--arp":
- ip, netMask = ipFunction(self.tokens[2])
- ipAddr = []
- i = 0
- while i < netMask:
- if netMask > 1:
- ipS = ip.split('.')
- ipS[3] = str(int(ipS[3])+1)
- ip='.'.join(ipS)
- packet=IP(dst=ip)/ARP()
- res=sr(packet, timeout=1, verbose = 0)
- if res[0]:
- print 'Host ' + ip + ' is alive'
- ipAddr.append(ip)
- else:
- print 'Host ' + ip + ' is not alive'
- i = i + 1
- return ipAddr
- return self.error
- def main():
- while True:
- try:
- text = raw_input('scapy_nmap> ')
- except EOFError:
- break
- if not text:
- continue
- interpreter = Interpreter(text)
- result = interpreter.expr()
- #print result
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement