1 2 23 24 package net.fenyo.gnetwatch.actions; 25 26 import net.fenyo.gnetwatch.*; 27 import net.fenyo.gnetwatch.GUI.GUI; 28 import net.fenyo.gnetwatch.actions.Action.InterruptCause; 29 import net.fenyo.gnetwatch.activities.Background; 30 import net.fenyo.gnetwatch.data.*; 31 import net.fenyo.gnetwatch.targets.*; 32 33 import java.io.*; 34 import java.util.regex.Matcher ; 35 import java.util.regex.Pattern ; 36 37 import org.apache.commons.logging.Log; 38 import org.apache.commons.logging.LogFactory; 39 import org.eclipse.swt.SWT; 40 import org.eclipse.swt.widgets.MessageBox; 41 42 48 49 51 public class ActionNmap extends Action { 52 private static Log log = LogFactory.getLog(ActionNmap.class); 53 54 private ExternalCommand cmd_nmap = null; 57 58 private String address = ""; 59 private boolean invoked = false; 60 61 66 public ActionNmap(final Target target, final Background background) { 69 super(target, background); 70 setItem("nmap"); 71 } 72 73 77 public ActionNmap() { 80 setItem("nmap"); 81 } 82 83 88 protected void initialize(final GUI gui) { 89 super.initialize(gui); 90 } 91 92 97 public String getQueueName() { 99 return "nmap"; 100 } 101 102 107 public long getMaxDelay() { 109 return new Long (getGUI().getConfig().getProperty("nmap.timeout")); 110 } 111 112 118 public void interrupt(final InterruptCause cause) throws IOException { 121 if (cmd_nmap != null) { 122 cmd_nmap.end(); 123 if (cause == InterruptCause.timeout) 124 getGUI().appendConsole(getGUI().getConfig().getPattern("nmap_interrupted", address) + "<BR/>"); 125 } 126 } 127 128 135 public void invoke() throws IOException, InterruptedException { 138 if (isDisposed() == true) return; 139 140 super.invoke(); 141 142 if (invoked == true) return; 143 invoked = true; 144 145 try { 146 final String [] cmdLine; 147 148 if (TargetIPv4.class.isInstance(getTarget())) { 149 final TargetIPv4 target = (TargetIPv4) getTarget(); 150 address = target.getAddress().toString(); 151 address = address.substring(1 + address.indexOf('/')); 152 cmdLine = new String [] { "nmap", "-A", address }; 153 } else if (TargetIPv6.class.isInstance(getTarget())) { 154 final TargetIPv6 target = (TargetIPv6) getTarget(); 155 address = target.getAddress().toString(); 156 address = address.substring(1 + address.indexOf('/')); 157 cmdLine = new String [] { "nmap", "-6", "-A", address }; 158 } else return; 159 160 String cmd_line = ""; 161 for (final String part : cmdLine) cmd_line += part + " "; 162 getGUI().setStatus(getGUI().getConfig().getPattern("forking", cmd_line)); 163 164 cmd_nmap = new ExternalCommand(cmdLine, true); 165 try { 166 cmd_nmap.fork(); 167 } catch (final IOException ex) { 168 getGUI().asyncExecIfNeeded(new Runnable () { 169 public void run() { 170 final MessageBox dialog = new MessageBox(getGUI().getShell(), SWT.ICON_ERROR | SWT.OK); 171 dialog.setText(getGUI().getConfig().getString("nmap_error")); 172 dialog.setMessage(getGUI().getConfig().getString("nmap_error_long")); 173 dialog.open(); 174 } 175 }); 176 177 throw ex; 178 } 179 180 final String cmd_output = cmd_nmap.readStdout(); 181 cmd_nmap.end(); 182 183 final Matcher match = Pattern.compile("(\r|\n)Running: (.*)").matcher(cmd_output); 184 if (match.find() == true) if (match.group(2) != null) 185 getParents().get(0).setType(match.group(2)); 186 getTarget().addEvent(new EventNmap(cmd_output)); 187 188 } finally { 189 getGUI().asyncExecIfNeeded(new Runnable () { 190 public void run() { 191 synchronized (getGUI().sync_tree) { 192 removeVisualElements(getParents().get(0)); 193 } 194 } 195 }); 196 } 197 } 198 199 204 protected void disposed() { 205 super.disposed(); 207 208 try { 210 interrupt(InterruptCause.removed); 211 } catch (final IOException ex) { 212 log.error("Exception", ex); 213 } 214 } 215 } 216 | Popular Tags |