├── .gitignore ├── README.md ├── images ├── Thumbs.db └── appearance.PNG ├── manifest.mf ├── pom.xml ├── spy.log └── src └── main ├── java └── com │ └── github │ └── dmtk │ ├── Authentication.java │ ├── BDCOMOlt.java │ ├── DLinkSwitch.java │ ├── EdgeCoreSwitch.java │ ├── Formater.java │ ├── GUI.form │ ├── GUI.java │ ├── Host.java │ ├── MainClass.java │ ├── Modem.java │ ├── MyButtonUI.java │ ├── PingWindow.form │ ├── PingWindow.java │ ├── Pinger.java │ ├── Port.java │ ├── Ssh.java │ ├── StringCrypter.java │ ├── Telnet.java │ ├── Terminal.java │ ├── TextAreaOutputStream.java │ └── Vlan.java └── resources └── log4j2.xml /.gitignore: -------------------------------------------------------------------------------- 1 | Config.xml 2 | temp.out 3 | #Netbeans 4 | 5 | nbproject/private/ 6 | build/ 7 | nbbuild/ 8 | dist/ 9 | nbdist/ 10 | nbactions.xml 11 | nb-configuration.xml 12 | 13 | .nb-gradle/ 14 | 15 | *.class 16 | 17 | # Package Files # 18 | *.jar 19 | *.war 20 | *.ear 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | 25 | #Some files 26 | 27 | *.pdf 28 | *.doc 29 | *.docx 30 | *.djvu 31 | /nbproject/ 32 | /target/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GUI-Telnet-Client 2 | ![alt tag](https://github.com/dmtk/GUI-Telnet-Client/blob/master/images/appearance.PNG) 3 | -------------------------------------------------------------------------------- /images/Thumbs.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmytro-m/GUI-Telnet-Client/e9b50a92ae1f59fc4aa7d52ad532956461cd0cc6/images/Thumbs.db -------------------------------------------------------------------------------- /images/appearance.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmytro-m/GUI-Telnet-Client/e9b50a92ae1f59fc4aa7d52ad532956461cd0cc6/images/appearance.PNG -------------------------------------------------------------------------------- /manifest.mf: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | X-COMMENT: Main-Class will be added automatically by build 3 | 4 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | com.github.dmtk 5 | Telnet 6 | 1.0-SNAPSHOT 7 | jar 8 | 9 | 10 | 11 | commons-net 12 | commons-net 13 | 2.2 14 | 15 | 16 | org.seleniumhq.selenium 17 | selenium 18 | 2.0b1 19 | 20 | 21 | com.jcraft 22 | jsch 23 | 0.1.52 24 | 25 | 26 | org.apache.logging.log4j 27 | log4j-1.2-api 28 | 2.0.1 29 | 30 | 31 | 32 | UTF-8 33 | 1.7 34 | 1.7 35 | 36 | -------------------------------------------------------------------------------- /spy.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmytro-m/GUI-Telnet-Client/e9b50a92ae1f59fc4aa7d52ad532956461cd0cc6/spy.log -------------------------------------------------------------------------------- /src/main/java/com/github/dmtk/Authentication.java: -------------------------------------------------------------------------------- 1 | package com.github.dmtk; 2 | 3 | import java.io.File; 4 | import java.io.FileInputStream; 5 | import java.io.FileNotFoundException; 6 | import java.io.FileOutputStream; 7 | import java.io.IOException; 8 | import java.io.ObjectInputStream; 9 | import java.io.ObjectOutputStream; 10 | import java.io.Serializable; 11 | import org.apache.log4j.Logger; 12 | 13 | public class Authentication implements Serializable { 14 | 15 | private String loginTerminal; 16 | private String passwdTerminal; 17 | private String loginBilling; 18 | private String passwdBilling; 19 | private final static Logger log = Logger.getLogger(Authentication.class); 20 | private byte[] key=new byte[]{35, 56, 20, 40, 25, 97, 47, 28};//key for DES encryption 21 | 22 | Authentication(GUI gui) throws IOException, ClassNotFoundException { 23 | 24 | FileInputStream fis; 25 | try { 26 | fis = new FileInputStream("temp.out"); 27 | ObjectInputStream ois = new ObjectInputStream(fis); 28 | Authentication ted = (Authentication) ois.readObject(); 29 | StringCrypter crypter = new StringCrypter(key); 30 | this.loginBilling = crypter.decrypt(ted.loginBilling); 31 | this.loginTerminal = crypter.decrypt(ted.loginTerminal); 32 | this.passwdBilling = crypter.decrypt(ted.passwdBilling); 33 | this.passwdTerminal = crypter.decrypt(ted.passwdTerminal); 34 | gui.getjTextField9().setText(this.loginBilling); 35 | gui.getjTextField10().setText(this.loginTerminal); 36 | gui.getjPasswordField1().setText(this.passwdBilling); 37 | gui.getjPasswordField2().setText(this.passwdTerminal); 38 | new File("temp.out").delete(); 39 | } catch (FileNotFoundException ex) { 40 | 41 | this.loginBilling = gui.getjTextField9().getText(); 42 | this.loginTerminal = gui.getjTextField10().getText(); 43 | this.passwdBilling = new String(gui.getjPasswordField1().getPassword()); 44 | this.passwdTerminal = new String(gui.getjPasswordField2().getPassword()); 45 | log.error(ex); 46 | } 47 | 48 | } 49 | 50 | public void serialize() throws FileNotFoundException, IOException { 51 | 52 | String tempLoginBilling=this.loginBilling; 53 | String tempLoginTelnet=this.loginTerminal; 54 | String tempPasswdBilling=this.passwdBilling; 55 | String tempPasswdTelnet=this.passwdTerminal; 56 | StringCrypter crypter = new StringCrypter(key); 57 | this.loginBilling = crypter.encrypt(GUI.getInstance().getjTextField9().getText()); 58 | this.loginTerminal = crypter.encrypt(GUI.getInstance().getjTextField10().getText()); 59 | this.passwdBilling = crypter.encrypt(new String(GUI.getInstance().getjPasswordField1().getPassword())); 60 | this.passwdTerminal = crypter.encrypt(new String(GUI.getInstance().getjPasswordField2().getPassword())); 61 | FileOutputStream fos = new FileOutputStream("temp.out"); 62 | ObjectOutputStream oos = new ObjectOutputStream(fos); 63 | oos.writeObject(this); 64 | oos.flush(); 65 | oos.close(); 66 | this.loginBilling=tempLoginBilling; 67 | this.loginTerminal=tempLoginTelnet; 68 | this.passwdBilling=tempPasswdBilling; 69 | this.passwdTerminal=tempPasswdTelnet; 70 | } 71 | 72 | public void repair() throws FileNotFoundException, IOException, ClassNotFoundException { 73 | FileInputStream fis = new FileInputStream("temp.out"); 74 | ObjectInputStream ois = new ObjectInputStream(fis); 75 | Authentication ted = (Authentication) ois.readObject(); 76 | StringCrypter crypter = new StringCrypter(key); 77 | this.loginBilling = crypter.decrypt(ted.loginBilling); 78 | this.loginTerminal = crypter.decrypt(ted.loginTerminal); 79 | this.passwdBilling = crypter.decrypt(ted.passwdBilling); 80 | this.passwdTerminal = crypter.decrypt(ted.passwdTerminal); 81 | ois.close(); 82 | new File("temp.out").delete(); 83 | } 84 | 85 | public String getLoginTerminal() { 86 | return loginTerminal; 87 | } 88 | 89 | public void setLoginTerminal(String loginTerminal) { 90 | this.loginTerminal = loginTerminal; 91 | } 92 | 93 | public String getPasswdTerminal() { 94 | return passwdTerminal; 95 | } 96 | 97 | public void setPasswdTerminal(String passwdTerminal) { 98 | this.passwdTerminal = passwdTerminal; 99 | } 100 | 101 | public String getLoginBilling() { 102 | return loginBilling; 103 | } 104 | 105 | public void setLoginBilling(String loginBilling) { 106 | this.loginBilling = loginBilling; 107 | } 108 | 109 | public String getPasswdBilling() { 110 | return passwdBilling; 111 | } 112 | 113 | public void setPasswdBilling(String passwdBilling) { 114 | this.passwdBilling = passwdBilling; 115 | } 116 | 117 | } 118 | -------------------------------------------------------------------------------- /src/main/java/com/github/dmtk/BDCOMOlt.java: -------------------------------------------------------------------------------- 1 | package com.github.dmtk; 2 | 3 | public class BDCOMOlt implements Modem { 4 | 5 | @Override 6 | public String testCable(String port) { 7 | return ""; 8 | } 9 | 10 | @Override 11 | public String showPorts() { 12 | return "show int brief"; 13 | } 14 | 15 | @Override 16 | public String showMACAddressTable() { 17 | return "show mac address-table "; 18 | } 19 | 20 | public String showMACAddressTable(int eponPort, int olt) { 21 | return "show mac address-table interface epON 0/" + eponPort + ":" + olt; 22 | } 23 | 24 | public String showMACAddressTableMAC(String mac) { 25 | return "show mac address-table " + mac; 26 | } 27 | 28 | public String showMACAddressTableVlan(int vlan) { 29 | return "show fdb vlanid " + vlan; 30 | } 31 | 32 | @Override 33 | public String reload() { 34 | return "reload"; 35 | } 36 | 37 | public String showDhcpRelaySnooping() { 38 | return "show ip dhcp-relay snooping binding all"; 39 | } 40 | 41 | public String showVlan() { 42 | return "show vlan"; 43 | } 44 | 45 | public String showOpticalTxRxDiagnosis() { 46 | return "show epon optical-transceiver-diagnosis"; 47 | } 48 | 49 | public String showOpticalTxRxDiagnosis(int eponPort, int olt) { 50 | return "show epon interface epON 0/" + eponPort + ":" + olt + " onu ctc optical-transceiver-diagnosis "; 51 | } 52 | 53 | public String showEponVlan(int eponPort, int olt) { 54 | return "show epon interface epon 0/" + eponPort + ":" + olt + " onu ctc vlan"; 55 | } 56 | 57 | public String showEponSnmp(int eponPort, int olt) { 58 | return "show epon interface epon 0/" + eponPort + ":" + olt + " onu ctc snmp-info"; 59 | } 60 | 61 | public String rebootOnu(int eponPort, int olt) { 62 | return "epon reboot onu interface epon 0/" + eponPort + ":" + olt; 63 | } 64 | 65 | public String showEponDba(int eponPort, int olt) { 66 | return "show epon interface epon 0/" + eponPort + ":" + olt + " onu ctc dba"; 67 | } 68 | 69 | public String showEponInfo(int eponPort, int olt) { 70 | return "show epon interface epon 0/" + eponPort + ":" + olt + " onu ctc basic-info"; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/com/github/dmtk/DLinkSwitch.java: -------------------------------------------------------------------------------- 1 | package com.github.dmtk; 2 | 3 | public class DLinkSwitch implements Modem { 4 | 5 | @Override 6 | public String testCable(String port) { 7 | return "cable_diag ports " + port; 8 | } 9 | 10 | @Override 11 | public String showPorts() { 12 | return "show ports description"; 13 | } 14 | 15 | @Override 16 | public String showMACAddressTable() { 17 | return "show fdb"; 18 | } 19 | 20 | public String showMACAddressTable(String port) { 21 | return "show fdb" + port; 22 | } 23 | 24 | public String showMACAddressTableMAC(String mac) { 25 | return "show fdb mac " + mac; 26 | } 27 | 28 | public String showMACAddressTableVlan(int vlan) { 29 | return "show fdb vlanid " + vlan; 30 | } 31 | 32 | @Override 33 | public String reload() { 34 | return "reload"; 35 | } 36 | 37 | public String showTrafficSegmentation() { 38 | 39 | return "sh traffic_segmentation"; 40 | } 41 | 42 | public String changePortState(String port, boolean enabled) { 43 | String command = null; 44 | if (enabled) { 45 | command = "config ports " + port + " state enable"; 46 | 47 | } else if (!enabled) { 48 | command = "config ports " + port + " state disable"; 49 | 50 | } 51 | return command; 52 | } 53 | 54 | public String changeSpeed(String port, String speed) { 55 | 56 | String command = null; 57 | if ("Auto".equals(speed)) { 58 | command = "config ports " + port + " speed auto"; 59 | 60 | } else if ("1000 Mbit/s".equals(speed)) { 61 | command = "config ports " + port + " speed 1000_full"; 62 | } else if ("100 Mbit/s - Full".equals(speed)) { 63 | command = "config ports " + port + " speed 100_full"; 64 | 65 | } else if ("100 Mbit/s - Half".equals(speed)) { 66 | command = "config ports " + port + " speed 100_half"; 67 | 68 | } else if ("10 Mbit/s - Full".equals(speed)) { 69 | command = "config ports " + port + " speed 10_full"; 70 | } else if ("10 Mbit/s - Half".equals(speed)) { 71 | command = "config ports " + port + " speed 10_half"; 72 | 73 | } 74 | return command; 75 | } 76 | 77 | public String configAddressBindingZeroIP(String port, boolean enable) { 78 | String command = null; 79 | if (enable) { 80 | command = "config address_binding ip_mac ports" + port + " state enable allow_zeroip enable"; 81 | 82 | } else if (!enable) { 83 | 84 | command = "config address_binding ip_mac ports " + port + " state disable allow_zeroip disable"; 85 | } 86 | return command; 87 | 88 | } 89 | 90 | public String showErrorsOnPort(String port) { 91 | return "show error ports " + port; 92 | } 93 | 94 | public String showPacketsOnPort(String port) { 95 | return "show packet ports " + port; 96 | } 97 | 98 | public String showAddressBindingTable() { 99 | return "show address_binding ip_mac all"; 100 | } 101 | 102 | public String showBandwidthControl() { 103 | return "show bandwidth_control"; 104 | } 105 | 106 | public String configurePortSecurityMaxLearningAddr2(String port) { 107 | return "config port_security ports " + port + " admin_state disable max_learning_addr 2 lock_address_mode DeleteOnTimeout"; 108 | } 109 | 110 | public String configurePortRxRateNoLimit(int port1, int port2) { 111 | return "config bandwidth_control " + port1 + " " + port2 + " rx_rate no_limit"; 112 | } 113 | 114 | public String configurePortTxRateNoLimit(int port1, int port2) { 115 | return "config bandwidth_control " + port1 + " " + port2 + " tx_rate no_limit"; 116 | } 117 | 118 | public String addAddressBindingPort(String ip, String mac, int port) { 119 | return "create address_binding ip_mac ipaddress " + ip + " mac_address " + mac + " ports " + port; 120 | } 121 | 122 | public String removeAddressBindingPort(String ip, String mac) { 123 | return "delete address_binding ip_mac ipaddress " + ip + " mac_address " + mac; 124 | } 125 | 126 | public String showAddressBindingBlocked() { 127 | return "show address_binding blocked all"; 128 | } 129 | 130 | public String addVlanToSwitch(String vlanId, int vlan) { 131 | 132 | return "create vlan " + vlanId + " tag " + vlan; 133 | } 134 | 135 | public String removeVlanFromSwitch(String vlanId) { 136 | return "delete vlan vlanid " + vlanId; 137 | } 138 | 139 | public String addVlanToPort(int port, String vlanId, boolean tagged) { 140 | String command = null; 141 | if (tagged) { 142 | command = "config vlan " + vlanId + " add tagged " + port; 143 | 144 | } else if (!tagged) { 145 | command = "config vlan " + vlanId + " add untagged " + port; 146 | 147 | } 148 | return command; 149 | } 150 | 151 | public String removeVlanFromPort(int port, String vlanId) { 152 | return "config vlan" + vlanId + " delete " + port; 153 | } 154 | 155 | public String showDhcpRelay() { 156 | return "show dhcp_relay"; 157 | } 158 | 159 | } 160 | -------------------------------------------------------------------------------- /src/main/java/com/github/dmtk/EdgeCoreSwitch.java: -------------------------------------------------------------------------------- 1 | package com.github.dmtk; 2 | 3 | public class EdgeCoreSwitch implements Modem { 4 | 5 | private String model = "ES3528";//"ES4210" 6 | 7 | @Override 8 | public String showPorts() { 9 | return "show int brief\n"; 10 | } 11 | 12 | @Override 13 | public String showMACAddressTable() { 14 | return "show mac-address-table"; 15 | } 16 | 17 | @Override 18 | public String reload() { 19 | 20 | return "reload"; 21 | } 22 | 23 | public String showMACAddressTable(String port) { 24 | return "show mac-address-table interface ethernet 1/" + port; 25 | } 26 | 27 | public String showMACAddressTableVlan(String vlan) { 28 | return "show mac-address-table vlan " + vlan; 29 | } 30 | 31 | public String showMACAddressTableMAC(String MAC) { 32 | return "show mac-address-table address " + MAC; 33 | } 34 | 35 | public String showInterfaceStatus(String port) { 36 | return "show interfaces status ethernet 1/" + port; 37 | } 38 | 39 | public String testConfig(String port) { 40 | String command = "configure\n" 41 | + "interface ethernet 1/" + port + "\n" 42 | + "no ip dhcp snooping trust\n" 43 | + "no ip arp inspection trust\n" 44 | + "no ip arp inspection trust\n" 45 | + "switchport mode hybrid\n" 46 | + "switchport acceptable-frame-types all\n" 47 | + "switchport native vlan 2\n" 48 | + "switchport allowed vlan remove 1,10\n" 49 | + "mvr type receiver\n" 50 | + "mvr immediate\n" 51 | + "switchport multicast packet-rate 64\n" 52 | + "ip igmp max-groups 2\n" 53 | + "ip igmp max-groups action replace\n" 54 | + "ip igmp query-drop\n" 55 | + "spanning-tree bpdu-guard\n" 56 | + "spanning-tree bpdu-guard auto-recovery\n" 57 | + "spanning-tree loopback-detection trap\n" 58 | + "end"; 59 | return command; 60 | } 61 | 62 | public String exampleConfig(String port) { 63 | 64 | String command = "configure\n" 65 | + "interface ethernet 1/" + port + "\n" 66 | + "media-type copper-forced\n" 67 | + "port security max-mac-count 2\n" 68 | + "ip source-guard sip-mac\n" 69 | + "no ip dhcp snooping trust\n" 70 | + "no ip arp inspection trust\n" 71 | + "spanning-tree bpdu-guard\n" 72 | + "spanning-tree bpdu-guard auto-recovery\n" 73 | + "spanning-tree loopback-detection trap\n" 74 | + "switchport mode hybrid\n" 75 | + "switchport acceptable-frame-types all\n" 76 | + "switchport native vlan 2\n" 77 | + "ip igmp filter 1\n" 78 | + "ip igmp max-groups 2\n" 79 | + "ip igmp max-groups action replace\n" 80 | + "mvr type receiver\n" 81 | + "mvr immediate\n" 82 | + "ip igmp query-drop\n" 83 | + "switchport multicast packet-rate 64\n" 84 | + "end"; 85 | return command; 86 | } 87 | 88 | public String showCableTestResult() { 89 | return "show cable-diagnostics tdr interface"; 90 | } 91 | 92 | public String showCableTestResult(String port) { 93 | String command = null; 94 | if (model.equals("ES4210")) { 95 | command = "show cable-diagnostics interface ethernet 1/" + port; 96 | } else { 97 | command = "show cable-diagnostics tdr interface ethernet 1/" + port; 98 | } 99 | return command; 100 | } 101 | 102 | @Override 103 | public String testCable(String port) { 104 | String command = null; 105 | if (model.equals("ES4210")) { 106 | command = "test cable-diagnostics interface ethernet 1/" + port; 107 | } else { 108 | command = "test cable-diagnostics tdr interface ethernet 1/" + port; 109 | } 110 | return command; 111 | } 112 | 113 | public String testCable(String firstPort, String lastPort) { 114 | return "test cable-diagnostics tdr interface ethernet 1/" + firstPort + "-" + lastPort; 115 | } 116 | 117 | public String saveConfig() { 118 | return "copy running-config startup-config"; 119 | } 120 | 121 | public String showSystem() { 122 | return "show system"; 123 | } 124 | 125 | public String showArpLog() { 126 | return "show ip arp inspection log"; 127 | } 128 | 129 | public String showArpConfig() { 130 | return "show ip arp inspection configuration"; 131 | } 132 | 133 | public String showArpStat() { 134 | return "show ip arp inspection statistics"; 135 | } 136 | 137 | public String showArpVlan(String vlan) { 138 | return "show ip arp inspection vlan" + vlan; 139 | } 140 | 141 | public String showSourceGuardBindingDhcpSnooping() { 142 | return "show ip source-guard binding dhcp-snooping"; 143 | } 144 | 145 | public String showSourceGuardBindingStatic() { 146 | return "show ip source-guard binding static"; 147 | } 148 | 149 | public void changeModel(String model) { 150 | this.model = model; 151 | } 152 | 153 | public String showMvr() { 154 | return "show mvr"; 155 | } 156 | 157 | public String addVlanToSwitch(String vlan, String name) { 158 | String command = "configure\n" 159 | + "vlan database\nvlan " + vlan + " name " + name + " media ethernet state active\n" 160 | + "end"; 161 | return command; 162 | } 163 | 164 | public String showVlanId(String vlan) { 165 | return "show vlan id " + vlan; 166 | } 167 | 168 | public String changePortSpeed(String speed, String port) { 169 | String command = null; 170 | if ("1000 Mbit/s".equals(speed)) { 171 | command = "configure\ninterface ethernet 1/" + port + "\nspeed-duplex 1000full\nend"; 172 | 173 | } else if ("100 Mbit/s - Full".equals(speed)) { 174 | command = "configure\ninterface ethernet 1/" + port + "\nspeed-duplex 100full\nend"; 175 | 176 | } else if ("100 Mbit/s - Half".equals(speed)) { 177 | command = "configure\ninterface ethernet 1/" + port + "\nspeed-duplex 100half\nend"; 178 | 179 | } else if ("10 Mbit/s - Full".equals(speed)) { 180 | command = "configure\ninterface ethernet 1/" + port + "\nspeed-duplex 10full\nend"; 181 | 182 | } else if ("10 Mbit/s - Half".equals(speed)) { 183 | command = "configure\ninterface ethernet 1/" + port + "\nspeed-duplex 10half\nend"; 184 | 185 | } 186 | return command; 187 | } 188 | 189 | public String showTCAM() { 190 | return "show access-list tcam"; 191 | } 192 | 193 | public String showVlan() { 194 | return "show vlan"; 195 | } 196 | 197 | public String showCPU() { 198 | return "show process cpu"; 199 | } 200 | 201 | public String changePolicy(String port, String policy) { 202 | String command = null; 203 | if ("DEFAULT_POLICY".equals(policy)) { 204 | command = "configure\ninterface ethernet 1/" + port + "\nservice-policy input DEFAULT_POLICY\nend"; 205 | 206 | } else if ("DENY_ALL".equals(policy)) { 207 | command = "configure\ninterface ethernet 1/" + port + "\nservice-policy input DENY_ALL\nend"; 208 | 209 | } else if ("DENY_MSNET".equals(policy)) { 210 | command = "configure\ninterface ethernet 1/" + port + "\nservice-policy input DENY_MSNET\nend"; 211 | 212 | } 213 | return command; 214 | } 215 | 216 | public String showAccessList() { 217 | return "show access-list"; 218 | } 219 | 220 | public String showIGMPFilter(String port) { 221 | return "show ip igmp filter interface ethernet 1/" + port; 222 | } 223 | 224 | public String changePortState(String port, boolean enabled) { 225 | String command = null; 226 | 227 | if (enabled) { 228 | command = "configure\ninterface ethernet 1/" + port + "\nno shutdown\nend"; 229 | 230 | } else if (!enabled) { 231 | command = "configure\ninterface ethernet 1/" + port + "\nshutdown\nend"; 232 | 233 | } 234 | 235 | return command; 236 | } 237 | 238 | public String capabilities1000Full(String port, boolean enable) { 239 | String command = null; 240 | 241 | if (enable) { 242 | command = "configure\ninterface ethernet 1/" + port + "\n capabilities 1000full\nend"; 243 | } else if (!enable) { 244 | command = "configure\ninterface ethernet 1/" + port + "\nno capabilities 1000full\nend"; 245 | } 246 | return command; 247 | } 248 | 249 | public String changeMediaType(String port, String type) { 250 | 251 | String command = null; 252 | if ("copper-forced".equals(type)) { 253 | command = "configure\ninterface ethernet 1/" + port + "\nmedia-type copper-forced\nend"; 254 | 255 | } else if ("sfp-forced".equals(type)) { 256 | command = "configure\ninterface ethernet 1/" + port + "\nmedia-type sfp-forced\nend"; 257 | 258 | } else if ("sfp-preferred-auto".equals(type)) { 259 | command = "configure\ninterface ethernet 1/" + port + "\nmedia-type sfp-preferred-auto\nend"; 260 | 261 | } else if ("sfp-forced 100fx".equals(type)) { 262 | command = "configure\ninterface ethernet 1/" + port + "\nmedia-type sfp-forced 100fx\nend"; 263 | 264 | } 265 | return command; 266 | } 267 | 268 | public String changeNegotiation(String port, boolean enable) { 269 | 270 | String command = null; 271 | 272 | if (enable) { 273 | command = "configure\ninterface ethernet 1/" + port + "\n negotiation\nend"; 274 | } else if (!enable) { 275 | command = "configure\ninterface ethernet 1/" + port + "\nno negotiation\nend"; 276 | } 277 | return command; 278 | } 279 | 280 | public String changePortSecurity(String port, boolean enable) { 281 | 282 | String command = null; 283 | 284 | if (enable) { 285 | command = "configure\ninterface ethernet 1/" + port + "\nport security\nend"; 286 | } else if (!enable) { 287 | command = "configure\ninterface ethernet 1/" + port + "\nno port security\nend"; 288 | } 289 | return command; 290 | } 291 | 292 | public String changeIPSourceGuardSipMac(String port, boolean enable) { 293 | 294 | String command = null; 295 | 296 | if (enable) { 297 | command = "configure\ninterface ethernet 1/" + port + "\nip source-guard sip-mac\nend"; 298 | } else if (!enable) { 299 | command = "configure\ninterface ethernet 1/" + port + "\nno ip source-guard\nend"; 300 | } 301 | return command; 302 | } 303 | 304 | public String changeArpInspection(String port, boolean enable) { 305 | 306 | String command = null; 307 | 308 | if (enable) { 309 | command = "configure\ninterface ethernet 1/" + port + "\nno ip arp inspection trust\nend"; 310 | } else if (!enable) { 311 | command = "configure\ninterface ethernet 1/" + port + "\nip arp inspection trust\nend"; 312 | } 313 | return command; 314 | } 315 | 316 | public String showCurrentPortConfig(String port) { 317 | return "show running-config interface ethernet 1/" + port; 318 | } 319 | 320 | public String showPortCounters(String port) { 321 | return "show interfaces counters ethernet 1/" + port; 322 | } 323 | 324 | public String clearPortCounters(String port) { 325 | return "clear counters ethernet 1/" + port; 326 | } 327 | 328 | public String showDHCPSnoping() { 329 | return "show ip dhcp snooping"; 330 | } 331 | 332 | public String enableStpBpdu(String port, boolean enable) { 333 | String command = null; 334 | if (enable) { 335 | command = "configure\ninterface ethernet 1/" + port + "\nspanning-tree bpdu-guard\nend"; 336 | 337 | } else if (!enable) { 338 | command = "configure\ninterface ethernet 1/" + port + "\nno spanning-tree bpdu-guard\nend"; 339 | 340 | } 341 | return command; 342 | 343 | } 344 | 345 | public String enableStpBpduAutoRecovery(String port, boolean enable) { 346 | String command = null; 347 | if (enable) { 348 | command = "configure\ninterface ethernet 1/" + port + "\nspanning-tree bpdu-guard auto-recovery\nend"; 349 | 350 | } else if (!enable) { 351 | command = "configure\ninterface ethernet 1/" + port + "\nno spanning-tree bpdu-guard auto-recovery\nend"; 352 | 353 | } 354 | return command; 355 | 356 | } 357 | 358 | public String addVlanToPort(String port, String vlan, boolean tagged) { 359 | 360 | String command = null; 361 | if (tagged) { 362 | command = "configure\ninterface ethernet 1/" + port + "\n switchport allowed vlan add " + vlan + " tagged\nend"; 363 | 364 | } else if (!tagged) { 365 | command = "configure\ninterface ethernet 1/" + port + "\n switchport allowed vlan add " + vlan + " untagged\nend"; 366 | } 367 | return command; 368 | 369 | } 370 | 371 | public String showPolicy(String port) { 372 | return "show policy-map interface 1/" + port + " input"; 373 | } 374 | 375 | public String removeVlanFromPort(String port, String vlan) { 376 | return "configure\n" 377 | + "interface ethernet 1/" + port + "\n" 378 | + "switchport allowed vlan remove " + vlan + "\n" 379 | + "end"; 380 | } 381 | 382 | public String configureDHCPSnoppingVlan(String vlan, boolean enabled) { 383 | 384 | String command = null; 385 | if (enabled) { 386 | command = "configure\nip dhcp snooping vlan " + vlan + "\nend"; 387 | 388 | } else if (!enabled) { 389 | command = "configure\nno ip dhcp snooping vlan " + vlan + "\nend"; 390 | } 391 | return command; 392 | } 393 | 394 | public String configurePortsIpSourceGuard(int port1, int port2, boolean enable) { 395 | 396 | String command = null; 397 | if (enable) { 398 | command = "configure\ninterface ethernet 1/" + port1 + "-" + port2 + "\nip source-guard sip-mac\nend"; 399 | 400 | } else if (!enable) { 401 | command = "configure\ninterface ethernet 1/" + port1 + "-" + port2 + "\nno ip source-guard\nend"; 402 | } 403 | return command; 404 | } 405 | 406 | public String configurePortsSecurity(int port1, int port2, boolean enable) { 407 | 408 | String command = null; 409 | if (enable) { 410 | command = "configure\ninterface ethernet 1/" + port1 + "-" + port2 + "\nip source-guard sip-mac\nend"; 411 | 412 | } else if (!enable) { 413 | command = "configure\ninterface ethernet 1/" + port1 + "-" + port2 + "\nno ip source-guard\nend"; 414 | } 415 | return command; 416 | } 417 | 418 | public String configureVlanArpInspection(int vlan, boolean enable) { 419 | 420 | String command = null; 421 | if (enable) { 422 | command = "configure\nip arp inspection vlan " + vlan + "\nend"; 423 | 424 | } else if (!enable) { 425 | command = "configure\nno ip arp inspection vlan " + vlan + "\nend"; 426 | 427 | } 428 | return command; 429 | } 430 | 431 | public String showStpPort(String port) { 432 | 433 | return "show spanning-tree ethernet 1/" + port; 434 | } 435 | 436 | public String configureMacAddressTable(String mac, int port, int vlan, boolean add) { 437 | 438 | String command = null; 439 | if (add) { 440 | command = "configure\n" + "mac-address-table static " + mac + " interface ethernet 1/" + port + " vlan " + vlan + " \nend"; 441 | 442 | } else if (!add) { 443 | command = "configure\n" + "no mac-address-table static " + mac + " vlan " + vlan + "\nend"; 444 | } 445 | return command; 446 | } 447 | 448 | public String configureIpSourceGuardBindingTable(String mac, int vlan, String ip, int port, boolean enable) { 449 | String command = null; 450 | if (enable) { 451 | command = "config\nip source-guard binding " + mac + " vlan " + vlan + " " + ip + " interface ethernet 1/" + port + "\nend"; 452 | 453 | } else if (!enable) { 454 | command = "config\nno ip source-guard binding " + mac + " vlan " + vlan + "\nend"; 455 | 456 | } 457 | return command; 458 | } 459 | 460 | } 461 | -------------------------------------------------------------------------------- /src/main/java/com/github/dmtk/Formater.java: -------------------------------------------------------------------------------- 1 | package com.github.dmtk; 2 | 3 | public class Formater { 4 | 5 | //convert MAC string from XX-XX-XX-XX-XX-XX to XXXX.XXXX.XXXX 6 | public static String convert(String str) { 7 | String out = ""; 8 | int count = 0; 9 | for (int i = 0; i < str.length(); i++) { 10 | if (str.charAt(i) == '-') { 11 | count++; 12 | if (count % 2 == 0) { 13 | 14 | out += "."; 15 | } 16 | } else { 17 | out += str.charAt(i); 18 | } 19 | 20 | } 21 | return out; 22 | } 23 | public static String replace(String str) { 24 | 25 | return str.replace(':', '-'); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/github/dmtk/Host.java: -------------------------------------------------------------------------------- 1 | package com.github.dmtk; 2 | 3 | import java.util.Objects; 4 | 5 | public class Host { 6 | 7 | private String id; 8 | private String mac; 9 | private String ip; 10 | private String port; 11 | private String vlan; 12 | 13 | public String getId() { 14 | return id; 15 | } 16 | 17 | public void setId(String id) { 18 | this.id = id; 19 | } 20 | 21 | public String getMac() { 22 | return mac; 23 | } 24 | 25 | public void setMac(String mac) { 26 | this.mac = mac; 27 | } 28 | 29 | public String getIp() { 30 | return ip; 31 | } 32 | 33 | public void setIp(String ip) { 34 | this.ip = ip; 35 | } 36 | 37 | public String getPort() { 38 | return port; 39 | } 40 | 41 | public void setPort(String port) { 42 | this.port = port; 43 | } 44 | 45 | public String getVlan() { 46 | return vlan; 47 | } 48 | 49 | public void setVlan(String vlan) { 50 | this.vlan = vlan; 51 | } 52 | 53 | @Override 54 | public String toString() { 55 | return id+" " 56 | + mac+" " 57 | + ip+" " 58 | + port+" " 59 | + vlan; 60 | } 61 | @Override 62 | public boolean equals(Object object){ 63 | if(object==null) return false; 64 | else if(this==object) return true; 65 | else if(object!=null){ 66 | Host host=(Host) object; 67 | if(!this.id.equals(host.getId())) return false; 68 | if(!this.ip.equals(host.getIp())) return false; 69 | if(!this.mac.equals(host.getMac())) return false; 70 | if(!this.port.equals(host.getPort())) return false; 71 | if(!this.vlan.equals(host.getVlan())) return false; 72 | return true; 73 | } 74 | else return false; 75 | 76 | } 77 | 78 | 79 | @Override 80 | public int hashCode() { 81 | int hash = 7; 82 | hash = 67 * hash + Objects.hashCode(this.id); 83 | hash = 67 * hash + Objects.hashCode(this.mac); 84 | hash = 67 * hash + Objects.hashCode(this.ip); 85 | hash = 67 * hash + Objects.hashCode(this.port); 86 | hash = 67 * hash + Objects.hashCode(this.vlan); 87 | return hash; 88 | } 89 | 90 | } 91 | -------------------------------------------------------------------------------- /src/main/java/com/github/dmtk/MainClass.java: -------------------------------------------------------------------------------- 1 | package com.github.dmtk; 2 | 3 | import java.io.IOException; 4 | import javax.swing.UIManager; 5 | import javax.swing.UnsupportedLookAndFeelException; 6 | import org.apache.log4j.Logger; 7 | 8 | public class MainClass { 9 | 10 | private static GUI gui; 11 | private final static Logger log = Logger.getLogger(MainClass.class); 12 | public static void main(String[] argv) throws IOException, UnsupportedLookAndFeelException, Exception { 13 | 14 | log.info("Start"); 15 | try { 16 | for (UIManager.LookAndFeelInfo info : UIManager 17 | .getInstalledLookAndFeels()) { 18 | if ("Windows".equals(info.getName())) { 19 | UIManager.setLookAndFeel(info.getClassName()); 20 | break; 21 | } 22 | } 23 | } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 24 | log.error(ex); 25 | } 26 | gui = GUI.getInstance(); 27 | gui.setVisible(true); 28 | } 29 | 30 | public static void launchBrowser(String url) { 31 | String os = System.getProperty("os.name").toLowerCase(); 32 | Runtime rt = Runtime.getRuntime(); 33 | try { 34 | if (os.indexOf("win") >= 0) { 35 | rt.exec("rundll32 url.dll,FileProtocolHandler " + url); 36 | } else if (os.indexOf("mac") >= 0) { 37 | rt.exec("open " + url); 38 | } else if ((os.indexOf("nix") >= 0) || (os.indexOf("nux") >= 0)) { 39 | String[] browsers = {"epiphany", "firefox", "mozilla", 40 | "konqueror", "netscape", "opera", "links", "lynx", 41 | "chromium"}; 42 | 43 | StringBuffer cmd = new StringBuffer(); 44 | for (int i = 0; i < browsers.length; i++) { 45 | cmd.append(i == 0 ? "" : " || ").append(browsers[i]) 46 | .append(" \"").append(url).append("\" "); 47 | } 48 | rt.exec(new String[]{"sh", "-c", cmd.toString()}); 49 | } 50 | } catch (IOException e) { 51 | log.error(e); 52 | } 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/github/dmtk/Modem.java: -------------------------------------------------------------------------------- 1 | package com.github.dmtk; 2 | 3 | public interface Modem { 4 | 5 | public String testCable(String port); 6 | 7 | public String showPorts(); 8 | 9 | public String showMACAddressTable(); 10 | 11 | public String reload(); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/github/dmtk/MyButtonUI.java: -------------------------------------------------------------------------------- 1 | package com.github.dmtk; 2 | 3 | import java.awt.AlphaComposite; 4 | import java.awt.BasicStroke; 5 | import java.awt.Color; 6 | import java.awt.Composite; 7 | import java.awt.GradientPaint; 8 | import java.awt.Graphics; 9 | import java.awt.Graphics2D; 10 | import java.awt.Insets; 11 | import java.awt.RenderingHints; 12 | import java.awt.Stroke; 13 | import java.awt.event.ActionEvent; 14 | import java.awt.event.ActionListener; 15 | import java.awt.event.ItemEvent; 16 | import java.awt.event.ItemListener; 17 | import java.awt.event.MouseAdapter; 18 | import java.awt.event.MouseEvent; 19 | import java.awt.geom.GeneralPath; 20 | import java.util.ArrayList; 21 | import java.util.Arrays; 22 | import javax.swing.AbstractButton; 23 | import javax.swing.BorderFactory; 24 | import javax.swing.ButtonModel; 25 | import javax.swing.JComponent; 26 | import javax.swing.SwingUtilities; 27 | import javax.swing.Timer; 28 | import javax.swing.plaf.basic.BasicToggleButtonUI; 29 | 30 | public class MyButtonUI extends BasicToggleButtonUI { 31 | 32 | public static final float defaultMouseoverTransparency = 0.4f; 33 | public static final int maxRounding = 8; 34 | public static final int midRounding = 6; 35 | 36 | public static final Stroke focusStroke 37 | = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 5, 38 | new float[]{3, 3}, 0); 39 | 40 | public static Color buttonBg =new Color(139,203,250);//(193,222,236);// new Color(205, 205, 225); 41 | 42 | private AbstractButton button; 43 | 44 | private Color topBg = new Color(255, 255, 255, 128); 45 | private Color bg = Color.WHITE; 46 | private boolean alwaysDrawFocus = false; 47 | 48 | private Color borderColor =new Color(139,203,250); //Color.LIGHT_GRAY; 49 | private Color selectedBorderColor = Color.GRAY; 50 | 51 | private boolean alwaysDrawBackground = false; 52 | private Color staticTopBg = new Color(240, 240, 240);//Color.WHITE; 53 | private Color disabledStaticTopBg = Color.WHITE; 54 | private Color staticBottomBg = new Color(245, 247, 249); 55 | private Color disabledStaticBottomBg = new Color(245, 245, 245); 56 | 57 | private Color staticBorderColor = new Color(180, 180, 180); 58 | private Color staticDisabledBorderColor = new Color(230, 230, 230); 59 | 60 | private static final int maxFadeTimes = 16; 61 | 62 | private boolean mouseOver = false; 63 | private float mouseoverTransparency = defaultMouseoverTransparency; 64 | 65 | private Timer fadeOutTimer; 66 | private float fadeTime = maxFadeTimes; 67 | 68 | private boolean sharpTopLeft = false; 69 | private boolean sharpTopRight = false; 70 | private boolean sharpBottomLeft = false; 71 | private boolean sharpBottomRight = false; 72 | 73 | public MyButtonUI(final AbstractButton button) { 74 | this(button, true, defaultMouseoverTransparency); 75 | } 76 | 77 | public MyButtonUI(final AbstractButton button, final boolean changeForeground) { 78 | this(button, changeForeground, defaultMouseoverTransparency); 79 | } 80 | 81 | public MyButtonUI(final AbstractButton button, float mouseoverTransparency) { 82 | this(button, true, mouseoverTransparency); 83 | } 84 | 85 | public MyButtonUI(final AbstractButton button, final boolean changeForeground, 86 | float mouseoverTransparency) { 87 | super(); 88 | 89 | this.button = button; 90 | this.mouseoverTransparency = mouseoverTransparency; 91 | 92 | button.setBorderPainted(false); 93 | button.setOpaque(false); 94 | 95 | final Color foreground = button.getForeground(); 96 | if (changeForeground) { 97 | button.setForeground(button.isSelected() ? foreground : Color.WHITE); 98 | } 99 | 100 | fadeOutTimer = new Timer(1000 / 24, new ActionListener() { 101 | public void actionPerformed(ActionEvent e) { 102 | fadeTime++; 103 | SwingUtilities.invokeLater(new Runnable() { 104 | public void run() { 105 | button.repaint(); 106 | } 107 | }); 108 | 109 | if (fadeTime == maxFadeTimes) { 110 | fadeOutTimer.stop(); 111 | } 112 | } 113 | }); 114 | button.addItemListener(new ItemListener() { 115 | public void itemStateChanged(ItemEvent e) { 116 | if (button.isSelected()) { 117 | fadeOutTimer.stop(); 118 | fadeTime = 1; 119 | if (changeForeground) { 120 | button.setForeground(foreground); 121 | } 122 | } else { 123 | if (changeForeground) { 124 | button.setForeground(Color.WHITE); 125 | } 126 | fadeTime = 1; 127 | fadeOutTimer.restart(); 128 | } 129 | } 130 | }); 131 | button.addMouseListener(new MouseAdapter() { 132 | public void mouseEntered(MouseEvent e) { 133 | mouseOver = true; 134 | fadeOutTimer.stop(); 135 | fadeTime = 1; 136 | button.repaint(); 137 | } 138 | 139 | public void mouseExited(MouseEvent e) { 140 | dropMouseOver(); 141 | } 142 | }); 143 | } 144 | 145 | public void dropMouseOver() { 146 | mouseOver = false; 147 | fadeTime = 1; 148 | fadeOutTimer.restart(); 149 | } 150 | 151 | public void setColor(Color color, int alpha) { 152 | this.topBg = new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha); 153 | this.bg = color; 154 | } 155 | 156 | public void setStaticColor(Color topBg, Color bottomBg) { 157 | this.staticTopBg = topBg; 158 | this.staticBottomBg = bottomBg; 159 | this.disabledStaticTopBg = topBg; 160 | this.disabledStaticBottomBg = new Color(Math.min(255, bottomBg.getRed() + 5), 161 | Math.min(255, bottomBg.getGreen() + 5), 162 | Math.min(255, bottomBg.getBlue() + 5)); 163 | } 164 | 165 | public void setStaticBorderColor(Color staticBorderColor, Color staticDisabledBorderColor) { 166 | this.staticBorderColor = staticBorderColor; 167 | this.staticDisabledBorderColor = staticDisabledBorderColor; 168 | } 169 | 170 | public void setBorderColor(Color borderColor, Color selectedBorderColor) { 171 | this.borderColor = borderColor; 172 | this.selectedBorderColor = selectedBorderColor; 173 | } 174 | 175 | public void setInnerBorderColor(Color borderColor, Color selectedBorderColor) { 176 | this.borderColor = borderColor; 177 | this.selectedBorderColor = selectedBorderColor; 178 | } 179 | 180 | public Color getTopBg() { 181 | return topBg; 182 | } 183 | 184 | public Color getBg() { 185 | return bg; 186 | } 187 | 188 | public void setRoundedSides(java.util.List rounded) { 189 | setSharpTopLeft(!rounded.contains(1) && !rounded.contains(-1)); 190 | setSharpTopRight(!rounded.contains(2) && !rounded.contains(-1)); 191 | setSharpBottomLeft(!rounded.contains(3) && !rounded.contains(-1)); 192 | setSharpBottomRight(!rounded.contains(4) && !rounded.contains(-1)); 193 | } 194 | 195 | public java.util.List getRoundedSides() { 196 | java.util.List rounded = new ArrayList(); 197 | if (!isSharpTopLeft() && !isSharpBottomRight() && !isSharpBottomLeft() 198 | && !isSharpBottomRight()) { 199 | rounded.add(-1); 200 | } else if (isSharpTopLeft() && isSharpBottomRight() && isSharpBottomLeft() 201 | && isSharpBottomRight()) { 202 | rounded.add(0); 203 | } else { 204 | if (!isSharpTopLeft()) { 205 | rounded.add(1); 206 | } 207 | if (!isSharpTopRight()) { 208 | rounded.add(2); 209 | } 210 | if (!isSharpBottomLeft()) { 211 | rounded.add(3); 212 | } 213 | if (!isSharpBottomRight()) { 214 | rounded.add(4); 215 | } 216 | } 217 | return rounded; 218 | } 219 | 220 | public boolean isSharpBottomLeft() { 221 | return sharpBottomLeft; 222 | } 223 | 224 | public void setSharpBottomLeft(boolean sharpBottomLeft) { 225 | this.sharpBottomLeft = sharpBottomLeft; 226 | } 227 | 228 | public boolean isSharpBottomRight() { 229 | return sharpBottomRight; 230 | } 231 | 232 | public void setSharpBottomRight(boolean sharpBottomRight) { 233 | this.sharpBottomRight = sharpBottomRight; 234 | } 235 | 236 | public boolean isSharpTopLeft() { 237 | return sharpTopLeft; 238 | } 239 | 240 | public void setSharpTopLeft(boolean sharpTopLeft) { 241 | this.sharpTopLeft = sharpTopLeft; 242 | } 243 | 244 | public boolean isSharpTopRight() { 245 | return sharpTopRight; 246 | } 247 | 248 | public void setSharpTopRight(boolean sharpTopRight) { 249 | this.sharpTopRight = sharpTopRight; 250 | } 251 | 252 | public void setAllSharp(boolean sharp) { 253 | this.sharpTopLeft = sharp; 254 | this.sharpTopRight = sharp; 255 | this.sharpBottomLeft = sharp; 256 | this.sharpBottomRight = sharp; 257 | } 258 | 259 | public boolean isAlwaysDrawFocus() { 260 | return alwaysDrawFocus; 261 | } 262 | 263 | public void setAlwaysDrawFocus(boolean alwaysDrawFocus) { 264 | this.alwaysDrawFocus = alwaysDrawFocus; 265 | } 266 | 267 | public boolean isAlwaysDrawBackground() { 268 | return alwaysDrawBackground; 269 | } 270 | 271 | public void setAlwaysDrawBackground(boolean alwaysDrawBackground) { 272 | this.alwaysDrawBackground = alwaysDrawBackground; 273 | } 274 | 275 | public void paint(Graphics g, JComponent c) { 276 | Graphics2D g2d = (Graphics2D) g; 277 | Object aa = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING); 278 | g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 279 | 280 | ButtonModel model = ((AbstractButton) c).getModel(); 281 | 282 | // Создаем три полигона бордеров 283 | GeneralPath gp0 = new GeneralPath(); 284 | GeneralPath gp1 = new GeneralPath(); 285 | GeneralPath gp2 = new GeneralPath(); 286 | if (!sharpTopLeft) { 287 | gp0.moveTo(0, maxRounding / 2); 288 | gp0.quadTo(0, 0, maxRounding / 2, 0); 289 | gp1.moveTo(1, 1 + midRounding / 2); 290 | gp1.quadTo(1, 1, 1 + midRounding / 2, 1); 291 | gp2.moveTo(2, 2 + midRounding / 2); 292 | gp2.quadTo(2, 2, 2 + midRounding / 2, 2); 293 | } else { 294 | gp0.moveTo(0, 0); 295 | gp1.moveTo(1, 1); 296 | gp2.moveTo(2, 2); 297 | } 298 | if (!sharpTopRight) { 299 | gp0.lineTo(c.getWidth() - maxRounding / 2 - 1, 0); 300 | gp0.quadTo(c.getWidth() - 1, 0, c.getWidth() - 1, maxRounding / 2); 301 | gp1.lineTo(c.getWidth() - midRounding / 2 - 2, 1); 302 | gp1.quadTo(c.getWidth() - 2, 1, c.getWidth() - 2, 1 + midRounding / 2); 303 | gp2.lineTo(c.getWidth() - midRounding / 2 - 3, 2); 304 | gp2.quadTo(c.getWidth() - 3, 2, c.getWidth() - 3, 2 + midRounding / 2); 305 | } else { 306 | gp0.lineTo(c.getWidth() - 1, 0); 307 | gp1.lineTo(c.getWidth() - 2, 1); 308 | gp2.lineTo(c.getWidth() - 3, 2); 309 | } 310 | if (!sharpBottomRight) { 311 | gp0.lineTo(c.getWidth() - 1, c.getHeight() - maxRounding / 2 - 1); 312 | gp0.quadTo(c.getWidth() - 1, c.getHeight() - 1, c.getWidth() - maxRounding / 2 - 1, 313 | c.getHeight() - 1); 314 | gp1.lineTo(c.getWidth() - 2, c.getHeight() - midRounding / 2 - 2); 315 | gp1.quadTo(c.getWidth() - 2, c.getHeight() - 2, c.getWidth() - midRounding / 2 - 2, 316 | c.getHeight() - 2); 317 | gp2.lineTo(c.getWidth() - 3, c.getHeight() - midRounding / 2 - 3); 318 | gp2.quadTo(c.getWidth() - 3, c.getHeight() - 3, c.getWidth() - midRounding / 2 - 3, 319 | c.getHeight() - 3); 320 | } else { 321 | gp0.lineTo(c.getWidth() - 1, c.getHeight() - 1); 322 | gp1.lineTo(c.getWidth() - 2, c.getHeight() - 2); 323 | gp2.lineTo(c.getWidth() - 3, c.getHeight() - 3); 324 | } 325 | if (!sharpBottomLeft) { 326 | gp0.lineTo(maxRounding / 2, c.getHeight() - 1); 327 | gp0.quadTo(0, c.getHeight() - 1, 0, c.getHeight() - maxRounding / 2 - 1); 328 | gp1.lineTo(1 + midRounding / 2, c.getHeight() - 2); 329 | gp1.quadTo(1, c.getHeight() - 2, 1, c.getHeight() - midRounding / 2 - 2); 330 | gp2.lineTo(2 + midRounding / 2, c.getHeight() - 3); 331 | gp2.quadTo(2, c.getHeight() - 3, 2, c.getHeight() - midRounding / 2 - 3); 332 | } else { 333 | gp0.lineTo(0, c.getHeight() - 1); 334 | gp1.lineTo(1, c.getHeight() - 2); 335 | gp2.lineTo(2, c.getHeight() - 3); 336 | } 337 | if (!sharpTopLeft) { 338 | gp0.lineTo(0, maxRounding / 2); 339 | gp1.lineTo(1, 1 + midRounding / 2); 340 | gp2.lineTo(2, 2 + midRounding / 2); 341 | } else { 342 | gp0.lineTo(0, 0); 343 | gp1.lineTo(1, 1); 344 | gp2.lineTo(2, 2); 345 | } 346 | 347 | // Статичный фон 348 | if (alwaysDrawBackground) { 349 | g2d.setPaint( 350 | new GradientPaint(0, 0, c.isEnabled() ? staticTopBg : disabledStaticTopBg, 0, 351 | c.getHeight(), 352 | c.isEnabled() ? staticBottomBg : disabledStaticBottomBg)); 353 | g2d.fill(gp0); 354 | g2d.setPaint(c.isEnabled() ? staticBorderColor : staticDisabledBorderColor); 355 | g2d.draw(gp0); 356 | } 357 | 358 | if (c.isEnabled()) { 359 | // Рисуем фон 360 | Composite composite = g2d.getComposite(); 361 | if (model.isSelected() || model.isArmed()) { 362 | g2d.setPaint(new GradientPaint(c.getWidth() / 3, c.getHeight() / 3, topBg, 363 | c.getWidth(), c.getHeight(), bg)); 364 | g2d.fill(gp0); 365 | } else if (mouseOver || fadeTime != maxFadeTimes) { 366 | g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 367 | mouseoverTransparency * (maxFadeTimes - fadeTime) / maxFadeTimes)); 368 | g2d.setPaint(bg); 369 | g2d.fill(gp0); 370 | if (mouseOver) { 371 | g2d.setComposite(composite); 372 | } else { 373 | g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 374 | 1f * (maxFadeTimes - fadeTime) / maxFadeTimes)); 375 | } 376 | } 377 | 378 | // Рисуем фокус 379 | if (c.isFocusOwner() || alwaysDrawFocus) { 380 | Composite cc = g2d.getComposite(); 381 | g2d.setComposite(composite); 382 | Stroke s = g2d.getStroke(); 383 | 384 | g2d.setStroke(focusStroke); 385 | g2d.setPaint(c.isFocusOwner() ? Color.GRAY : bg); 386 | 387 | // Внутренний бордер 388 | g2d.draw(gp2); 389 | 390 | // Внешний бордер 391 | if (alwaysDrawFocus) { 392 | g2d.setStroke(s); 393 | } 394 | g2d.draw(gp0); 395 | 396 | if (!alwaysDrawFocus) { 397 | g2d.setStroke(s); 398 | } 399 | g2d.setComposite(cc); 400 | } 401 | 402 | // Рисуем кайму 403 | if (model.isSelected() || model.isArmed() || mouseOver || fadeTime != maxFadeTimes) { 404 | g2d.setPaint(model.isSelected() ? selectedBorderColor : borderColor); 405 | g2d.draw(gp0); 406 | g2d.setPaint(model.isPressed() ? bg : Color.WHITE); 407 | g2d.draw(gp1); 408 | } 409 | 410 | // Возвращаем исходный композит 411 | g2d.setComposite(composite); 412 | } 413 | 414 | // Возвращаем исходный антиалиасинг 415 | g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aa); 416 | 417 | // Для отрисовки эффекта нажатия 418 | if (model.isPressed()) { 419 | g2d.translate(1, 1); 420 | } 421 | 422 | // Отрисовка текста и изорбажения 423 | super.paint(g, c); 424 | } 425 | 426 | 427 | /* 428 | * Методы для упрощения стилизации кнопок 429 | */ 430 | public static MyButtonUI setupButtonUI(AbstractButton button) { 431 | return setupButtonUI(button, -1); 432 | } 433 | 434 | public static MyButtonUI setupButtonUI(AbstractButton button, Insets insets) { 435 | return setupButtonUI(button, -1, insets); 436 | } 437 | 438 | public static MyButtonUI setupButtonUI(AbstractButton button, int rounded) { 439 | return setupButtonUI(button, Arrays.asList(rounded)); 440 | } 441 | 442 | public static MyButtonUI setupButtonUI(AbstractButton button, int rounded, Insets insets) { 443 | return setupButtonUI(button, Arrays.asList(rounded), insets); 444 | } 445 | 446 | public static MyButtonUI setupButtonUI(AbstractButton button, 447 | java.util.List rounded) { 448 | return setupButtonUI(button, rounded, new Insets(4, 4, 4, 4)); 449 | } 450 | 451 | public static MyButtonUI setupButtonUI(AbstractButton button, java.util.List rounded, 452 | Insets insets) { 453 | MyButtonUI stbui = new MyButtonUI(button, false); 454 | stbui.setColor(buttonBg, 128); 455 | stbui.setRoundedSides(rounded); 456 | stbui.setAlwaysDrawBackground(true); 457 | button.setUI(stbui); 458 | button.setMargin(insets); 459 | button.setBorder(BorderFactory 460 | .createEmptyBorder(insets.top, insets.left, insets.bottom, insets.right)); 461 | return stbui; 462 | } 463 | 464 | public static MyButtonUI setupDialogButtonUI(AbstractButton button, int rounded) { 465 | return setupDialogButtonUI(button, Arrays.asList(rounded)); 466 | } 467 | 468 | public static MyButtonUI setupDialogButtonUI(AbstractButton button, 469 | java.util.List rounded) { 470 | return setupDialogButtonUI(button, rounded, new Insets(4, 12, 4, 12)); 471 | } 472 | 473 | public static MyButtonUI setupDialogButtonUI(AbstractButton button, int rounded, 474 | Insets insets) { 475 | return setupDialogButtonUI(button, Arrays.asList(rounded), insets); 476 | } 477 | 478 | public static MyButtonUI setupDialogButtonUI(AbstractButton button, 479 | java.util.List rounded, Insets insets) { 480 | MyButtonUI gbui = setupButtonUI(button, rounded, insets); 481 | gbui.setStaticBorderColor(new Color(155, 155, 155), new Color(175, 175, 175)); 482 | gbui.setStaticColor(new Color(210, 210, 210), new Color(175, 175, 175)); 483 | return gbui; 484 | } 485 | } 486 | 487 | -------------------------------------------------------------------------------- /src/main/java/com/github/dmtk/PingWindow.form: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /src/main/java/com/github/dmtk/PingWindow.java: -------------------------------------------------------------------------------- 1 | package com.github.dmtk; 2 | 3 | public class PingWindow extends javax.swing.JFrame { 4 | 5 | public PingWindow() { 6 | initComponents(); 7 | } 8 | 9 | // //GEN-BEGIN:initComponents 10 | private void initComponents() { 11 | 12 | jScrollPane1 = new javax.swing.JScrollPane(); 13 | jTextArea1 = new javax.swing.JTextArea(); 14 | 15 | setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); 16 | setTitle("Ping"); 17 | setAlwaysOnTop(true); 18 | 19 | jTextArea1.setEditable(false); 20 | jTextArea1.setBackground(new java.awt.Color(0, 0, 0)); 21 | jTextArea1.setColumns(20); 22 | jTextArea1.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N 23 | jTextArea1.setForeground(new java.awt.Color(255, 255, 255)); 24 | jTextArea1.setLineWrap(true); 25 | jTextArea1.setRows(5); 26 | jScrollPane1.setViewportView(jTextArea1); 27 | 28 | javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 29 | getContentPane().setLayout(layout); 30 | layout.setHorizontalGroup( 31 | layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 32 | .addGroup(layout.createSequentialGroup() 33 | .addGap(0, 0, 0) 34 | .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 516, Short.MAX_VALUE) 35 | .addGap(0, 0, 0)) 36 | ); 37 | layout.setVerticalGroup( 38 | layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 39 | .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE) 40 | ); 41 | 42 | pack(); 43 | }// //GEN-END:initComponents 44 | 45 | 46 | // Variables declaration - do not modify//GEN-BEGIN:variables 47 | private javax.swing.JScrollPane jScrollPane1; 48 | private javax.swing.JTextArea jTextArea1; 49 | // End of variables declaration//GEN-END:variables 50 | 51 | public javax.swing.JTextArea getjTextArea1() { 52 | return jTextArea1; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/github/dmtk/Pinger.java: -------------------------------------------------------------------------------- 1 | package com.github.dmtk; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.io.PrintStream; 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | import org.apache.log4j.Logger; 10 | 11 | public class Pinger { 12 | 13 | private final static Logger log = Logger.getLogger(Pinger.class); 14 | 15 | static synchronized void pingClient(PrintStream stream, String ip) { 16 | 17 | List commands = new ArrayList(); 18 | commands.add("ping"); 19 | commands.add(ip); 20 | String s = null; 21 | ProcessBuilder pb = new ProcessBuilder(commands); 22 | Process process = null; 23 | try { 24 | process = pb.start(); 25 | } catch (IOException ex) { 26 | log.error(ex); 27 | } 28 | BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream())); 29 | BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream())); 30 | try { 31 | while ((s = stdInput.readLine()) != null) { 32 | 33 | String newString = new String(s.getBytes("windows-1251"), "cp866");//change encoding 34 | stream.println(newString); 35 | } 36 | } catch (IOException ex) { 37 | log.error(ex); 38 | } 39 | // read any errors from the attempted command 40 | try { 41 | while ((s = stdError.readLine()) != null) { 42 | stream.println(s); 43 | } 44 | } catch (IOException ex) { 45 | log.error(ex); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/github/dmtk/Port.java: -------------------------------------------------------------------------------- 1 | package com.github.dmtk; 2 | import org.apache.log4j.Logger; 3 | 4 | public class Port{ 5 | 6 | private int port; 7 | private final int defaultPort = 2; 8 | private final static Logger log = Logger.getLogger(Port.class); 9 | 10 | public Port(String port) { 11 | 12 | try { 13 | intialize(Integer.parseInt(port)); 14 | } catch (NumberFormatException ex) { 15 | log.error("Wrong port number format" + port); 16 | } 17 | 18 | } 19 | 20 | public int getNum(){ 21 | return port; 22 | } 23 | 24 | public Port(int port) { 25 | intialize(port); 26 | } 27 | 28 | private void intialize(int port) { 29 | if (validate(port)) { 30 | this.port = port; 31 | } else { 32 | this.port = defaultPort; 33 | log.error("Wrong port number" + port); 34 | } 35 | } 36 | 37 | private boolean validate(int vlan) { 38 | return vlan > 0 && vlan < 52;//Ports range 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/github/dmtk/Ssh.java: -------------------------------------------------------------------------------- 1 | package com.github.dmtk; 2 | 3 | import com.jcraft.jsch.*; 4 | import java.io.ByteArrayInputStream; 5 | import java.io.IOException; 6 | import java.io.PrintStream; 7 | import javax.swing.*; 8 | import org.apache.log4j.Logger; 9 | 10 | public class Ssh extends Terminal { 11 | 12 | private Session session; 13 | private final static Logger log = Logger.getLogger(Ssh.class); 14 | 15 | public void start(String host, int port, PrintStream output, String user, String password) { 16 | 17 | try { 18 | JSch jsch = new JSch(); 19 | //jsch.setKnownHosts("/home/foo/.ssh/known_hosts"); 20 | session = jsch.getSession(user, host, port); 21 | session.setPassword(password); 22 | UserInfo ui = new MyUserInfo() { 23 | public void showMessage(String message) { 24 | JOptionPane.showMessageDialog(null, message); 25 | } 26 | 27 | public boolean promptYesNo(String message) { 28 | Object[] options = {"yes", "no"}; 29 | int foo = JOptionPane.showOptionDialog(null, 30 | message, 31 | "Warning", 32 | JOptionPane.DEFAULT_OPTION, 33 | JOptionPane.WARNING_MESSAGE, 34 | null, options, options[0]); 35 | return foo == 0; 36 | } 37 | 38 | // If password is not given before the invocation of Session#connect(), 39 | // implement also following methods, 40 | // * UserInfo#getPassword(), 41 | // * UserInfo#promptPassword(String message) and 42 | // * UIKeyboardInteractive#promptKeyboardInteractive() 43 | }; 44 | session.setUserInfo(ui); 45 | // It must not be recommended, but if you want to skip host-key check, 46 | // invoke following, 47 | // session.setConfig("StrictHostKeyChecking", "no"); 48 | //session.connect(); 49 | session.connect(30000); // making a connection with timeout. 50 | Channel channel = session.openChannel("shell"); 51 | channel.setInputStream(System.in); 52 | channel.setOutputStream(output); 53 | streamOut = channel.getOutputStream(); 54 | 55 | // Enable agent-forwarding. 56 | //((ChannelShell)channel).setAgentForwarding(true); 57 | 58 | /* 59 | // a hack for MS-DOS prompt on Windows. 60 | channel.setInputStream(new FilterInputStream(System.in){ 61 | public int read(byte[] b, int off, int len)throws IOException{ 62 | return in.read(b, off, (len>1024?1024:len)); 63 | } 64 | }); 65 | */ 66 | /* 67 | // Choose the pty-type "vt102". 68 | ((ChannelShell)channel).setPtyType("vt102"); 69 | */ 70 | /* 71 | // Set environment variable "LANG" as "ja_JP.eucJP". 72 | ((ChannelShell)channel).setEnv("LANG", "ja_JP.eucJP"); 73 | */ 74 | //channel.connect(); 75 | channel.connect(60 * 1000);//60 sec timeout 76 | while (!end_loop) { 77 | end_loop = false; 78 | int ret_read = 0; 79 | do { 80 | buff = this.waitCommand(); 81 | in = new ByteArrayInputStream(buff); 82 | try { 83 | ret_read = in.read(buff); 84 | //System.out.print(ret_read + " " + new String(buff, 0, ret_read) + "\n"); 85 | 86 | try { 87 | streamOut.write(buff, 0, ret_read); 88 | streamOut.flush(); 89 | } catch (IOException e) { 90 | end_loop = true; 91 | } 92 | 93 | } catch (IOException ex) { 94 | log.error("Exception while reading keyboard:" + ex); 95 | end_loop = true; 96 | } 97 | } while ((ret_read > 0) && (end_loop == false)); 98 | } 99 | } catch (JSchException | IOException ex) { 100 | log.error(ex); 101 | } 102 | 103 | } 104 | 105 | @Override 106 | public void disconnect() throws IOException { 107 | end_loop = true; 108 | if (session != null && session.isConnected()) { 109 | session.disconnect(); 110 | } 111 | 112 | } 113 | 114 | public static abstract class MyUserInfo 115 | implements UserInfo, UIKeyboardInteractive { 116 | 117 | public String getPassword() { 118 | return null; 119 | } 120 | 121 | public boolean promptYesNo(String str) { 122 | return false; 123 | } 124 | 125 | public String getPassphrase() { 126 | return null; 127 | } 128 | 129 | public boolean promptPassphrase(String message) { 130 | return false; 131 | } 132 | 133 | public boolean promptPassword(String message) { 134 | return false; 135 | } 136 | 137 | public void showMessage(String message) { 138 | } 139 | 140 | public String[] promptKeyboardInteractive(String destination, 141 | String name, 142 | String instruction, 143 | String[] prompt, 144 | boolean[] echo) { 145 | return null; 146 | } 147 | } 148 | 149 | } 150 | -------------------------------------------------------------------------------- /src/main/java/com/github/dmtk/StringCrypter.java: -------------------------------------------------------------------------------- 1 | package com.github.dmtk; 2 | 3 | import java.io.IOException; 4 | import org.apache.commons.codec.binary.Base64; 5 | import java.io.UnsupportedEncodingException; 6 | import java.security.InvalidKeyException; 7 | import java.security.NoSuchAlgorithmException; 8 | import javax.crypto.BadPaddingException; 9 | import javax.crypto.Cipher; 10 | import javax.crypto.IllegalBlockSizeException; 11 | import javax.crypto.NoSuchPaddingException; 12 | import javax.crypto.SecretKey; 13 | import org.apache.log4j.Logger; 14 | 15 | public class StringCrypter { 16 | 17 | private final static Logger log = Logger.getLogger(StringCrypter.class); 18 | /** 19 | * Упрощенный конструктор. Создает StringCrypter с ключом 20 | * DESSecretKey (алгоритм шифрования DES) со значением key. 21 | * Ключ key должен иметь длину 8 байт 22 | */ 23 | public StringCrypter(byte[] key) { 24 | try { 25 | updateSecretKey(new DESSecretKey(key)); 26 | } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException ex) { 27 | throw new IllegalArgumentException(ex.getMessage()); 28 | } 29 | } 30 | 31 | public StringCrypter(SecretKey key) throws NoSuchPaddingException, 32 | NoSuchAlgorithmException, 33 | InvalidKeyException { 34 | updateSecretKey(key); 35 | } 36 | 37 | private void updateSecretKey(SecretKey key) throws NoSuchPaddingException, 38 | NoSuchAlgorithmException, 39 | InvalidKeyException { 40 | ecipher = Cipher.getInstance(key.getAlgorithm()); 41 | dcipher = Cipher.getInstance(key.getAlgorithm()); 42 | ecipher.init(Cipher.ENCRYPT_MODE, key); 43 | dcipher.init(Cipher.DECRYPT_MODE, key); 44 | } 45 | 46 | public static class DESSecretKey implements SecretKey { 47 | 48 | private final byte[] key; 49 | 50 | /** 51 | * ключ должен иметь длину 8 байт 52 | */ 53 | public DESSecretKey(byte[] key) { 54 | this.key = key; 55 | } 56 | 57 | @Override 58 | public String getAlgorithm() { 59 | return "DES"; 60 | } 61 | 62 | @Override 63 | public String getFormat() { 64 | return "RAW"; 65 | } 66 | 67 | @Override 68 | public byte[] getEncoded() { 69 | return key; 70 | } 71 | } 72 | 73 | private Cipher ecipher; 74 | private Cipher dcipher; 75 | 76 | /** 77 | * Функция шифрования 78 | * 79 | * @param str строка открытого текста 80 | * @return зашифрованная строка в формате Base64 81 | */ 82 | public String encrypt(String str) { 83 | try { 84 | byte[] utf8 = str.getBytes("UTF8"); 85 | byte[] enc = ecipher.doFinal(utf8); 86 | return Base64.encodeBase64String(enc); 87 | } catch (IllegalBlockSizeException | BadPaddingException | UnsupportedEncodingException ex) { 88 | log.error(ex); 89 | } 90 | return null; 91 | } 92 | 93 | /** 94 | * Функция дешифрования 95 | * 96 | * @param str зашифрованная строка в формате Base64 97 | * @return расшифрованная строка 98 | */ 99 | public String decrypt(String str) { 100 | try { 101 | byte[] dec = Base64.decodeBase64(str); 102 | byte[] utf8 = dcipher.doFinal(dec); 103 | return new String(utf8, "UTF8"); 104 | } catch (IllegalBlockSizeException | BadPaddingException | IOException ex) { 105 | log.error(ex); 106 | } 107 | return null; 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /src/main/java/com/github/dmtk/Telnet.java: -------------------------------------------------------------------------------- 1 | package com.github.dmtk; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.FileOutputStream; 5 | import java.io.IOException; 6 | import java.io.InputStream; 7 | import java.io.OutputStream; 8 | import java.io.PrintStream; 9 | import java.util.StringTokenizer; 10 | import org.apache.commons.net.telnet.EchoOptionHandler; 11 | import org.apache.commons.net.telnet.InvalidTelnetOptionException; 12 | import org.apache.commons.net.telnet.SimpleOptionHandler; 13 | import org.apache.commons.net.telnet.SuppressGAOptionHandler; 14 | import org.apache.commons.net.telnet.TelnetClient; 15 | import org.apache.commons.net.telnet.TelnetNotificationHandler; 16 | import org.apache.commons.net.telnet.TerminalTypeOptionHandler; 17 | import org.apache.log4j.Logger; 18 | 19 | public class Telnet extends Terminal implements Runnable, TelnetNotificationHandler { 20 | 21 | private static TelnetClient tc = null; 22 | private final String remoteip; 23 | private final int remoteport; 24 | private final PrintStream out; 25 | private final static Logger log = Logger.getLogger(Telnet.class); 26 | private final int defaultTelnetPort = 23; 27 | 28 | public Telnet(String remoteip, PrintStream out) { 29 | 30 | this.remoteip = remoteip; 31 | this.remoteport = defaultTelnetPort; 32 | this.out = out; 33 | 34 | } 35 | 36 | public Telnet(String remoteip, int remoteport, PrintStream out) { 37 | 38 | this.remoteip = remoteip; 39 | this.remoteport = remoteport; 40 | this.out = out; 41 | 42 | } 43 | 44 | public void execute() { 45 | 46 | end_loop = false; 47 | FileOutputStream fout = null; 48 | try { 49 | fout = new FileOutputStream("spy.log", true); 50 | } catch (IOException ex) { 51 | log.error(ex); 52 | } 53 | tc = new TelnetClient(); 54 | TerminalTypeOptionHandler ttopt = new TerminalTypeOptionHandler("VT100", false, false, true, false); 55 | EchoOptionHandler echoopt = new EchoOptionHandler(true, false, true, false); 56 | SuppressGAOptionHandler gaopt = new SuppressGAOptionHandler(true, true, true, true); 57 | try { 58 | tc.addOptionHandler(ttopt); 59 | tc.addOptionHandler(echoopt); 60 | tc.addOptionHandler(gaopt); 61 | } catch (InvalidTelnetOptionException ex) { 62 | log.error(ex); 63 | } 64 | 65 | while (!end_loop) { 66 | 67 | try { 68 | tc.connect(remoteip, remoteport); 69 | Thread reader = new Thread(new Telnet(remoteip, remoteport, out)); 70 | tc.registerNotifHandler(new Telnet(remoteip, remoteport, out)); 71 | reader.start(); 72 | OutputStream outstr = tc.getOutputStream(); 73 | byte[] buff = new byte[1024]; 74 | ByteArrayInputStream in; 75 | int ret_read = 0; 76 | do { 77 | buff = this.waitCommand(); 78 | in = new ByteArrayInputStream(buff); 79 | try { 80 | ret_read = in.read(buff); 81 | //System.out.print(ret_read + " " + new String(buff, 0, ret_read) + "\n"); 82 | if (ret_read > 0) { 83 | if ((new String(buff, 0, ret_read)).startsWith("AYT")) { 84 | try { 85 | log.info("AYT response:" + tc.sendAYT(5000)); 86 | } catch (IOException e) { 87 | log.error("Exception waiting AYT response: " + e.getMessage()); 88 | } catch (IllegalArgumentException ex) { 89 | log.error(ex); 90 | } catch (InterruptedException ex) { 91 | log.error(ex); 92 | } 93 | } else if ((new String(buff, 0, ret_read)).startsWith("OPT")) { 94 | log.info("Status of options:"); 95 | for (int ii = 0; ii < 25; ii++) { 96 | log.info("Local Option " + ii + ":" + tc.getLocalOptionState(ii) + " Remote Option " + ii + ":" + tc.getRemoteOptionState(ii)); 97 | } 98 | } else if ((new String(buff, 0, ret_read)).startsWith("REGISTER")) { 99 | StringTokenizer st = new StringTokenizer(new String(buff)); 100 | try { 101 | st.nextToken(); 102 | int opcode = Integer.parseInt(st.nextToken()); 103 | boolean initlocal = Boolean.parseBoolean(st.nextToken()); 104 | boolean initremote = Boolean.parseBoolean(st.nextToken()); 105 | boolean acceptlocal = Boolean.parseBoolean(st.nextToken()); 106 | boolean acceptremote = Boolean.parseBoolean(st.nextToken()); 107 | SimpleOptionHandler opthand = new SimpleOptionHandler(opcode, initlocal, initremote, 108 | acceptlocal, acceptremote); 109 | tc.addOptionHandler(opthand); 110 | } catch (NumberFormatException | InvalidTelnetOptionException e) { 111 | if (e instanceof InvalidTelnetOptionException) { 112 | log.error("Error registering option: " + e.getMessage()); 113 | } else { 114 | log.error("Invalid REGISTER command."); 115 | log.error("Use REGISTER optcode initlocal initremote acceptlocal acceptremote"); 116 | log.error("(optcode is an integer.)"); 117 | log.error("(initlocal, initremote, acceptlocal, acceptremote are boolean)"); 118 | } 119 | } 120 | } else if ((new String(buff, 0, ret_read)).startsWith("UNREGISTER")) { 121 | StringTokenizer st = new StringTokenizer(new String(buff)); 122 | try { 123 | st.nextToken(); 124 | int opcode = (new Integer(st.nextToken())).intValue(); 125 | tc.deleteOptionHandler(opcode); 126 | } catch (Exception e) { 127 | if (e instanceof InvalidTelnetOptionException) { 128 | log.error("Error unregistering option: " + e.getMessage()); 129 | } else { 130 | log.error("Invalid UNREGISTER command."); 131 | log.error("Use UNREGISTER optcode"); 132 | log.error("(optcode is an integer)"); 133 | } 134 | } 135 | } else if ((new String(buff, 0, ret_read)).startsWith("SPY")) { 136 | tc.registerSpyStream(fout); 137 | } else if ((new String(buff, 0, ret_read)).startsWith("UNSPY")) { 138 | tc.stopSpyStream(); 139 | } else { 140 | try { 141 | outstr.write(buff, 0, ret_read); 142 | outstr.flush(); 143 | } catch (IOException e) { 144 | end_loop = true; 145 | } 146 | } 147 | } 148 | } catch (IOException e) { 149 | log.error("Exception while reading keyboard:" + e.getMessage()); 150 | end_loop = true; 151 | } 152 | } while ((ret_read > 0) && (end_loop == false)); 153 | 154 | try { 155 | tc.disconnect(); 156 | } catch (IOException e) { 157 | log.error("Exception while connecting:" + e.getMessage()); 158 | 159 | } 160 | } catch (IOException e) { 161 | log.error("Exception while connecting:" + e.getMessage()); 162 | 163 | } 164 | } 165 | } 166 | 167 | /** 168 | * * 169 | * Callback method called when TelnetClient receives an option negotiation 170 | * command. 171 | *

