1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
public class PortScanner extends Frame{
Socket s;
int startPort,endPort;
Label l1;
TextField hostName,fromPort,toPort;
TextArea log;
Label host,from,to,label;
Button scan,reset;
int fp,tp;
String h;
static boolean close=false;
Dialog d;
public PortScanner(){
Frame f=new Frame("Port Scanner");
f.pack();
f.setSize( 500,400);
f.setLocationRelativeTo(null);
f.setVisible(true);
f.setLayout(new GridLayout(5,1));
l1=new Label("Port Scanner",Label.CENTER);
l1.setForeground(Color.red);
l1.setFont(new Font("TimesRoman",Font.BOLD,25));
f.add(l1);
Panel p1=new Panel(new GridLayout(3,3));
host=new Label("Adresse IP:");
host.setForeground(Color.blue);
p1.add(host);
hostName=new TextField(15);
p1.add(hostName);
from=new Label("Port Début:");
from.setForeground(Color.blue);
p1.add(from);
fromPort=new TextField(15);
p1.add(fromPort);
to=new Label("Port Fin:");
to.setForeground(Color.blue);
p1.add(to);
toPort=new TextField(15);
p1.add(toPort);
f.add(p1);
Panel p2=new Panel();
scan=new Button("Scan");
scan.addActionListener(new ActionListener(){
public static boolean isIp(String ip)
{
String[] hostName = ip.split("\\.");
boolean isIp = Boolean.TRUE;
try {
if (hostName.length!=4) return false;
int digitIp =0;
for (int i=0;i<hostName.length;i++)
{
digitIp = Integer.parseInt(hostName[i]);
if (digitIp<0 || digitIp>255)
isIp = Boolean.FALSE;
}
} catch (Exception e) {
isIp = Boolean.FALSE;
}
return isIp;
}
public void actionPerformed(ActionEvent r){
if(hostName.getText().equals("")){//log.setText("Veuillez remplir tous les champs");
JOptionPane.showMessageDialog(null, "Veuillez remplir tous les champs");
return;
}
else if (fromPort.getText().equals("")){//log.setText("Veuillez remplir tous les champs");
JOptionPane.showMessageDialog(null, "Veuillez remplir tous les champs");
return;}
else if(toPort.getText().equals("")){//log.setText("Veuillez remplir tous les champs");
JOptionPane.showMessageDialog(null, "Veuillez remplir tous les champs");
return;}
close=false;
Thread run1=new Thread(){
public void run(){
scan.setEnabled(false);
reset.setLabel("Stop");
log.setText("");
log.repaint();
h=hostName.getText();
fp=Integer.parseInt(fromPort.getText());
tp=Integer.parseInt(toPort.getText());
for(int i=fp;i<=tp;i++){
label.setText("Port "+i+" is being tested");
if(close)
break;
try{
s=new Socket(h,i);
log.append("Port "+i+" is open."+"\n");
log.repaint();
s.close();
}
catch(Exception er){continue;}
}
scan.setEnabled(true);
reset.setLabel("Reset");
label.setText("Press Scan to start.");
}
};
run1.start();
}
});
reset=new Button("Reset");
reset.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent t){
Thread run2=new Thread(){
public void run(){
close=true;
hostName.setText("");
fromPort.setText("");
toPort.setText("");
}
};
run2.start();
}
});
Label empty=new Label();
p2.add(scan);
p2.add(empty);
p2.add(reset);
f.add(p2);
log=new TextArea();
f.add(log);
label=new Label("Press Scan to start.");
label.setForeground(Color.blue);
f.add(label);
f.setSize(320,460);
//f.show(true);
f.repaint();
f.setResizable(false);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(1);
}
});
}
public static void main(String args[]){
new PortScanner();
}
}// end of class |
Partager