概述
由于项目原有一些java自动化测试脚本,后续添加时还是优先考虑原框架。
但java在发包时不能灵活设置五元组,因此还是需要调用scapy。
有两种方法:1是使用jpython, 2是把scapy 当成shell命令来执行。
本文使用的是第二种方法。
python脚本
#!/bin/env python
# -*- coding: utf-8 -*-
"""define send packet method , such as : send_arp_request, send_l2_packet, send_l3_packet
"""
__author__ = 'godlaughing'
__date__ = '2013-11-20'
import sys
import types
from scapy.all import *
default_iface = "eth1"
default_inter = 0
default_count = 3
def send_arp_request(sender_mac,sender_ip,target_ip,iface=default_iface,
inter=default_inter,count=default_count):
#iprint "this is send_arp_request %s %s %s " % (sender_mac,sender_ip,target_ip)
packet = Ether(dst="ff:ff:ff:ff:ff:ff", src=sender_mac
)/ARP(hwsrc=sender_mac, psrc=sender_ip, pdst=target_ip)
sendp(packet, iface=iface, inter=inter, count=count)
def send_l2_packet(dmac,smac,iface=default_iface,
inter=default_inter,count=default_count,dip="1.1.1.1",sip="2.2.2.2"):
#print "this is send_l2_packet %s %s %s " % (dmac,smac,iface)
packet = Ether(dst=dmac, src=smac)/IP(dst=dip, src=sip)
sendp(packet, iface=iface, inter=inter, count=count)
def send_l3_packet(dip,sip,iface=default_iface,
inter=default_inter,count=default_count):
""" if you want to send multi sip, use "ip/mask" """
#print "this is send_l3_packet %s %s %s " % (dip,sip,iface)
packet = IP(dst=dip, src=sip)
send(packet, iface=iface, inter=inter, count=count)
if __name__ == "__main__":
command = sys.argv[1]
para = ""
for i in range(2,len(sys.argv)):
temp = sys.argv[i]
try:
int(temp)
except:
temp = '"' + temp + '"'
para += temp
para += ","
command = command + "(" + para + ")"
eval(command)JAVA脚本
public void send_packet(String ip, String command) throws Exception {
# ip is the pc where the command exec, it need ssh
String ScriptPath = "/root/send.py ";
command = "python " + ScriptPath + command;
Process proc =Runtime.getRuntime().exec(command);
}
send_packet("5.5.5.5","send_l2_packet ff:ff:ff:ff:ff:ff 00:ff:ff:ff:ff:99");
本文介绍了如何在Java自动化测试框架中,通过Python脚本和Scapy库灵活配置五元组,以解决Java在发包时的限制问题。详细阐述了两种方法的应用,并提供了具体的Python脚本示例。
877

被折叠的 条评论
为什么被折叠?