172 | * @param negotiation_code - type of negotiation command received 173 | * (RECEIVED_DO, RECEIVED_DONT, RECEIVED_WILL, RECEIVED_WONT) 174 | *

175 | * @param option_code - code of the option negotiated 176 | *

177 | ** 178 | */ 179 | // @Override 180 | public void receivedNegotiation(int negotiation_code, int option_code) { 181 | String command = null; 182 | if (negotiation_code == TelnetNotificationHandler.RECEIVED_DO) { 183 | command = "DO"; 184 | } else if (negotiation_code == TelnetNotificationHandler.RECEIVED_DONT) { 185 | command = "DONT"; 186 | } else if (negotiation_code == TelnetNotificationHandler.RECEIVED_WILL) { 187 | command = "WILL"; 188 | } else if (negotiation_code == TelnetNotificationHandler.RECEIVED_WONT) { 189 | command = "WONT"; 190 | } 191 | log.info("Received " + command + " for option code " + option_code); 192 | } 193 | 194 | /** 195 | * * 196 | * Reader thread. Reads lines from the TelnetClient and echoes them on the 197 | * screen. * 198 | */ 199 | // @Override 200 | public void run() { 201 | InputStream instr = tc.getInputStream(); 202 | try { 203 | byte[] buff = new byte[1024]; 204 | int ret_read = 0; 205 | 206 | do { 207 | ret_read = instr.read(buff); 208 | if (ret_read > 0) { 209 | 210 | out.print(new String(buff, 0, ret_read)); 211 | } 212 | } while (ret_read >= 0); 213 | } catch (Exception e) { 214 | log.error("Exception while reading socket:" + e.getMessage()); 215 | 216 | } 217 | try { 218 | tc.disconnect(); 219 | } catch (Exception e) { 220 | log.error("Exception while closing telnet:" + e.getMessage()); 221 | 222 | } 223 | } 224 | 225 | @Override 226 | public void disconnect() throws IOException { 227 | end_loop = true; 228 | if (tc.isConnected()) { 229 | tc.disconnect(); 230 | } 231 | 232 | } 233 | 234 | } 235 | -------------------------------------------------------------------------------- /src/main/java/com/github/dmtk/Terminal.java: -------------------------------------------------------------------------------- 1 | package com.github.dmtk; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.IOException; 5 | import java.io.OutputStream; 6 | 7 | import java.nio.CharBuffer; 8 | import java.nio.charset.Charset; 9 | import org.apache.log4j.Logger; 10 | 11 | public abstract class Terminal { 12 | 13 | private final static Logger log = Logger.getLogger(Terminal.class); 14 | protected OutputStream streamOut; 15 | protected boolean end_loop = false; 16 | protected byte[] buff = new byte[1024]; 17 | protected ByteArrayInputStream in = new ByteArrayInputStream(buff); 18 | protected String cmd = ""; 19 | protected boolean interruptWaiting = false; 20 | 21 | public void sendCommand(String command) { 22 | 23 | cmd = command; 24 | cmd = cmd + "\n"; 25 | interruptWaiting = true; 26 | 27 | } 28 | 29 | public byte[] waitCommand() { 30 | 31 | while (!interruptWaiting) { 32 | try { 33 | Thread.sleep(20); 34 | } catch (InterruptedException ex) { 35 | log.error(ex); 36 | } 37 | } 38 | interruptWaiting = false; 39 | char[] chars = cmd.toCharArray(); 40 | byte[] bytes = Charset.forName("ASCII").encode(CharBuffer.wrap(chars)).array();//encode chars to bytes 41 | cmd = ""; 42 | return bytes; 43 | } 44 | 45 | public abstract void disconnect() throws IOException ; 46 | 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/github/dmtk/TextAreaOutputStream.java: -------------------------------------------------------------------------------- 1 | package com.github.dmtk; 2 | 3 | import java.io.IOException; 4 | import java.io.OutputStream; 5 | import javax.swing.JTextArea; 6 | 7 | public class TextAreaOutputStream extends OutputStream { 8 | private JTextArea textArea; 9 | 10 | public TextAreaOutputStream(JTextArea textArea) { 11 | this.textArea = textArea; 12 | } 13 | 14 | @Override 15 | public void write(int b) throws IOException { 16 | // redirects data to the text area 17 | textArea.append(String.valueOf((char)b)); 18 | // scrolls the text area to the end of data 19 | textArea.setCaretPosition(textArea.getDocument().getLength()); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/github/dmtk/Vlan.java: -------------------------------------------------------------------------------- 1 | package com.github.dmtk; 2 | import org.apache.log4j.Logger; 3 | 4 | public class Vlan { 5 | 6 | private int vlanId; 7 | private final int defaultVlan = 2; 8 | private final static Logger log = Logger.getLogger(Vlan.class); 9 | 10 | public Vlan(String vlan) { 11 | 12 | try { 13 | intialize(Integer.parseInt(vlan)); 14 | } catch (NumberFormatException ex) { 15 | log.error("Wrong VLAN number format" + vlan); 16 | } 17 | 18 | } 19 | 20 | public int getId(){ 21 | return vlanId; 22 | } 23 | 24 | public Vlan(int vlan) { 25 | intialize(vlan); 26 | } 27 | 28 | private void intialize(int vlan) { 29 | if (validate(vlan)) { 30 | this.vlanId = vlan; 31 | } else { 32 | this.vlanId = defaultVlan; 33 | log.error("Wrong VLAN number" + vlan); 34 | } 35 | } 36 | 37 | private boolean validate(int vlan) { 38 | return vlan > 0 && vlan < 4094;//VLANs range 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/resources/log4j2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | --------------------------------------------------------------------------------