0
私は与えられたインターフェイスのトラフィックをキャプチャするスクリプトを書いています。しかし、プログラムの実行中に、私はちょうどメッセージを得ることは言う:Pythonプログラムからインデントアウトされたputを取得できません
"Process finished with exit code 0"
私のコードの下に注意してください。
import socket
from struct import *
import datetime
import pcapy
import sys
def main(argv):
devices = pcapy.findalldevs()
print (devices)
for d in devices:
print "Available devices are ", d
#Device to be sniffed
input_dev = raw_input("Enter deice name to sniff")
print "Following device would be printed", input_dev
#open
#device
# Arguments here are:
# device
# snaplen (maximum number of bytes to capture _per_packet_)
# promiscious mode (1 for true)
# timeout (in milliseconds)
cap = pcapy.open_live(input_dev,65536,1,0)
#Interating all packets captured in to a variable
while(1):
(header, packet) = cap.next()
parse_packet(packet)
#Convert a string of 6 characters of ethernet address into a dash separated hex string
def eth_addr(a):
b = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x" % (ord(a[0]), ord(a[1]), ord(a[2]), ord(a[3]), ord(a[4]), ord(a[5]))
# Parsing captured packets
def parse_packet(packet):
eth_length = 14
# Get the protocol used from the captured packet
eth_header = packet[:eth_length]
eth = unpack('!6s6sH', eth_header)
eth_protocol = socket.ntohs(eth[2])
print 'Destination MAC : ' + eth_addr(packet[0:6])+'Source MAC: ' + eth_addr(packet[0:12]) + 'Protocol: ' +str(eth_protocol)
# Parse the IP packet
#
if eth_protocol == 8:
#Parse IP header
#take first 20 characters for the ip header
ip_header = packet[eth_length:20+eth_length]
#Now unpack them
iph = unpack('!BBHHHBBH4s4s', ip_header)
version_ihl = iph[0]
version = version_ihl >> 4
ihl = version_ihl & oxf # need to know what is the oxf
iph_length = ihl * 4
ttl = iph[5]
print "ttl value is = ", str(ttl)
protocol = ip[6]
print "protocol = ", str(protocol)
s_addr = socket.inet_ntoa(iph[8]);
print "source address = ", str(s_addr)
d_address = socket.inet_ntoa(iph[9])
print "Destination address = ", str(d_address)
#Parsing TCP header
if protocol == 6:
#if protocol = 6, it would be TCP
t = iph_length + eth_length
tcp_heder = packet
#Unpack the packet
tcph = unpack('!HHLLBBHHH', tcp_heder)
source_port = tcph[0]
print "Source port =", source_port
dst_port = tcph[1]
print "Dst port =", dst_port
ack_no = tcph[3]
print "ack_No = ", ack_no
doff_deserverved = tcph[4]
print "doff_deserved = ", doff_deserverved
tcp_length = doff_deserverved >> 4
h_size = eth_length + iph_length + tcp_length * 4
data_size = len(packet) - h_size
#get the data from the packet
data = packet[data_size:]
print "Data is = ", data
elif protocol == 1:
u = iph_length + eth_length
icmp_length = 4
icmp_header= packet[u:u+4]
#Now unpack them
icmp = unpack('!BBH' , icmp_header)
icmp_type = icmp[0]
print "Type is = ", icmp
code = icmp[1]
print "Code is = ", code
checkSum = icmp[3]
print "Check sum is ", checkSum
h_size = eth_length + iph_length + icmp_length
data_sze = len(packet)- h_size
print "Data size = ", data_size
#Get Data from the packet
data2 = packet[h_size:]
print "Data is = ", data2
#Parsing UDP packet
elif protocol == 17:
u = iph_length + eth_length
udph_leungth = 8
udp_header = packet[u:u+8]
#Now unpack them
udp_packet = unpack('!HHHH', udp_header)
s_port = udp_packet[0]
print "Source Port = ", s_port
d_port = udp_packet[1]
print "Udp Packet = ", d_port
lenth_of_packet = udp_packet[2]
print "Length of packet = ", lenth_of_packet
check_sum = udp_packet[3]
print "check sum is ", check_sum
h_size_of_packet = eth_length + iph_length + udph_leungth
actaul_size = len(packet) - h_size_of_packet
#Retrieving data packet from the size
data = packet[h_size_of_packet]
print "Actual Data = ", data
#IF there any other some protocol
else:
print "Protol other than TCP/UDP/ICMP"
main(sys.argv)
私が出てインデントプットを生成するか、しない理由がどうなるか教えてくださいそれを得るための他の方法。
コードの最後の行にあるインデントを調べることができます。 'main(sys.argv)'は 'main'関数の中にあります。その行のインデントを削除します。 – larsks
他のインデントの問題もあります。 –