save
Showing
67 changed files
with
2912 additions
and
0 deletions
DevInfo/.classpath
0 → 100644
| 1 | <?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | <classpath> | ||
| 3 | <classpathentry kind="src" output="target/classes" path="src/main/java"> | ||
| 4 | <attributes> | ||
| 5 | <attribute name="optional" value="true"/> | ||
| 6 | <attribute name="maven.pomderived" value="true"/> | ||
| 7 | </attributes> | ||
| 8 | </classpathentry> | ||
| 9 | <classpathentry kind="src" output="target/test-classes" path="src/test/java"> | ||
| 10 | <attributes> | ||
| 11 | <attribute name="optional" value="true"/> | ||
| 12 | <attribute name="maven.pomderived" value="true"/> | ||
| 13 | </attributes> | ||
| 14 | </classpathentry> | ||
| 15 | <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"> | ||
| 16 | <attributes> | ||
| 17 | <attribute name="maven.pomderived" value="true"/> | ||
| 18 | </attributes> | ||
| 19 | </classpathentry> | ||
| 20 | <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> | ||
| 21 | <attributes> | ||
| 22 | <attribute name="maven.pomderived" value="true"/> | ||
| 23 | </attributes> | ||
| 24 | </classpathentry> | ||
| 25 | <classpathentry kind="output" path="target/classes"/> | ||
| 26 | </classpath> |
DevInfo/.gitignore
0 → 100644
| 1 | /target |
DevInfo/.project
0 → 100644
| 1 | <?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | <projectDescription> | ||
| 3 | <name>DevInfo</name> | ||
| 4 | <comment></comment> | ||
| 5 | <projects> | ||
| 6 | </projects> | ||
| 7 | <buildSpec> | ||
| 8 | <buildCommand> | ||
| 9 | <name>org.eclipse.jdt.core.javabuilder</name> | ||
| 10 | <arguments> | ||
| 11 | </arguments> | ||
| 12 | </buildCommand> | ||
| 13 | <buildCommand> | ||
| 14 | <name>org.eclipse.m2e.core.maven2Builder</name> | ||
| 15 | <arguments> | ||
| 16 | </arguments> | ||
| 17 | </buildCommand> | ||
| 18 | </buildSpec> | ||
| 19 | <natures> | ||
| 20 | <nature>org.eclipse.jdt.core.javanature</nature> | ||
| 21 | <nature>org.eclipse.m2e.core.maven2Nature</nature> | ||
| 22 | </natures> | ||
| 23 | </projectDescription> |
DevInfo/.settings/org.eclipse.jdt.core.prefs
0 → 100644
DevInfo/.settings/org.eclipse.m2e.core.prefs
0 → 100644
DevInfo/DevInfo.log
0 → 100644
File mode changed
DevInfo/DevInfo.xml
0 → 100644
| 1 | <?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | <java version="1.8.0_25" class="java.beans.XMLDecoder"> | ||
| 3 | <object class="DevInfo.DevInfo.DevInfoBean"> | ||
| 4 | <void property="description"> | ||
| 5 | <string>Tes Device</string> | ||
| 6 | </void> | ||
| 7 | <void property="guid"> | ||
| 8 | <string>dsdd-3424-sad-asdas-sddd</string> | ||
| 9 | </void> | ||
| 10 | </object> | ||
| 11 | </java> |
DevInfo/pom.xml
0 → 100644
| 1 | <project> | ||
| 2 | <modelVersion>4.0.0</modelVersion> | ||
| 3 | |||
| 4 | <groupId>DevInfo</groupId> | ||
| 5 | <artifactId>DevInfo</artifactId> | ||
| 6 | <version>0.0.1-SNAPSHOT</version> | ||
| 7 | <packaging>jar</packaging> | ||
| 8 | |||
| 9 | <dependencies> | ||
| 10 | |||
| 11 | |||
| 12 | |||
| 13 | <dependency> | ||
| 14 | <groupId>net.sourceforge.argparse4j</groupId> | ||
| 15 | <artifactId>argparse4j</artifactId> | ||
| 16 | <version>0.6.0</version> | ||
| 17 | </dependency> | ||
| 18 | |||
| 19 | |||
| 20 | <dependency> | ||
| 21 | <groupId>log4j</groupId> | ||
| 22 | <artifactId>log4j</artifactId> | ||
| 23 | <version>1.2.17</version> | ||
| 24 | </dependency> | ||
| 25 | |||
| 26 | <dependency> | ||
| 27 | <groupId>junit</groupId> | ||
| 28 | <artifactId>junit</artifactId> | ||
| 29 | <version>4.12</version> | ||
| 30 | </dependency> | ||
| 31 | |||
| 32 | </dependencies> | ||
| 33 | </project> | ||
| 34 | |||
| 35 | |||
| 36 | |||
| 37 | |||
| 38 | |||
| 39 | |||
| 40 | |||
| 41 |
| 1 | package org.emercit.devinfo; | ||
| 2 | |||
| 3 | import java.io.IOException; | ||
| 4 | import java.nio.ByteBuffer; | ||
| 5 | import java.nio.channels.ByteChannel; | ||
| 6 | import java.nio.file.Files; | ||
| 7 | import java.nio.file.Path; | ||
| 8 | import java.nio.file.Paths; | ||
| 9 | import java.nio.file.StandardOpenOption; | ||
| 10 | import java.security.MessageDigest; | ||
| 11 | import java.security.NoSuchAlgorithmException; | ||
| 12 | |||
| 13 | import net.sourceforge.argparse4j.ArgumentParsers; | ||
| 14 | import net.sourceforge.argparse4j.impl.Arguments; | ||
| 15 | import net.sourceforge.argparse4j.inf.ArgumentParser; | ||
| 16 | import net.sourceforge.argparse4j.inf.ArgumentParserException; | ||
| 17 | import net.sourceforge.argparse4j.inf.Namespace; | ||
| 18 | |||
| 19 | import org.apache.log4j.Logger; | ||
| 20 | import org.apache.log4j.ConsoleAppender; | ||
| 21 | import org.apache.log4j.Level; | ||
| 22 | import org.apache.log4j.PatternLayout; | ||
| 23 | import org.apache.log4j.RollingFileAppender; | ||
| 24 | import org.emercit.devinfo.model.DevInfoBean; | ||
| 25 | import org.emercit.devinfo.service.DevInfoService; | ||
| 26 | |||
| 27 | |||
| 28 | public class App | ||
| 29 | { | ||
| 30 | private static final Logger log = Logger.getLogger(App.class); | ||
| 31 | |||
| 32 | |||
| 33 | private static String method; | ||
| 34 | private static String pr; | ||
| 35 | private static String value; | ||
| 36 | |||
| 37 | |||
| 38 | public static void main( String[] args ) | ||
| 39 | { | ||
| 40 | ArgumentParser parser = ArgumentParsers.newArgumentParser("prog"); | ||
| 41 | |||
| 42 | |||
| 43 | |||
| 44 | try { | ||
| 45 | |||
| 46 | parser.description("Информация об устройстве") | ||
| 47 | .version("Version:1.0.0.0") | ||
| 48 | .defaultHelp(true); | ||
| 49 | |||
| 50 | parser.addArgument("-v") | ||
| 51 | .type(String.class) | ||
| 52 | .setDefault("") | ||
| 53 | .help("Значение параметра"); | ||
| 54 | |||
| 55 | parser.addArgument("-m") | ||
| 56 | .type(String.class) | ||
| 57 | .setDefault("get") | ||
| 58 | .help("Функция. Получение параметров - get, установка параметров - set, создание файла DevInfo.xml - create"); | ||
| 59 | |||
| 60 | parser.addArgument("-log") | ||
| 61 | .type(Integer.class) | ||
| 62 | .setDefault(0) | ||
| 63 | .help("Логирование. 0-выкл.; 1-консоль; 2-файл"); | ||
| 64 | |||
| 65 | |||
| 66 | parser.addArgument("-p") | ||
| 67 | .type(String.class) | ||
| 68 | .setDefault("get") | ||
| 69 | .help("Параметр, который хотим прочитать или изменить"); | ||
| 70 | |||
| 71 | |||
| 72 | Logger rootLogger = Logger.getRootLogger(); | ||
| 73 | PatternLayout layout = new PatternLayout("%d{ISO8601} [%t] %-5p %c %x - %m%n"); | ||
| 74 | |||
| 75 | //rootLogger.setLevel(Level.OFF); | ||
| 76 | rootLogger.setLevel(Level.DEBUG); | ||
| 77 | |||
| 78 | rootLogger.addAppender(new ConsoleAppender(layout)); | ||
| 79 | try | ||
| 80 | { | ||
| 81 | RollingFileAppender fileAppender = new RollingFileAppender(layout, "/var/log/devtools/DevInfo.log"); | ||
| 82 | rootLogger.addAppender(fileAppender); | ||
| 83 | } | ||
| 84 | catch (IOException e) | ||
| 85 | { | ||
| 86 | log.error(e.getMessage()); | ||
| 87 | } | ||
| 88 | |||
| 89 | |||
| 90 | |||
| 91 | Namespace res = parser.parseArgs(args); | ||
| 92 | |||
| 93 | method=res.getString("m"); | ||
| 94 | pr=res.getString("p"); | ||
| 95 | |||
| 96 | if (method.equals("set")) { // Установка параметров | ||
| 97 | |||
| 98 | value = res.getString("v"); | ||
| 99 | |||
| 100 | |||
| 101 | DevInfoBean m=DevInfoService.INSTANCE.Get(); | ||
| 102 | |||
| 103 | if (pr.equals("title")) { | ||
| 104 | m.setDescription(value); | ||
| 105 | }else if (pr.equals("guid")) { | ||
| 106 | m.setGuid(value); | ||
| 107 | } | ||
| 108 | |||
| 109 | DevInfoService.INSTANCE.Set(m); | ||
| 110 | |||
| 111 | }else if (method.equals("get")) { // Получение параметров | ||
| 112 | |||
| 113 | DevInfoBean m=DevInfoService.INSTANCE.Get(); | ||
| 114 | |||
| 115 | if (pr.equals("title")) { | ||
| 116 | System.out.println(m.getDescription()); | ||
| 117 | }else if (pr.equals("guid")) { | ||
| 118 | System.out.println(m.getGuid()); | ||
| 119 | } | ||
| 120 | }else if (method.equals("create")) { | ||
| 121 | DevInfoBean db=new DevInfoBean(); | ||
| 122 | db.setDescription(""); | ||
| 123 | db.setGuid(""); | ||
| 124 | DevInfoService.INSTANCE.Set(db); | ||
| 125 | |||
| 126 | } | ||
| 127 | |||
| 128 | }catch(Exception e){ | ||
| 129 | |||
| 130 | |||
| 131 | |||
| 132 | if(e instanceof ArgumentParserException){ | ||
| 133 | log.error(e.getMessage()); | ||
| 134 | } else { | ||
| 135 | parser.handleError((ArgumentParserException) e); | ||
| 136 | log.error(e.getMessage()); | ||
| 137 | System.exit(1); | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | |||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | |||
| 146 | |||
| 147 | |||
| 148 | |||
| 149 | |||
| 150 | |||
| 151 | |||
| 152 | |||
| 153 | |||
| 154 | |||
| 155 | |||
| 156 | |||
| 157 |
| 1 | package org.emercit.devinfo.model; | ||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 | |||
| 6 | import java.io.Serializable; | ||
| 7 | import java.util.Vector; | ||
| 8 | |||
| 9 | public class DevInfoBean implements Serializable { | ||
| 10 | |||
| 11 | |||
| 12 | private String guid; | ||
| 13 | |||
| 14 | private String description; | ||
| 15 | |||
| 16 | |||
| 17 | public DevInfoBean() { | ||
| 18 | |||
| 19 | } | ||
| 20 | |||
| 21 | public String getGuid() { | ||
| 22 | return guid; | ||
| 23 | } | ||
| 24 | |||
| 25 | public void setGuid(String value) { | ||
| 26 | this.guid=value; | ||
| 27 | } | ||
| 28 | |||
| 29 | public String getDescription() { | ||
| 30 | return description; | ||
| 31 | } | ||
| 32 | |||
| 33 | public void setDescription(String value) { | ||
| 34 | this.description=value; | ||
| 35 | } | ||
| 36 | |||
| 37 | } | ||
| 38 |
| 1 | package org.emercit.devinfo.service; | ||
| 2 | |||
| 3 | import java.beans.XMLDecoder; | ||
| 4 | import java.beans.XMLEncoder; | ||
| 5 | import java.io.BufferedInputStream; | ||
| 6 | import java.io.BufferedOutputStream; | ||
| 7 | import java.io.FileInputStream; | ||
| 8 | import java.io.FileOutputStream; | ||
| 9 | |||
| 10 | import org.apache.log4j.Logger; | ||
| 11 | import org.emercit.devinfo.App; | ||
| 12 | import org.emercit.devinfo.model.DevInfoBean; | ||
| 13 | |||
| 14 | public class DevInfoImpl implements IDevInfo { | ||
| 15 | |||
| 16 | private static final Logger log = Logger.getLogger(DevInfoImpl.class); | ||
| 17 | |||
| 18 | public boolean Set(DevInfoBean m) { | ||
| 19 | |||
| 20 | try { | ||
| 21 | |||
| 22 | FileOutputStream fos = new FileOutputStream("/opt/devtools/beans/DevInfo.xml"); | ||
| 23 | BufferedOutputStream bos = new BufferedOutputStream(fos); | ||
| 24 | XMLEncoder xmlEncoder = new XMLEncoder(bos); | ||
| 25 | xmlEncoder.writeObject(m); | ||
| 26 | xmlEncoder.close(); | ||
| 27 | return true; | ||
| 28 | }catch(Exception e) { | ||
| 29 | log.error(e.getMessage()); | ||
| 30 | return false; | ||
| 31 | } | ||
| 32 | |||
| 33 | |||
| 34 | |||
| 35 | } | ||
| 36 | |||
| 37 | public DevInfoBean Get() { | ||
| 38 | try { | ||
| 39 | XMLDecoder d = new XMLDecoder( | ||
| 40 | new BufferedInputStream( | ||
| 41 | new FileInputStream("/opt/devtools/beans/DevInfo.xml"))); | ||
| 42 | DevInfoBean result =(DevInfoBean) d.readObject(); | ||
| 43 | d.close(); | ||
| 44 | |||
| 45 | return result; | ||
| 46 | }catch(Exception e) { | ||
| 47 | log.error(e.getMessage()); | ||
| 48 | return null; | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | } |
| 1 | package org.emercit.devinfo.service; | ||
| 2 | |||
| 3 | import java.beans.XMLEncoder; | ||
| 4 | import java.beans.XMLDecoder; | ||
| 5 | import java.io.BufferedOutputStream; | ||
| 6 | import java.io.BufferedInputStream; | ||
| 7 | import java.io.FileOutputStream; | ||
| 8 | import java.io.FileInputStream; | ||
| 9 | |||
| 10 | import org.emercit.devinfo.model.DevInfoBean; | ||
| 11 | |||
| 12 | public enum DevInfoService { | ||
| 13 | |||
| 14 | INSTANCE; | ||
| 15 | |||
| 16 | public boolean Set(DevInfoBean m) throws Exception { | ||
| 17 | boolean result=false; | ||
| 18 | |||
| 19 | FileOutputStream fos = new FileOutputStream("/opt/devtools/beans/DevInfo.xml"); | ||
| 20 | BufferedOutputStream bos = new BufferedOutputStream(fos); | ||
| 21 | XMLEncoder xmlEncoder = new XMLEncoder(bos); | ||
| 22 | xmlEncoder.writeObject(m); | ||
| 23 | xmlEncoder.close(); | ||
| 24 | |||
| 25 | return result; | ||
| 26 | |||
| 27 | } | ||
| 28 | |||
| 29 | public DevInfoBean Get() throws Exception { | ||
| 30 | |||
| 31 | DevInfoBean result=null; | ||
| 32 | XMLDecoder d = new XMLDecoder( | ||
| 33 | |||
| 34 | new BufferedInputStream( | ||
| 35 | new FileInputStream("/opt/devtools/beans/DevInfo.xml"))); | ||
| 36 | result =(DevInfoBean) d.readObject(); | ||
| 37 | d.close(); | ||
| 38 | return result; | ||
| 39 | } | ||
| 40 | |||
| 41 | } |
| 1 | package org.emercit.devinfo.service; | ||
| 2 | |||
| 3 | import java.beans.XMLDecoder; | ||
| 4 | import java.beans.XMLEncoder; | ||
| 5 | import java.io.BufferedInputStream; | ||
| 6 | import java.io.BufferedOutputStream; | ||
| 7 | import java.io.FileInputStream; | ||
| 8 | import java.io.FileOutputStream; | ||
| 9 | |||
| 10 | import org.emercit.devinfo.model.DevInfoBean; | ||
| 11 | |||
| 12 | public interface IDevInfo { | ||
| 13 | |||
| 14 | |||
| 15 | public boolean Set(DevInfoBean m); | ||
| 16 | |||
| 17 | |||
| 18 | public DevInfoBean Get(); | ||
| 19 | |||
| 20 | |||
| 21 | |||
| 22 | |||
| 23 | |||
| 24 | } |
| 1 | package org.emercit.devinfo; | ||
| 2 | |||
| 3 | import junit.framework.Test; | ||
| 4 | import junit.framework.TestCase; | ||
| 5 | import junit.framework.TestSuite; | ||
| 6 | |||
| 7 | /** | ||
| 8 | * Unit test for simple App. | ||
| 9 | */ | ||
| 10 | public class AppTest | ||
| 11 | extends TestCase | ||
| 12 | { | ||
| 13 | /** | ||
| 14 | * Create the test case | ||
| 15 | * | ||
| 16 | * @param testName name of the test case | ||
| 17 | */ | ||
| 18 | public AppTest( String testName ) | ||
| 19 | { | ||
| 20 | super( testName ); | ||
| 21 | } | ||
| 22 | |||
| 23 | /** | ||
| 24 | * @return the suite of tests being tested | ||
| 25 | */ | ||
| 26 | public static Test suite() | ||
| 27 | { | ||
| 28 | return new TestSuite( AppTest.class ); | ||
| 29 | } | ||
| 30 | |||
| 31 | /** | ||
| 32 | * Rigourous Test :-) | ||
| 33 | */ | ||
| 34 | public void testApp() | ||
| 35 | { | ||
| 36 | assertTrue( true ); | ||
| 37 | } | ||
| 38 | } |
DnsManager/.classpath
0 → 100644
| 1 | <?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | <classpath> | ||
| 3 | <classpathentry kind="src" output="target/classes" path="src/main/java"> | ||
| 4 | <attributes> | ||
| 5 | <attribute name="optional" value="true"/> | ||
| 6 | <attribute name="maven.pomderived" value="true"/> | ||
| 7 | </attributes> | ||
| 8 | </classpathentry> | ||
| 9 | <classpathentry kind="src" output="target/test-classes" path="src/test/java"> | ||
| 10 | <attributes> | ||
| 11 | <attribute name="optional" value="true"/> | ||
| 12 | <attribute name="maven.pomderived" value="true"/> | ||
| 13 | </attributes> | ||
| 14 | </classpathentry> | ||
| 15 | <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"> | ||
| 16 | <attributes> | ||
| 17 | <attribute name="maven.pomderived" value="true"/> | ||
| 18 | </attributes> | ||
| 19 | </classpathentry> | ||
| 20 | <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> | ||
| 21 | <attributes> | ||
| 22 | <attribute name="maven.pomderived" value="true"/> | ||
| 23 | </attributes> | ||
| 24 | </classpathentry> | ||
| 25 | <classpathentry kind="output" path="target/classes"/> | ||
| 26 | </classpath> |
DnsManager/.gitignore
0 → 100644
| 1 | /target |
DnsManager/.project
0 → 100644
| 1 | <?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | <projectDescription> | ||
| 3 | <name>DnsManager</name> | ||
| 4 | <comment></comment> | ||
| 5 | <projects> | ||
| 6 | </projects> | ||
| 7 | <buildSpec> | ||
| 8 | <buildCommand> | ||
| 9 | <name>org.eclipse.jdt.core.javabuilder</name> | ||
| 10 | <arguments> | ||
| 11 | </arguments> | ||
| 12 | </buildCommand> | ||
| 13 | <buildCommand> | ||
| 14 | <name>org.eclipse.m2e.core.maven2Builder</name> | ||
| 15 | <arguments> | ||
| 16 | </arguments> | ||
| 17 | </buildCommand> | ||
| 18 | </buildSpec> | ||
| 19 | <natures> | ||
| 20 | <nature>org.eclipse.jdt.core.javanature</nature> | ||
| 21 | <nature>org.eclipse.m2e.core.maven2Nature</nature> | ||
| 22 | </natures> | ||
| 23 | </projectDescription> |
DnsManager/pom.xml
0 → 100644
| 1 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| 2 | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| 3 | <modelVersion>4.0.0</modelVersion> | ||
| 4 | |||
| 5 | <groupId>DnsServer</groupId> | ||
| 6 | <artifactId>DnsServer</artifactId> | ||
| 7 | <version>0.0.1-SNAPSHOT</version> | ||
| 8 | <packaging>jar</packaging> | ||
| 9 | |||
| 10 | <name>DnsServer</name> | ||
| 11 | <url>http://maven.apache.org</url> | ||
| 12 | |||
| 13 | <properties> | ||
| 14 | <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| 15 | <java-version>1.7</java-version> | ||
| 16 | </properties> | ||
| 17 | |||
| 18 | <dependencies> | ||
| 19 | |||
| 20 | <dependency> | ||
| 21 | <groupId>net.sourceforge.argparse4j</groupId> | ||
| 22 | <artifactId>argparse4j</artifactId> | ||
| 23 | <version>0.6.0</version> | ||
| 24 | </dependency> | ||
| 25 | |||
| 26 | <dependency> | ||
| 27 | <groupId>log4j</groupId> | ||
| 28 | <artifactId>log4j</artifactId> | ||
| 29 | <version>1.2.17</version> | ||
| 30 | </dependency> | ||
| 31 | |||
| 32 | <dependency> | ||
| 33 | <groupId>junit</groupId> | ||
| 34 | <artifactId>junit</artifactId> | ||
| 35 | <version>4.12</version> | ||
| 36 | </dependency> | ||
| 37 | |||
| 38 | <dependency> | ||
| 39 | <groupId>com.cedarsoftware</groupId> | ||
| 40 | <artifactId>json-io</artifactId> | ||
| 41 | <version>4.0.0</version> | ||
| 42 | </dependency> | ||
| 43 | </dependencies> | ||
| 44 | </project> | ||
| 45 | |||
| 46 | |||
| 47 | |||
| 48 | |||
| 49 | |||
| 50 | |||
| 51 | |||
| 52 | |||
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | |||
| 58 |
| 1 | package org.emercit.dnsmanager; | ||
| 2 | |||
| 3 | import java.io.IOException; | ||
| 4 | import java.nio.ByteBuffer; | ||
| 5 | import java.nio.channels.ByteChannel; | ||
| 6 | import java.nio.file.Files; | ||
| 7 | import java.nio.file.Path; | ||
| 8 | import java.nio.file.Paths; | ||
| 9 | import java.nio.file.StandardOpenOption; | ||
| 10 | import java.security.MessageDigest; | ||
| 11 | import java.security.NoSuchAlgorithmException; | ||
| 12 | |||
| 13 | import net.sourceforge.argparse4j.ArgumentParsers; | ||
| 14 | import net.sourceforge.argparse4j.impl.Arguments; | ||
| 15 | import net.sourceforge.argparse4j.inf.ArgumentParser; | ||
| 16 | import net.sourceforge.argparse4j.inf.ArgumentParserException; | ||
| 17 | import net.sourceforge.argparse4j.inf.Namespace; | ||
| 18 | |||
| 19 | import org.apache.log4j.Logger; | ||
| 20 | import org.apache.log4j.ConsoleAppender; | ||
| 21 | import org.apache.log4j.Level; | ||
| 22 | import org.apache.log4j.PatternLayout; | ||
| 23 | import org.apache.log4j.RollingFileAppender; | ||
| 24 | import org.emercit.dnsmanager.service.DnsLinux; | ||
| 25 | |||
| 26 | public class App | ||
| 27 | { | ||
| 28 | |||
| 29 | private static final Logger log = Logger.getLogger(App.class); | ||
| 30 | |||
| 31 | private static DnsLinux dnslinux=new DnsLinux(); | ||
| 32 | |||
| 33 | private static String method; | ||
| 34 | |||
| 35 | private static String param; | ||
| 36 | |||
| 37 | private static String value; | ||
| 38 | |||
| 39 | |||
| 40 | public static void main( String[] args ) | ||
| 41 | { | ||
| 42 | ArgumentParser parser = ArgumentParsers.newArgumentParser("prog"); | ||
| 43 | |||
| 44 | |||
| 45 | try { | ||
| 46 | |||
| 47 | parser.description("Конфигуратор DNS") | ||
| 48 | .version("Version:1.0.0.0") | ||
| 49 | .defaultHelp(true); | ||
| 50 | |||
| 51 | parser.addArgument("-m") | ||
| 52 | .type(String.class) | ||
| 53 | .setDefault("get") | ||
| 54 | .help("Методы: \n" | ||
| 55 | + "1) get - получение всех IP DNS серверов;\n" | ||
| 56 | + "2) add - добавление IP адреса DNS сервера;\n" | ||
| 57 | + "3) del - удаление IP DNS сервера.\n"); | ||
| 58 | |||
| 59 | parser.addArgument("-v") | ||
| 60 | .type(String.class) | ||
| 61 | .setDefault("") | ||
| 62 | .help("Значение параметра"); | ||
| 63 | |||
| 64 | parser.addArgument("-p") | ||
| 65 | .type(String.class) | ||
| 66 | .setDefault("") | ||
| 67 | .help("Имя параметра.\n Возможные параметры:\n" | ||
| 68 | +"1) ip - адрес DNS сервера\n"); | ||
| 69 | |||
| 70 | parser.addArgument("-e") | ||
| 71 | .type(String.class) | ||
| 72 | .setDefault("") | ||
| 73 | .help("Название сетевого интерфейса"); | ||
| 74 | |||
| 75 | parser.addArgument("-log") | ||
| 76 | .type(Integer.class) | ||
| 77 | .setDefault(0) | ||
| 78 | .help("Логирование. 0-выкл.; 1-консоль; 2-файл"); | ||
| 79 | |||
| 80 | Logger rootLogger = Logger.getRootLogger(); | ||
| 81 | PatternLayout layout = new PatternLayout("%d{ISO8601} [%t] %-5p %c %x - %m%n"); | ||
| 82 | |||
| 83 | //rootLogger.setLevel(Level.OFF); | ||
| 84 | rootLogger.setLevel(Level.DEBUG); | ||
| 85 | |||
| 86 | rootLogger.addAppender(new ConsoleAppender(layout)); | ||
| 87 | try { | ||
| 88 | RollingFileAppender fileAppender = new RollingFileAppender(layout, "/var/log/devtools/DnsManager.log"); | ||
| 89 | rootLogger.addAppender(fileAppender); | ||
| 90 | } catch (IOException e) { | ||
| 91 | log.error(e.getMessage()); | ||
| 92 | } | ||
| 93 | |||
| 94 | Namespace res = parser.parseArgs(args); | ||
| 95 | |||
| 96 | |||
| 97 | method=res.getString("m"); | ||
| 98 | |||
| 99 | if (method.equals("add")) { //Добавление Ip DNS сервера | ||
| 100 | |||
| 101 | value=res.getString("v"); | ||
| 102 | dnslinux.Add(value); | ||
| 103 | |||
| 104 | }else if (method.equals("get")) { //Вывод списка DNS в JSON формате | ||
| 105 | |||
| 106 | dnslinux.PrintInfoDnsAll(); | ||
| 107 | |||
| 108 | }else if (method.equals("del")) { // Удаление IP адреса DNS | ||
| 109 | |||
| 110 | value=res.getString("v"); | ||
| 111 | dnslinux.Delete(value); | ||
| 112 | } | ||
| 113 | |||
| 114 | }catch(Exception e){ | ||
| 115 | |||
| 116 | if(e instanceof ArgumentParserException){ | ||
| 117 | log.error(e.getMessage()); | ||
| 118 | } | ||
| 119 | else { | ||
| 120 | parser.handleError((ArgumentParserException) e); | ||
| 121 | log.error(e.getMessage()); | ||
| 122 | System.exit(1); | ||
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | |||
| 130 | |||
| 131 | |||
| 132 | |||
| 133 | |||
| 134 | |||
| 135 | |||
| 136 | |||
| 137 | |||
| 138 |
| 1 | package org.emercit.dnsmanager.model; | ||
| 2 | |||
| 3 | |||
| 4 | import java.io.BufferedReader; | ||
| 5 | import java.io.InputStreamReader; | ||
| 6 | import java.util.List; | ||
| 7 | import java.util.ArrayList; | ||
| 8 | import java.util.Random; | ||
| 9 | |||
| 10 | import java.util.Vector; | ||
| 11 | |||
| 12 | |||
| 13 | |||
| 14 | public class CommandExecutor { | ||
| 15 | |||
| 16 | |||
| 17 | public Vector<String> Run(String cmd) throws Exception { | ||
| 18 | |||
| 19 | Vector<String> v=new Vector<String>(); | ||
| 20 | |||
| 21 | Runtime run = Runtime.getRuntime(); | ||
| 22 | |||
| 23 | Process proc = run.exec(new String[]{"/bin/sh", "-c",cmd}); | ||
| 24 | proc.waitFor(); | ||
| 25 | BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream())); | ||
| 26 | while(br.ready()) { | ||
| 27 | v.add(br.readLine()); | ||
| 28 | } | ||
| 29 | |||
| 30 | return v; | ||
| 31 | } | ||
| 32 | } |
| 1 | package org.emercit.dnsmanager.model; | ||
| 2 | |||
| 3 | import java.io.Serializable; | ||
| 4 | |||
| 5 | public class DnsBean implements Serializable { | ||
| 6 | |||
| 7 | |||
| 8 | private int id; | ||
| 9 | |||
| 10 | private String ip; | ||
| 11 | |||
| 12 | public DnsBean() { | ||
| 13 | |||
| 14 | } | ||
| 15 | |||
| 16 | public void setIp(String value) { | ||
| 17 | this.ip=value; | ||
| 18 | } | ||
| 19 | |||
| 20 | public String getIp() { | ||
| 21 | return ip; | ||
| 22 | } | ||
| 23 | |||
| 24 | public void setId(int value) { | ||
| 25 | this.id=value; | ||
| 26 | } | ||
| 27 | |||
| 28 | public int getId() { | ||
| 29 | return id; | ||
| 30 | } | ||
| 31 | |||
| 32 | |||
| 33 | |||
| 34 | |||
| 35 | } |
| 1 | package org.emercit.dnsmanager.service; | ||
| 2 | |||
| 3 | import java.io.BufferedWriter; | ||
| 4 | import java.io.File; | ||
| 5 | import java.io.FileWriter; | ||
| 6 | import java.io.IOException; | ||
| 7 | import java.util.Vector; | ||
| 8 | |||
| 9 | import org.apache.log4j.Logger; | ||
| 10 | import org.emercit.dnsmanager.model.DnsBean; | ||
| 11 | |||
| 12 | import com.cedarsoftware.util.io.JsonWriter; | ||
| 13 | |||
| 14 | import org.emercit.dnsmanager.model.CommandExecutor; | ||
| 15 | |||
| 16 | |||
| 17 | public class DnsLinux implements IDns{ | ||
| 18 | |||
| 19 | private static final Logger log = Logger.getLogger(DnsLinux.class); | ||
| 20 | |||
| 21 | |||
| 22 | private CommandExecutor ce=new CommandExecutor(); | ||
| 23 | |||
| 24 | private Vector<DnsBean> dbs; | ||
| 25 | |||
| 26 | private DnsBean db; | ||
| 27 | |||
| 28 | private String json; | ||
| 29 | |||
| 30 | |||
| 31 | public void Add (String value){ | ||
| 32 | |||
| 33 | dbs=getAllDns(); | ||
| 34 | |||
| 35 | db=new DnsBean(); | ||
| 36 | |||
| 37 | db.setIp(value); | ||
| 38 | |||
| 39 | dbs.add(db); | ||
| 40 | |||
| 41 | Config(dbs); | ||
| 42 | |||
| 43 | } | ||
| 44 | |||
| 45 | |||
| 46 | /* | ||
| 47 | * Вывод на экран списка DNS серверов в JSON формате | ||
| 48 | * @see org.emerct.dnsmanager.service.IDns#PrintInfoDnsAll() | ||
| 49 | */ | ||
| 50 | public void PrintInfoDnsAll() { | ||
| 51 | |||
| 52 | dbs=getAllDns(); | ||
| 53 | |||
| 54 | try { | ||
| 55 | json = JsonWriter.objectToJson(dbs); | ||
| 56 | System.out.println(json); | ||
| 57 | |||
| 58 | }catch(Exception e) { | ||
| 59 | log.error(e.getMessage()); | ||
| 60 | } | ||
| 61 | |||
| 62 | |||
| 63 | } | ||
| 64 | |||
| 65 | public void Delete(String value) { | ||
| 66 | |||
| 67 | dbs=getAllDns(); | ||
| 68 | for (DnsBean db:dbs) { | ||
| 69 | |||
| 70 | if (db.getIp().equals(value)) { | ||
| 71 | dbs.remove(db); | ||
| 72 | break; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | Config(dbs); | ||
| 76 | } | ||
| 77 | |||
| 78 | |||
| 79 | public boolean Update(Vector<DnsBean> beans) { | ||
| 80 | |||
| 81 | |||
| 82 | return Config(beans); | ||
| 83 | |||
| 84 | } | ||
| 85 | |||
| 86 | |||
| 87 | |||
| 88 | |||
| 89 | |||
| 90 | |||
| 91 | |||
| 92 | public Vector<DnsBean> getAllDns() { | ||
| 93 | |||
| 94 | dbs=new Vector<DnsBean>(); | ||
| 95 | try { | ||
| 96 | Vector<String> v=ce.Run("cat /etc/resolv.conf | grep nameserver | awk '{print $2}'"); | ||
| 97 | |||
| 98 | int i=0; | ||
| 99 | for (String ip:v) { | ||
| 100 | |||
| 101 | |||
| 102 | db=new DnsBean(); | ||
| 103 | db.setIp(ip); | ||
| 104 | db.setId(i); | ||
| 105 | dbs.add(db); | ||
| 106 | i++; | ||
| 107 | } | ||
| 108 | |||
| 109 | } catch(Exception e) { | ||
| 110 | log.error(e.getMessage()); | ||
| 111 | } | ||
| 112 | return dbs; | ||
| 113 | } | ||
| 114 | |||
| 115 | |||
| 116 | |||
| 117 | public boolean Config(Vector<DnsBean> dbs) { | ||
| 118 | |||
| 119 | try { | ||
| 120 | File file = new File("/etc/resolv.conf"); | ||
| 121 | |||
| 122 | if (!file.exists()) { | ||
| 123 | file.createNewFile(); | ||
| 124 | } | ||
| 125 | |||
| 126 | FileWriter fw = new FileWriter(file.getAbsoluteFile()); | ||
| 127 | BufferedWriter bw = new BufferedWriter(fw); | ||
| 128 | |||
| 129 | bw.write("#Generated by DnsManager\n"); | ||
| 130 | bw.write("\n"); | ||
| 131 | |||
| 132 | for (DnsBean b:dbs) { | ||
| 133 | |||
| 134 | bw.write("nameserver "+b.getIp()); | ||
| 135 | bw.write("\n"); | ||
| 136 | } | ||
| 137 | |||
| 138 | bw.close(); | ||
| 139 | return true; | ||
| 140 | |||
| 141 | } catch (IOException e) { | ||
| 142 | |||
| 143 | log.error(e.getMessage()); | ||
| 144 | |||
| 145 | } | ||
| 146 | return false; | ||
| 147 | } | ||
| 148 | |||
| 149 | |||
| 150 | |||
| 151 | } |
| 1 | package org.emercit.dnsmanager.service; | ||
| 2 | |||
| 3 | import org.emercit.dnsmanager.model.DnsBean; | ||
| 4 | |||
| 5 | import java.util.Vector; | ||
| 6 | |||
| 7 | public interface IDns { | ||
| 8 | |||
| 9 | public void Add (String value); | ||
| 10 | |||
| 11 | public void PrintInfoDnsAll(); | ||
| 12 | |||
| 13 | public void Delete(String value); | ||
| 14 | |||
| 15 | public boolean Config(Vector<DnsBean> dbs); | ||
| 16 | |||
| 17 | public Vector<DnsBean> getAllDns(); | ||
| 18 | |||
| 19 | } | ||
| 20 | |||
| 21 | |||
| 22 | |||
| 23 | |||
| 24 |
| 1 | package DnsServer.DnsServer; | ||
| 2 | |||
| 3 | import junit.framework.Test; | ||
| 4 | import junit.framework.TestCase; | ||
| 5 | import junit.framework.TestSuite; | ||
| 6 | |||
| 7 | /** | ||
| 8 | * Unit test for simple App. | ||
| 9 | */ | ||
| 10 | public class AppTest | ||
| 11 | extends TestCase | ||
| 12 | { | ||
| 13 | /** | ||
| 14 | * Create the test case | ||
| 15 | * | ||
| 16 | * @param testName name of the test case | ||
| 17 | */ | ||
| 18 | public AppTest( String testName ) | ||
| 19 | { | ||
| 20 | super( testName ); | ||
| 21 | } | ||
| 22 | |||
| 23 | /** | ||
| 24 | * @return the suite of tests being tested | ||
| 25 | */ | ||
| 26 | public static Test suite() | ||
| 27 | { | ||
| 28 | return new TestSuite( AppTest.class ); | ||
| 29 | } | ||
| 30 | |||
| 31 | /** | ||
| 32 | * Rigourous Test :-) | ||
| 33 | */ | ||
| 34 | public void testApp() | ||
| 35 | { | ||
| 36 | assertTrue( true ); | ||
| 37 | } | ||
| 38 | } |
EthManager/.classpath
0 → 100644
| 1 | <?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | <classpath> | ||
| 3 | <classpathentry kind="src" output="target/classes" path="src/main/java"> | ||
| 4 | <attributes> | ||
| 5 | <attribute name="optional" value="true"/> | ||
| 6 | <attribute name="maven.pomderived" value="true"/> | ||
| 7 | </attributes> | ||
| 8 | </classpathentry> | ||
| 9 | <classpathentry kind="src" output="target/test-classes" path="src/test/java"> | ||
| 10 | <attributes> | ||
| 11 | <attribute name="optional" value="true"/> | ||
| 12 | <attribute name="maven.pomderived" value="true"/> | ||
| 13 | </attributes> | ||
| 14 | </classpathentry> | ||
| 15 | <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"> | ||
| 16 | <attributes> | ||
| 17 | <attribute name="maven.pomderived" value="true"/> | ||
| 18 | </attributes> | ||
| 19 | </classpathentry> | ||
| 20 | <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> | ||
| 21 | <attributes> | ||
| 22 | <attribute name="maven.pomderived" value="true"/> | ||
| 23 | </attributes> | ||
| 24 | </classpathentry> | ||
| 25 | <classpathentry kind="output" path="target/classes"/> | ||
| 26 | </classpath> |
EthManager/.gitignore
0 → 100644
| 1 | /target |
EthManager/.project
0 → 100644
| 1 | <?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | <projectDescription> | ||
| 3 | <name>EthManager</name> | ||
| 4 | <comment></comment> | ||
| 5 | <projects> | ||
| 6 | </projects> | ||
| 7 | <buildSpec> | ||
| 8 | <buildCommand> | ||
| 9 | <name>org.eclipse.jdt.core.javabuilder</name> | ||
| 10 | <arguments> | ||
| 11 | </arguments> | ||
| 12 | </buildCommand> | ||
| 13 | <buildCommand> | ||
| 14 | <name>org.eclipse.m2e.core.maven2Builder</name> | ||
| 15 | <arguments> | ||
| 16 | </arguments> | ||
| 17 | </buildCommand> | ||
| 18 | </buildSpec> | ||
| 19 | <natures> | ||
| 20 | <nature>org.eclipse.jdt.core.javanature</nature> | ||
| 21 | <nature>org.eclipse.m2e.core.maven2Nature</nature> | ||
| 22 | </natures> | ||
| 23 | </projectDescription> |
| 1 | eclipse.preferences.version=1 | ||
| 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled | ||
| 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 | ||
| 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve | ||
| 5 | org.eclipse.jdt.core.compiler.compliance=1.7 | ||
| 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate | ||
| 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate | ||
| 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate | ||
| 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error | ||
| 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error | ||
| 11 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning | ||
| 12 | org.eclipse.jdt.core.compiler.source=1.7 |
EthManager/pom.xml
0 → 100644
| 1 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| 2 | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| 3 | <modelVersion>4.0.0</modelVersion> | ||
| 4 | |||
| 5 | <groupId>NetConfig</groupId> | ||
| 6 | <artifactId>NetConfig</artifactId> | ||
| 7 | <version>0.0.1-SNAPSHOT</version> | ||
| 8 | <packaging>jar</packaging> | ||
| 9 | |||
| 10 | <name>NetConfig</name> | ||
| 11 | <url>http://maven.apache.org</url> | ||
| 12 | |||
| 13 | |||
| 14 | <properties> | ||
| 15 | <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| 16 | <java-version>1.7</java-version> | ||
| 17 | |||
| 18 | </properties> | ||
| 19 | |||
| 20 | |||
| 21 | <dependencies> | ||
| 22 | |||
| 23 | <dependency> | ||
| 24 | <groupId>net.sourceforge.argparse4j</groupId> | ||
| 25 | <artifactId>argparse4j</artifactId> | ||
| 26 | <version>0.6.0</version> | ||
| 27 | </dependency> | ||
| 28 | |||
| 29 | |||
| 30 | <dependency> | ||
| 31 | <groupId>log4j</groupId> | ||
| 32 | <artifactId>log4j</artifactId> | ||
| 33 | <version>1.2.17</version> | ||
| 34 | </dependency> | ||
| 35 | |||
| 36 | <dependency> | ||
| 37 | <groupId>junit</groupId> | ||
| 38 | <artifactId>junit</artifactId> | ||
| 39 | <version>4.12</version> | ||
| 40 | </dependency> | ||
| 41 | |||
| 42 | |||
| 43 | <dependency> | ||
| 44 | <groupId>com.cedarsoftware</groupId> | ||
| 45 | <artifactId>json-io</artifactId> | ||
| 46 | <version>4.0.0</version> | ||
| 47 | </dependency> | ||
| 48 | |||
| 49 | </dependencies> | ||
| 50 | </project> |
| 1 | package org.emercit.ethmanager; | ||
| 2 | |||
| 3 | |||
| 4 | import java.io.IOException; | ||
| 5 | import java.nio.ByteBuffer; | ||
| 6 | import java.nio.channels.ByteChannel; | ||
| 7 | import java.nio.file.Files; | ||
| 8 | import java.nio.file.Path; | ||
| 9 | import java.nio.file.Paths; | ||
| 10 | import java.nio.file.StandardOpenOption; | ||
| 11 | import java.security.MessageDigest; | ||
| 12 | import java.security.NoSuchAlgorithmException; | ||
| 13 | import java.util.Vector; | ||
| 14 | |||
| 15 | import net.sourceforge.argparse4j.ArgumentParsers; | ||
| 16 | import net.sourceforge.argparse4j.impl.Arguments; | ||
| 17 | import net.sourceforge.argparse4j.inf.ArgumentParser; | ||
| 18 | import net.sourceforge.argparse4j.inf.ArgumentParserException; | ||
| 19 | import net.sourceforge.argparse4j.inf.Namespace; | ||
| 20 | |||
| 21 | import org.apache.log4j.Logger; | ||
| 22 | import org.apache.log4j.ConsoleAppender; | ||
| 23 | import org.apache.log4j.Level; | ||
| 24 | import org.apache.log4j.PatternLayout; | ||
| 25 | import org.apache.log4j.RollingFileAppender; | ||
| 26 | import org.emercit.ethmanager.service.EthLinux; | ||
| 27 | |||
| 28 | |||
| 29 | |||
| 30 | public class App | ||
| 31 | { | ||
| 32 | |||
| 33 | private static final Logger log = Logger.getLogger(App.class); | ||
| 34 | |||
| 35 | private static EthLinux ethlinux=new EthLinux(); | ||
| 36 | |||
| 37 | public static void main( String[] args ) | ||
| 38 | { | ||
| 39 | ArgumentParser parser = ArgumentParsers.newArgumentParser("prog"); | ||
| 40 | |||
| 41 | String eth; //Название интерфейса | ||
| 42 | String param; //Параметр который нужно изменить | ||
| 43 | String method; // Метод | ||
| 44 | String value; // Значение параметра | ||
| 45 | String name; // Название интерфейса | ||
| 46 | |||
| 47 | try { | ||
| 48 | |||
| 49 | parser.description("Конфигуратор сетевых интерфейсов") | ||
| 50 | .version("Version:1.0.0.0") | ||
| 51 | .defaultHelp(true); | ||
| 52 | |||
| 53 | parser.addArgument("-m") | ||
| 54 | .type(String.class) | ||
| 55 | .setDefault("get") | ||
| 56 | .help("Методы: \n" | ||
| 57 | + "1) get - получение параметра;\n" | ||
| 58 | + "2) set - установка параметра;\n" | ||
| 59 | + "3) reset-сброс сетевых настроек на интерфейсах;\n" | ||
| 60 | + "4) eths- список сетевых интерфейсов.\n"); | ||
| 61 | |||
| 62 | parser.addArgument("-v") | ||
| 63 | .type(String.class) | ||
| 64 | .setDefault("") | ||
| 65 | .help("Значение параметра"); | ||
| 66 | |||
| 67 | parser.addArgument("-p") | ||
| 68 | .type(String.class) | ||
| 69 | .setDefault("") | ||
| 70 | .help("Имя параметра.\n Возможные параметры:\n" | ||
| 71 | +"1) dhcp - может принимать значение true или false;\n" | ||
| 72 | +"2) ip - адресс;\n" | ||
| 73 | +"3) netmask - маска сети;\n" | ||
| 74 | +"4) gateway - основной шлюз.\n"); | ||
| 75 | |||
| 76 | parser.addArgument("-e") | ||
| 77 | .type(String.class) | ||
| 78 | .setDefault("") | ||
| 79 | .help("Название сетевого интерфейса"); | ||
| 80 | |||
| 81 | parser.addArgument("-log") | ||
| 82 | .type(Integer.class) | ||
| 83 | .setDefault(0) | ||
| 84 | .help("Логирование. 0-выкл.; 1-консоль; 2-файл"); | ||
| 85 | |||
| 86 | Logger rootLogger = Logger.getRootLogger(); | ||
| 87 | PatternLayout layout = new PatternLayout("%d{ISO8601} [%t] %-5p %c %x - %m%n"); | ||
| 88 | |||
| 89 | //rootLogger.setLevel(Level.OFF); | ||
| 90 | rootLogger.setLevel(Level.DEBUG); | ||
| 91 | |||
| 92 | rootLogger.addAppender(new ConsoleAppender(layout)); | ||
| 93 | try { | ||
| 94 | RollingFileAppender fileAppender = new RollingFileAppender(layout, "/var/log/devtools/EthManager.log"); | ||
| 95 | rootLogger.addAppender(fileAppender); | ||
| 96 | } catch (IOException e) { | ||
| 97 | log.error(e.getMessage()); | ||
| 98 | } | ||
| 99 | |||
| 100 | Namespace res = parser.parseArgs(args); | ||
| 101 | |||
| 102 | // param=res.getString("p"); | ||
| 103 | method=res.getString("m"); | ||
| 104 | |||
| 105 | if (method.equals("set")) { //Конфигурация интерфейса | ||
| 106 | |||
| 107 | name=res.getString("e"); | ||
| 108 | param= res.getString("p"); | ||
| 109 | value=res.getString("v"); | ||
| 110 | |||
| 111 | ethlinux.ApplyConfig(name, param,(Object)value ); | ||
| 112 | |||
| 113 | }else if (method.equals("get")) { //Вывод информации из файла | ||
| 114 | |||
| 115 | name=res.getString("e"); | ||
| 116 | |||
| 117 | ethlinux.PrintInfoByName(name); | ||
| 118 | }else if (method.equals("reset")) { // Сброс сетевых настроек | ||
| 119 | ethlinux.setDefaults(); | ||
| 120 | }else if (method.equals("eths")) { | ||
| 121 | Vector<String> v=ethlinux.getListEth(); | ||
| 122 | for (String s:v) { | ||
| 123 | System.out.println(s); | ||
| 124 | } | ||
| 125 | |||
| 126 | } | ||
| 127 | |||
| 128 | }catch(Exception e){ | ||
| 129 | |||
| 130 | if(e instanceof ArgumentParserException){ | ||
| 131 | log.error(e.getMessage()); | ||
| 132 | } | ||
| 133 | else { | ||
| 134 | parser.handleError((ArgumentParserException) e); | ||
| 135 | log.error(e.getMessage()); | ||
| 136 | System.exit(1); | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | |||
| 144 | |||
| 145 | |||
| 146 | |||
| 147 | |||
| 148 | |||
| 149 | |||
| 150 | |||
| 151 |
| 1 | package org.emercit.ethmanager.model; | ||
| 2 | |||
| 3 | |||
| 4 | import java.io.BufferedReader; | ||
| 5 | import java.io.InputStreamReader; | ||
| 6 | import java.util.List; | ||
| 7 | import java.util.ArrayList; | ||
| 8 | import java.util.Random; | ||
| 9 | |||
| 10 | import java.util.Vector; | ||
| 11 | |||
| 12 | |||
| 13 | |||
| 14 | public class CommandExecutor { | ||
| 15 | |||
| 16 | |||
| 17 | public Vector<String> Run(String cmd) throws Exception { | ||
| 18 | |||
| 19 | Vector<String> v=new Vector<String>(); | ||
| 20 | |||
| 21 | Runtime run = Runtime.getRuntime(); | ||
| 22 | |||
| 23 | Process proc = run.exec(new String[]{"/bin/sh", "-c",cmd}); | ||
| 24 | proc.waitFor(); | ||
| 25 | BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream())); | ||
| 26 | while(br.ready()) { | ||
| 27 | v.add(br.readLine()); | ||
| 28 | } | ||
| 29 | |||
| 30 | return v; | ||
| 31 | } | ||
| 32 | } | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| 1 | package org.emercit.ethmanager.model; | ||
| 2 | |||
| 3 | |||
| 4 | import java.io.Serializable; | ||
| 5 | import java.util.Vector; | ||
| 6 | |||
| 7 | public class EthBean implements Serializable { | ||
| 8 | |||
| 9 | private static final long serialVersionUID = 1L; | ||
| 10 | |||
| 11 | |||
| 12 | private int id; | ||
| 13 | |||
| 14 | private String name; | ||
| 15 | |||
| 16 | private String mac; | ||
| 17 | |||
| 18 | private boolean dhcp; | ||
| 19 | |||
| 20 | private String ip; | ||
| 21 | |||
| 22 | private String netmask; | ||
| 23 | |||
| 24 | private String gateway; | ||
| 25 | |||
| 26 | private String broadcast; | ||
| 27 | |||
| 28 | private Vector<String> info; | ||
| 29 | |||
| 30 | |||
| 31 | |||
| 32 | public EthBean() { | ||
| 33 | |||
| 34 | } | ||
| 35 | |||
| 36 | |||
| 37 | public int getId() { | ||
| 38 | return this.id; | ||
| 39 | } | ||
| 40 | |||
| 41 | |||
| 42 | public void setId(int value) { | ||
| 43 | this.id=value; | ||
| 44 | } | ||
| 45 | |||
| 46 | public String getName() { | ||
| 47 | return name; | ||
| 48 | } | ||
| 49 | |||
| 50 | public void setName(String value) { | ||
| 51 | this.name=value; | ||
| 52 | } | ||
| 53 | |||
| 54 | public String getMac() { | ||
| 55 | return mac; | ||
| 56 | } | ||
| 57 | |||
| 58 | |||
| 59 | public void setMac(String value) { | ||
| 60 | this.mac=value; | ||
| 61 | } | ||
| 62 | |||
| 63 | public boolean getDhcp() { | ||
| 64 | return this.dhcp; | ||
| 65 | } | ||
| 66 | |||
| 67 | public void setDhcp(boolean value) { | ||
| 68 | this.dhcp = value; | ||
| 69 | } | ||
| 70 | |||
| 71 | public String getIp() { | ||
| 72 | return ip; | ||
| 73 | } | ||
| 74 | |||
| 75 | public void setIp(String value) { | ||
| 76 | this.ip = value; | ||
| 77 | } | ||
| 78 | |||
| 79 | public String getNetmask() { | ||
| 80 | return netmask; | ||
| 81 | } | ||
| 82 | |||
| 83 | public void setNetmask(String value) { | ||
| 84 | this.netmask=value; | ||
| 85 | } | ||
| 86 | |||
| 87 | public String getGateway() { | ||
| 88 | return gateway; | ||
| 89 | } | ||
| 90 | |||
| 91 | public void setGateway(String value) { | ||
| 92 | this.gateway=value; | ||
| 93 | } | ||
| 94 | |||
| 95 | public String getBroadcast() { | ||
| 96 | return broadcast; | ||
| 97 | } | ||
| 98 | |||
| 99 | public void setBroadcast(String value) { | ||
| 100 | this.broadcast=value; | ||
| 101 | } | ||
| 102 | |||
| 103 | public Vector<String> getInfo() { | ||
| 104 | return info; | ||
| 105 | } | ||
| 106 | |||
| 107 | public void setInfo(Vector<String> value) { | ||
| 108 | this.info=value; | ||
| 109 | } | ||
| 110 | |||
| 111 | } | ||
| 112 |
| 1 | package org.emercit.ethmanager.service; | ||
| 2 | |||
| 3 | import java.util.Vector; | ||
| 4 | import java.beans.XMLEncoder; | ||
| 5 | import java.beans.XMLDecoder; | ||
| 6 | import java.io.BufferedOutputStream; | ||
| 7 | import java.io.BufferedInputStream; | ||
| 8 | import java.io.FileOutputStream; | ||
| 9 | import java.io.FileInputStream; | ||
| 10 | |||
| 11 | import org.apache.log4j.Logger; | ||
| 12 | import org.emercit.ethmanager.App; | ||
| 13 | import org.emercit.ethmanager.model.EthBean; | ||
| 14 | |||
| 15 | public class EthDBImpl implements IEthDB { | ||
| 16 | |||
| 17 | private static final Logger log = Logger.getLogger(EthDBImpl.class); | ||
| 18 | |||
| 19 | public boolean updateDb(Vector<EthBean> m) { | ||
| 20 | |||
| 21 | boolean result=true; | ||
| 22 | try { | ||
| 23 | FileOutputStream fos = new FileOutputStream("/opt/devtools/beans/EthBeans.xml"); | ||
| 24 | BufferedOutputStream bos = new BufferedOutputStream(fos); | ||
| 25 | XMLEncoder xmlEncoder = new XMLEncoder(bos); | ||
| 26 | xmlEncoder.writeObject(m); | ||
| 27 | xmlEncoder.close(); | ||
| 28 | }catch(Exception e) { | ||
| 29 | log.error(e.getMessage()); | ||
| 30 | result=false; | ||
| 31 | } | ||
| 32 | return result; | ||
| 33 | } | ||
| 34 | |||
| 35 | public Vector<EthBean> selectDb() { | ||
| 36 | |||
| 37 | Vector<EthBean> result=new Vector<EthBean>(); | ||
| 38 | |||
| 39 | try { | ||
| 40 | XMLDecoder d = new XMLDecoder( | ||
| 41 | |||
| 42 | new BufferedInputStream( | ||
| 43 | new FileInputStream("/opt/devtools/beans/EthBeans.xml"))); | ||
| 44 | result=(Vector<EthBean>)d.readObject(); | ||
| 45 | d.close(); | ||
| 46 | }catch(Exception e) { | ||
| 47 | log.error(e.getMessage()); | ||
| 48 | result=null; | ||
| 49 | } | ||
| 50 | return result; | ||
| 51 | } | ||
| 52 | |||
| 53 | } | ||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | |||
| 58 | |||
| 59 | |||
| 60 | |||
| 61 | |||
| 62 |
| 1 | package org.emercit.ethmanager.service; | ||
| 2 | |||
| 3 | import org.apache.log4j.Logger; | ||
| 4 | import org.emercit.ethmanager.model.EthBean; | ||
| 5 | |||
| 6 | import com.cedarsoftware.util.io.JsonReader; | ||
| 7 | import com.cedarsoftware.util.io.JsonWriter; | ||
| 8 | |||
| 9 | import java.util.Vector; | ||
| 10 | import java.io.File; | ||
| 11 | import java.io.FileWriter; | ||
| 12 | import java.io.BufferedWriter; | ||
| 13 | import java.io.IOException; | ||
| 14 | |||
| 15 | import org.emercit.ethmanager.model.CommandExecutor; | ||
| 16 | |||
| 17 | public class EthLinux implements IEth { | ||
| 18 | |||
| 19 | private static final Logger log = Logger.getLogger(EthLinux.class); | ||
| 20 | |||
| 21 | private CommandExecutor ce=new CommandExecutor(); | ||
| 22 | |||
| 23 | private EthDBImpl ethStore=new EthDBImpl(); | ||
| 24 | |||
| 25 | private Vector<EthBean> ebs; | ||
| 26 | |||
| 27 | private Vector<String> extinfo; | ||
| 28 | |||
| 29 | private String mac; | ||
| 30 | |||
| 31 | private String json; | ||
| 32 | |||
| 33 | private Object obj; | ||
| 34 | |||
| 35 | private EthBean emp; | ||
| 36 | |||
| 37 | |||
| 38 | public void ApplyConfig (String eth, String p, Object v) { | ||
| 39 | |||
| 40 | ebs=ethStore.selectDb(); | ||
| 41 | |||
| 42 | if (!p.equals("object")) { | ||
| 43 | |||
| 44 | for(EthBean eb : ebs) { | ||
| 45 | if(eb.getName().equals(eth)) { | ||
| 46 | |||
| 47 | switch(p) { | ||
| 48 | case "dhcp": eb.setDhcp(Boolean.valueOf((String)v)); break; | ||
| 49 | case "ip": eb.setIp((String)v); break; | ||
| 50 | case "netmask": eb.setNetmask((String)v); break; | ||
| 51 | case "gateway": eb.setGateway((String)v); break; | ||
| 52 | |||
| 53 | default: break; | ||
| 54 | } | ||
| 55 | break; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | }else { | ||
| 59 | obj = JsonReader.jsonToJava((String)json); | ||
| 60 | emp = (EthBean) obj; | ||
| 61 | |||
| 62 | for(EthBean eb : ebs) { | ||
| 63 | if(eb.getName().equals(emp.getName())) { | ||
| 64 | |||
| 65 | eb.setDhcp(emp.getDhcp()); | ||
| 66 | eb.setIp(emp.getIp()); | ||
| 67 | eb.setNetmask(emp.getNetmask()); | ||
| 68 | eb.setGateway(emp.getGateway()); | ||
| 69 | } | ||
| 70 | break; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | |||
| 75 | Config(ebs); | ||
| 76 | } | ||
| 77 | |||
| 78 | /* | ||
| 79 | * Список интерфейсов Ethernet на данном устройстве | ||
| 80 | * @see org.emercit.ethmanager.service.IEth#getListEth() | ||
| 81 | */ | ||
| 82 | public Vector<String> getListEth() { | ||
| 83 | |||
| 84 | Vector<String> result=new Vector<String>(); | ||
| 85 | |||
| 86 | try { | ||
| 87 | Vector<String> buff=ce.Run("ifconfig | grep eth | awk '{print $1}'"); | ||
| 88 | |||
| 89 | ebs=ethStore.selectDb(); | ||
| 90 | |||
| 91 | for (EthBean eb:ebs) { | ||
| 92 | |||
| 93 | for (String ebbuff:buff) { | ||
| 94 | |||
| 95 | if (eb.getName().equals(ebbuff)) { | ||
| 96 | result.add(ebbuff); | ||
| 97 | //System.out.println(ebbuff); | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | } | ||
| 102 | |||
| 103 | }catch(Exception e) { | ||
| 104 | log.error(e.getMessage()); | ||
| 105 | } | ||
| 106 | return result; | ||
| 107 | } | ||
| 108 | |||
| 109 | |||
| 110 | public void PrintInfoByName(String name) { | ||
| 111 | |||
| 112 | ebs=ethStore.selectDb(); | ||
| 113 | |||
| 114 | |||
| 115 | |||
| 116 | for(EthBean eb : ebs) { | ||
| 117 | |||
| 118 | if(eb.getName().equals(name)) { | ||
| 119 | |||
| 120 | try { | ||
| 121 | extinfo=ce.Run("ifconfig"+" "+eb.getName()); | ||
| 122 | |||
| 123 | mac=ce.Run("ifconfig"+" "+eb.getName()+" | grep HWaddr | awk '{ print $5}' ").toString(); | ||
| 124 | |||
| 125 | eb.setInfo(extinfo); | ||
| 126 | eb.setMac(mac); | ||
| 127 | |||
| 128 | if (eb.getDhcp()) { | ||
| 129 | |||
| 130 | String addr=ce.Run("ifconfig"+" "+eb.getName()+" "+"| grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'").toString(); | ||
| 131 | String mask=ce.Run("ifconfig"+" "+eb.getName()+" "+"| grep 'inet addr:' | cut -d: -f4 | awk '{ print $1}'").toString(); | ||
| 132 | String gateway=ce.Run("/sbin/route -n | grep 'UG' | awk '{print $2}'").toString(); | ||
| 133 | |||
| 134 | eb.setIp(addr); | ||
| 135 | eb.setNetmask(mask); | ||
| 136 | eb.setGateway(gateway); | ||
| 137 | } | ||
| 138 | |||
| 139 | json = JsonWriter.objectToJson(eb); | ||
| 140 | |||
| 141 | System.out.println(json); | ||
| 142 | |||
| 143 | }catch(Exception e) { | ||
| 144 | log.error(e.getMessage()); | ||
| 145 | } | ||
| 146 | |||
| 147 | |||
| 148 | |||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | } | ||
| 153 | |||
| 154 | |||
| 155 | |||
| 156 | |||
| 157 | |||
| 158 | public EthBean getEthByName(String name) { | ||
| 159 | |||
| 160 | ebs=ethStore.selectDb(); | ||
| 161 | |||
| 162 | for(EthBean eb : ebs) { | ||
| 163 | |||
| 164 | if(eb.getName().equals(name)) { | ||
| 165 | |||
| 166 | try { | ||
| 167 | extinfo=ce.Run("ifconfig"+" "+eb.getName()); | ||
| 168 | |||
| 169 | mac=ce.Run("ifconfig"+" "+eb.getName()+" | grep HWaddr | awk '{ print $5}' ").toString(); | ||
| 170 | |||
| 171 | eb.setInfo(extinfo); | ||
| 172 | eb.setMac(mac); | ||
| 173 | |||
| 174 | if (eb.getDhcp()) { | ||
| 175 | |||
| 176 | String addr=ce.Run("ifconfig"+" "+eb.getName()+" "+"| grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'").toString(); | ||
| 177 | String mask=ce.Run("ifconfig"+" "+eb.getName()+" "+"| grep 'inet addr:' | cut -d: -f4 | awk '{ print $1}'").toString(); | ||
| 178 | String gateway=ce.Run("/sbin/route -n | grep 'UG' | awk '{print $2}'").toString(); | ||
| 179 | |||
| 180 | eb.setIp(addr); | ||
| 181 | eb.setNetmask(mask); | ||
| 182 | eb.setGateway(gateway); | ||
| 183 | } | ||
| 184 | |||
| 185 | return eb; | ||
| 186 | |||
| 187 | }catch(Exception e) { | ||
| 188 | log.error(e.getMessage()); | ||
| 189 | } | ||
| 190 | |||
| 191 | |||
| 192 | } | ||
| 193 | } | ||
| 194 | return null; | ||
| 195 | } | ||
| 196 | |||
| 197 | |||
| 198 | |||
| 199 | public boolean Update(EthBean m) { | ||
| 200 | |||
| 201 | ebs=ethStore.selectDb(); | ||
| 202 | |||
| 203 | for (EthBean eb:ebs) { | ||
| 204 | |||
| 205 | if (eb.getName().equals(m.getName())) { | ||
| 206 | |||
| 207 | eb.setId(m.getId()); | ||
| 208 | eb.setBroadcast(m.getBroadcast()); | ||
| 209 | eb.setGateway(m.getGateway()); | ||
| 210 | eb.setIp(m.getIp()); | ||
| 211 | eb.setNetmask(m.getNetmask()); | ||
| 212 | eb.setDhcp(m.getDhcp()); | ||
| 213 | |||
| 214 | break; | ||
| 215 | } | ||
| 216 | |||
| 217 | } | ||
| 218 | Config(ebs); | ||
| 219 | |||
| 220 | return true; | ||
| 221 | } | ||
| 222 | |||
| 223 | |||
| 224 | |||
| 225 | |||
| 226 | private void Config(Vector<EthBean> ebs) { | ||
| 227 | |||
| 228 | try { | ||
| 229 | File file = new File("/etc/network/interfaces"); | ||
| 230 | |||
| 231 | if (!file.exists()) { | ||
| 232 | file.createNewFile(); | ||
| 233 | } | ||
| 234 | |||
| 235 | FileWriter fw = new FileWriter(file.getAbsoluteFile()); | ||
| 236 | BufferedWriter bw = new BufferedWriter(fw); | ||
| 237 | bw.write("#Generated by EthManager \n"); | ||
| 238 | bw.write("\n"); | ||
| 239 | bw.write("auto lo"); | ||
| 240 | bw.write("\n"); | ||
| 241 | bw.write("iface lo inet loopback"); | ||
| 242 | bw.write("\n"); | ||
| 243 | bw.write("\n"); | ||
| 244 | |||
| 245 | int id=0; | ||
| 246 | |||
| 247 | for (EthBean eb:ebs) { | ||
| 248 | |||
| 249 | eb.setId(id); | ||
| 250 | |||
| 251 | if (!eb.getDhcp()) { | ||
| 252 | |||
| 253 | bw.write("auto "+eb.getName()); | ||
| 254 | bw.write("\n"); | ||
| 255 | bw.write("iface "+eb.getName()+" inet static"); | ||
| 256 | bw.write("\n"); | ||
| 257 | bw.write("address "+eb.getIp()); | ||
| 258 | bw.write("\n"); | ||
| 259 | bw.write("netmask "+eb.getNetmask()); | ||
| 260 | bw.write("\n"); | ||
| 261 | bw.write("gateway "+eb.getGateway()); | ||
| 262 | bw.write("\n"); | ||
| 263 | |||
| 264 | } else { | ||
| 265 | bw.write("auto "+eb.getName()); | ||
| 266 | bw.write("\n"); | ||
| 267 | bw.write("iface "+eb.getName()+" inet dhcp"); | ||
| 268 | bw.write("\n"); | ||
| 269 | } | ||
| 270 | |||
| 271 | bw.write("\n"); | ||
| 272 | bw.write("\n"); | ||
| 273 | |||
| 274 | id++; | ||
| 275 | } | ||
| 276 | |||
| 277 | bw.close(); | ||
| 278 | |||
| 279 | ethStore.updateDb(ebs); | ||
| 280 | |||
| 281 | } catch (IOException e) { | ||
| 282 | log.error(e.getMessage()); | ||
| 283 | } | ||
| 284 | |||
| 285 | } | ||
| 286 | |||
| 287 | public void setDefaults() { | ||
| 288 | |||
| 289 | |||
| 290 | ebs=ethStore.selectDb(); | ||
| 291 | Vector<String> v=new Vector<String>(); | ||
| 292 | v.add(""); | ||
| 293 | if (ebs==null || ebs.size()==0) { | ||
| 294 | ebs=new Vector<EthBean>(); | ||
| 295 | |||
| 296 | EthBean NewEb=new EthBean(); | ||
| 297 | |||
| 298 | //Eth0 | ||
| 299 | NewEb.setName("eth0"); | ||
| 300 | NewEb.setBroadcast(""); | ||
| 301 | NewEb.setDhcp(false); | ||
| 302 | NewEb.setIp("192.168.7.100"); | ||
| 303 | NewEb.setNetmask("255.255.255.0"); | ||
| 304 | NewEb.setGateway("192.168.7.1"); | ||
| 305 | NewEb.setInfo(v); | ||
| 306 | NewEb.setId(0); | ||
| 307 | |||
| 308 | ebs.add(NewEb); | ||
| 309 | |||
| 310 | //Eth1 | ||
| 311 | NewEb=new EthBean(); | ||
| 312 | |||
| 313 | NewEb.setName("eth1"); | ||
| 314 | NewEb.setDhcp(true); | ||
| 315 | NewEb.setIp(""); | ||
| 316 | NewEb.setNetmask(""); | ||
| 317 | NewEb.setGateway(""); | ||
| 318 | NewEb.setBroadcast(""); | ||
| 319 | NewEb.setMac(""); | ||
| 320 | NewEb.setId(1); | ||
| 321 | NewEb.setInfo(v); | ||
| 322 | |||
| 323 | |||
| 324 | |||
| 325 | ebs.add(NewEb); | ||
| 326 | |||
| 327 | } | ||
| 328 | Config(ebs); | ||
| 329 | } | ||
| 330 | |||
| 331 | } | ||
| 332 | |||
| 333 |
| 1 | package org.emercit.ethmanager.service; | ||
| 2 | |||
| 3 | |||
| 4 | import java.util.Vector; | ||
| 5 | |||
| 6 | import org.emercit.ethmanager.model.*; | ||
| 7 | |||
| 8 | public interface IEth { | ||
| 9 | |||
| 10 | public void ApplyConfig (String eth, String p, Object v); | ||
| 11 | |||
| 12 | public void PrintInfoByName(String name); | ||
| 13 | |||
| 14 | public void setDefaults(); | ||
| 15 | |||
| 16 | public boolean Update(EthBean m); | ||
| 17 | |||
| 18 | public EthBean getEthByName(String name); | ||
| 19 | |||
| 20 | public Vector<String> getListEth(); | ||
| 21 | |||
| 22 | } |
| 1 | package org.emercit.netmanager; | ||
| 2 | |||
| 3 | import junit.framework.Test; | ||
| 4 | import junit.framework.TestCase; | ||
| 5 | import junit.framework.TestSuite; | ||
| 6 | |||
| 7 | /** | ||
| 8 | * Unit test for simple App. | ||
| 9 | */ | ||
| 10 | public class AppTest | ||
| 11 | extends TestCase | ||
| 12 | { | ||
| 13 | /** | ||
| 14 | * Create the test case | ||
| 15 | * | ||
| 16 | * @param testName name of the test case | ||
| 17 | */ | ||
| 18 | public AppTest( String testName ) | ||
| 19 | { | ||
| 20 | super( testName ); | ||
| 21 | } | ||
| 22 | |||
| 23 | /** | ||
| 24 | * @return the suite of tests being tested | ||
| 25 | */ | ||
| 26 | public static Test suite() | ||
| 27 | { | ||
| 28 | return new TestSuite( AppTest.class ); | ||
| 29 | } | ||
| 30 | |||
| 31 | /** | ||
| 32 | * Rigourous Test :-) | ||
| 33 | */ | ||
| 34 | public void testApp() | ||
| 35 | { | ||
| 36 | assertTrue( true ); | ||
| 37 | } | ||
| 38 | } |
ProxyManager/.classpath
0 → 100644
| 1 | <?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | <classpath> | ||
| 3 | <classpathentry kind="src" output="target/classes" path="src/main/java"> | ||
| 4 | <attributes> | ||
| 5 | <attribute name="optional" value="true"/> | ||
| 6 | <attribute name="maven.pomderived" value="true"/> | ||
| 7 | </attributes> | ||
| 8 | </classpathentry> | ||
| 9 | <classpathentry kind="src" output="target/test-classes" path="src/test/java"> | ||
| 10 | <attributes> | ||
| 11 | <attribute name="optional" value="true"/> | ||
| 12 | <attribute name="maven.pomderived" value="true"/> | ||
| 13 | </attributes> | ||
| 14 | </classpathentry> | ||
| 15 | <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"> | ||
| 16 | <attributes> | ||
| 17 | <attribute name="maven.pomderived" value="true"/> | ||
| 18 | </attributes> | ||
| 19 | </classpathentry> | ||
| 20 | <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> | ||
| 21 | <attributes> | ||
| 22 | <attribute name="maven.pomderived" value="true"/> | ||
| 23 | </attributes> | ||
| 24 | </classpathentry> | ||
| 25 | <classpathentry kind="output" path="target/classes"/> | ||
| 26 | </classpath> |
ProxyManager/.gitignore
0 → 100644
| 1 | /target |
ProxyManager/.project
0 → 100644
| 1 | <?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | <projectDescription> | ||
| 3 | <name>ProxyManager</name> | ||
| 4 | <comment></comment> | ||
| 5 | <projects> | ||
| 6 | </projects> | ||
| 7 | <buildSpec> | ||
| 8 | <buildCommand> | ||
| 9 | <name>org.eclipse.jdt.core.javabuilder</name> | ||
| 10 | <arguments> | ||
| 11 | </arguments> | ||
| 12 | </buildCommand> | ||
| 13 | <buildCommand> | ||
| 14 | <name>org.eclipse.m2e.core.maven2Builder</name> | ||
| 15 | <arguments> | ||
| 16 | </arguments> | ||
| 17 | </buildCommand> | ||
| 18 | </buildSpec> | ||
| 19 | <natures> | ||
| 20 | <nature>org.eclipse.jdt.core.javanature</nature> | ||
| 21 | <nature>org.eclipse.m2e.core.maven2Nature</nature> | ||
| 22 | </natures> | ||
| 23 | </projectDescription> |
| 1 | eclipse.preferences.version=1 | ||
| 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled | ||
| 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 | ||
| 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve | ||
| 5 | org.eclipse.jdt.core.compiler.compliance=1.7 | ||
| 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate | ||
| 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate | ||
| 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate | ||
| 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error | ||
| 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error | ||
| 11 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning | ||
| 12 | org.eclipse.jdt.core.compiler.source=1.7 |
ProxyManager/pom.xml
0 → 100644
| 1 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| 2 | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| 3 | <modelVersion>4.0.0</modelVersion> | ||
| 4 | |||
| 5 | <groupId>ProxyManager</groupId> | ||
| 6 | <artifactId>ProxyManager</artifactId> | ||
| 7 | <version>0.0.1-SNAPSHOT</version> | ||
| 8 | <packaging>jar</packaging> | ||
| 9 | |||
| 10 | <name>ProxyManager</name> | ||
| 11 | <url>http://maven.apache.org</url> | ||
| 12 | |||
| 13 | <properties> | ||
| 14 | <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| 15 | <java-version>1.7</java-version> | ||
| 16 | </properties> | ||
| 17 | |||
| 18 | <dependencies> | ||
| 19 | |||
| 20 | <dependency> | ||
| 21 | <groupId>net.sourceforge.argparse4j</groupId> | ||
| 22 | <artifactId>argparse4j</artifactId> | ||
| 23 | <version>0.6.0</version> | ||
| 24 | </dependency> | ||
| 25 | |||
| 26 | <dependency> | ||
| 27 | <groupId>log4j</groupId> | ||
| 28 | <artifactId>log4j</artifactId> | ||
| 29 | <version>1.2.17</version> | ||
| 30 | </dependency> | ||
| 31 | |||
| 32 | <dependency> | ||
| 33 | <groupId>junit</groupId> | ||
| 34 | <artifactId>junit</artifactId> | ||
| 35 | <version>4.12</version> | ||
| 36 | </dependency> | ||
| 37 | |||
| 38 | <dependency> | ||
| 39 | <groupId>com.cedarsoftware</groupId> | ||
| 40 | <artifactId>json-io</artifactId> | ||
| 41 | <version>4.0.0</version> | ||
| 42 | </dependency> | ||
| 43 | |||
| 44 | </dependencies> | ||
| 45 | </project> | ||
| 46 | |||
| 47 | |||
| 48 | </dependencies> | ||
| 49 | </project> |
| 1 | package org.emercit.proxymanager; | ||
| 2 | |||
| 3 | |||
| 4 | import java.io.IOException; | ||
| 5 | import java.nio.ByteBuffer; | ||
| 6 | import java.nio.channels.ByteChannel; | ||
| 7 | import java.nio.file.Files; | ||
| 8 | import java.nio.file.Path; | ||
| 9 | import java.nio.file.Paths; | ||
| 10 | import java.nio.file.StandardOpenOption; | ||
| 11 | import java.security.MessageDigest; | ||
| 12 | import java.security.NoSuchAlgorithmException; | ||
| 13 | |||
| 14 | import net.sourceforge.argparse4j.ArgumentParsers; | ||
| 15 | import net.sourceforge.argparse4j.impl.Arguments; | ||
| 16 | import net.sourceforge.argparse4j.inf.ArgumentParser; | ||
| 17 | import net.sourceforge.argparse4j.inf.ArgumentParserException; | ||
| 18 | import net.sourceforge.argparse4j.inf.Namespace; | ||
| 19 | |||
| 20 | import org.apache.log4j.Logger; | ||
| 21 | import org.apache.log4j.ConsoleAppender; | ||
| 22 | import org.apache.log4j.Level; | ||
| 23 | import org.apache.log4j.PatternLayout; | ||
| 24 | import org.apache.log4j.RollingFileAppender; | ||
| 25 | import org.emercit.proxymanager.service.ProxyLinux; | ||
| 26 | |||
| 27 | |||
| 28 | public class App | ||
| 29 | { | ||
| 30 | |||
| 31 | private static final Logger log = Logger.getLogger(App.class); | ||
| 32 | |||
| 33 | private static ProxyLinux proxyLinux=new ProxyLinux(); | ||
| 34 | |||
| 35 | private static String eth; //Название интерфейса | ||
| 36 | private static String param; //Параметр который нужно изменить | ||
| 37 | private static String method; // Метод | ||
| 38 | private static String value; // Значение параметра | ||
| 39 | private static String name; // Название интерфейса | ||
| 40 | |||
| 41 | public static void main( String[] args ) | ||
| 42 | { | ||
| 43 | ArgumentParser parser = ArgumentParsers.newArgumentParser("prog"); | ||
| 44 | |||
| 45 | try { | ||
| 46 | |||
| 47 | parser.description("Настройки прокси") | ||
| 48 | .version("Version:1.0.0.0") | ||
| 49 | .defaultHelp(true); | ||
| 50 | |||
| 51 | parser.addArgument("-m") | ||
| 52 | .type(String.class) | ||
| 53 | .setDefault("get") | ||
| 54 | .help("Методы:\n 1) get - получение параметра;\n" | ||
| 55 | + "2) set- установка параметра.\n"); | ||
| 56 | |||
| 57 | parser.addArgument("-v") | ||
| 58 | .type(String.class) | ||
| 59 | .setDefault("") | ||
| 60 | .help("Значение параметра"); | ||
| 61 | |||
| 62 | |||
| 63 | parser.addArgument("-p") | ||
| 64 | .type(String.class) | ||
| 65 | .setDefault("") | ||
| 66 | .help("Имя параметра\n" | ||
| 67 | + "Возможные параметры:\n " | ||
| 68 | + "1) host - адрес прокси сервера;\n" | ||
| 69 | + "2) port - порт прокси сервера;\n" | ||
| 70 | + "3) login - имя пользователя;\n" | ||
| 71 | + "4) pass -пароль;\n" | ||
| 72 | + "5) enabled - проксирование. Значение true -включено, false- выкл.\n" | ||
| 73 | + "6) protocol - навзание протакола (http,https,ftp);\n" | ||
| 74 | + "7) useauth - использовать логин и пароль. Значение true - использовать, false -нет.\n"); | ||
| 75 | |||
| 76 | |||
| 77 | parser.addArgument("-protocol") | ||
| 78 | .type(String.class) | ||
| 79 | .setDefault("") | ||
| 80 | .help("Протокол"); | ||
| 81 | |||
| 82 | parser.addArgument("-log") | ||
| 83 | .type(Integer.class) | ||
| 84 | .setDefault(0) | ||
| 85 | .help("Логирование. 0-выкл.; 1-консоль; 2-файл"); | ||
| 86 | |||
| 87 | |||
| 88 | Logger rootLogger = Logger.getRootLogger(); | ||
| 89 | PatternLayout layout = new PatternLayout("%d{ISO8601} [%t] %-5p %c %x - %m%n"); | ||
| 90 | |||
| 91 | //rootLogger.setLevel(Level.OFF); | ||
| 92 | rootLogger.setLevel(Level.DEBUG); | ||
| 93 | |||
| 94 | rootLogger.addAppender(new ConsoleAppender(layout)); | ||
| 95 | try { | ||
| 96 | RollingFileAppender fileAppender = new RollingFileAppender(layout, "/var/log/devtools/ProxyManager.log"); | ||
| 97 | rootLogger.addAppender(fileAppender); | ||
| 98 | } catch (IOException e) { | ||
| 99 | log.error(e.getMessage()); | ||
| 100 | } | ||
| 101 | |||
| 102 | Namespace res = parser.parseArgs(args); | ||
| 103 | |||
| 104 | method=res.getString("m"); | ||
| 105 | |||
| 106 | if (method.equals("set")) { //Конфигурация интерфейса | ||
| 107 | |||
| 108 | name=res.getString("protocol"); | ||
| 109 | param= res.getString("p"); | ||
| 110 | value=res.getString("v"); | ||
| 111 | |||
| 112 | proxyLinux.ApplyConfig(name, param,(Object)value ); | ||
| 113 | |||
| 114 | }else if (method.equals("get")) { //Вывод информации из файла | ||
| 115 | |||
| 116 | name=res.getString("protocol"); | ||
| 117 | proxyLinux.InfoByName(name); | ||
| 118 | } | ||
| 119 | |||
| 120 | }catch(Exception e){ | ||
| 121 | log.error(e.getMessage()); | ||
| 122 | if(e instanceof ArgumentParserException){ | ||
| 123 | log.error(e.getMessage()); | ||
| 124 | } | ||
| 125 | else { | ||
| 126 | parser.handleError((ArgumentParserException) e); | ||
| 127 | log.error(e.getMessage()); | ||
| 128 | System.exit(1); | ||
| 129 | } | ||
| 130 | |||
| 131 | } | ||
| 132 | |||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | |||
| 137 | |||
| 138 | |||
| 139 | |||
| 140 | |||
| 141 | |||
| 142 | |||
| 143 | |||
| 144 | |||
| 145 |
| 1 | package org.emercit.proxymanager.model; | ||
| 2 | |||
| 3 | |||
| 4 | import java.io.Serializable; | ||
| 5 | |||
| 6 | public class ProxyBean implements Serializable { | ||
| 7 | |||
| 8 | |||
| 9 | |||
| 10 | private String protocol; | ||
| 11 | |||
| 12 | private String host; | ||
| 13 | |||
| 14 | private int port; | ||
| 15 | |||
| 16 | private String login; | ||
| 17 | |||
| 18 | private String pass; | ||
| 19 | |||
| 20 | private boolean enabled; | ||
| 21 | |||
| 22 | private boolean useauth; | ||
| 23 | |||
| 24 | public ProxyBean() { | ||
| 25 | |||
| 26 | |||
| 27 | } | ||
| 28 | |||
| 29 | /* | ||
| 30 | * SET | ||
| 31 | */ | ||
| 32 | |||
| 33 | |||
| 34 | |||
| 35 | public void setProtocol(String value) { | ||
| 36 | this.protocol=value; | ||
| 37 | } | ||
| 38 | |||
| 39 | public void setHost(String value) { | ||
| 40 | this.host=value; | ||
| 41 | } | ||
| 42 | |||
| 43 | public void setPort(int value) { | ||
| 44 | this.port=value; | ||
| 45 | } | ||
| 46 | |||
| 47 | public void setLogin(String value) { | ||
| 48 | this.login=value; | ||
| 49 | } | ||
| 50 | |||
| 51 | public void setPass(String value) { | ||
| 52 | this.pass=value; | ||
| 53 | } | ||
| 54 | |||
| 55 | public void setEnabled(boolean value) { | ||
| 56 | this.enabled=value; | ||
| 57 | } | ||
| 58 | |||
| 59 | public void setUseAuth(boolean value) { | ||
| 60 | this.useauth=value; | ||
| 61 | } | ||
| 62 | |||
| 63 | |||
| 64 | /* | ||
| 65 | * GET | ||
| 66 | */ | ||
| 67 | |||
| 68 | public String getProtocol() { | ||
| 69 | return this.protocol; | ||
| 70 | } | ||
| 71 | |||
| 72 | public String getHost() { | ||
| 73 | return this.host; | ||
| 74 | } | ||
| 75 | |||
| 76 | public int getPort() { | ||
| 77 | return this.port; | ||
| 78 | } | ||
| 79 | |||
| 80 | public String getLogin() { | ||
| 81 | return this.login; | ||
| 82 | } | ||
| 83 | |||
| 84 | public String getPass() { | ||
| 85 | return this.pass; | ||
| 86 | } | ||
| 87 | |||
| 88 | |||
| 89 | public boolean getEnabled() { | ||
| 90 | return this.enabled; | ||
| 91 | } | ||
| 92 | |||
| 93 | public boolean getUseAuth() { | ||
| 94 | return this.useauth; | ||
| 95 | } | ||
| 96 | |||
| 97 | |||
| 98 | |||
| 99 | |||
| 100 | |||
| 101 | |||
| 102 | } |
| 1 | package org.emercit.proxymanager.service; | ||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 | import java.util.Vector; | ||
| 6 | import java.beans.XMLEncoder; | ||
| 7 | import java.beans.XMLDecoder; | ||
| 8 | import java.io.BufferedOutputStream; | ||
| 9 | import java.io.BufferedInputStream; | ||
| 10 | import java.io.FileOutputStream; | ||
| 11 | import java.io.FileInputStream; | ||
| 12 | |||
| 13 | import org.apache.log4j.Logger; | ||
| 14 | import org.emercit.proxymanager.App; | ||
| 15 | import org.emercit.proxymanager.model.ProxyBean; | ||
| 16 | |||
| 17 | public class ProxyDBImpl implements IProxyDB { | ||
| 18 | |||
| 19 | private static final Logger log = Logger.getLogger(ProxyDBImpl.class); | ||
| 20 | |||
| 21 | public boolean updateDb(Vector<ProxyBean> m) { | ||
| 22 | |||
| 23 | boolean result=true; | ||
| 24 | try { | ||
| 25 | FileOutputStream fos = new FileOutputStream("/opt/devtools/beans/ProxyBeans.xml"); | ||
| 26 | BufferedOutputStream bos = new BufferedOutputStream(fos); | ||
| 27 | XMLEncoder xmlEncoder = new XMLEncoder(bos); | ||
| 28 | xmlEncoder.writeObject(m); | ||
| 29 | xmlEncoder.close(); | ||
| 30 | }catch(Exception e) { | ||
| 31 | log.error(e.getMessage()); | ||
| 32 | result=false; | ||
| 33 | } | ||
| 34 | return result; | ||
| 35 | } | ||
| 36 | |||
| 37 | public Vector<ProxyBean> selectDb() { | ||
| 38 | |||
| 39 | Vector<ProxyBean> result=new Vector<ProxyBean>(); | ||
| 40 | |||
| 41 | try { | ||
| 42 | XMLDecoder d = new XMLDecoder( | ||
| 43 | new BufferedInputStream( | ||
| 44 | new FileInputStream("/opt/devtools/beans/ProxyBeans.xml"))); | ||
| 45 | result=(Vector<ProxyBean>)d.readObject(); | ||
| 46 | d.close(); | ||
| 47 | }catch(Exception e) { | ||
| 48 | log.error(e.getMessage()); | ||
| 49 | result=null; | ||
| 50 | } | ||
| 51 | return result; | ||
| 52 | } | ||
| 53 | |||
| 54 | } | ||
| 55 | |||
| 56 | |||
| 57 | |||
| 58 | |||
| 59 | |||
| 60 | |||
| 61 | |||
| 62 | |||
| 63 |
| 1 | package org.emercit.proxymanager.service; | ||
| 2 | |||
| 3 | import java.io.BufferedWriter; | ||
| 4 | import java.io.File; | ||
| 5 | import java.io.FileWriter; | ||
| 6 | import java.io.IOException; | ||
| 7 | import java.util.Vector; | ||
| 8 | |||
| 9 | import org.apache.log4j.Logger; | ||
| 10 | import org.emercit.proxymanager.model.ProxyBean; | ||
| 11 | import org.emercit.proxymanager.service.ProxyDBImpl; | ||
| 12 | |||
| 13 | import com.cedarsoftware.util.io.JsonReader; | ||
| 14 | import com.cedarsoftware.util.io.JsonWriter; | ||
| 15 | |||
| 16 | public class ProxyLinux { | ||
| 17 | |||
| 18 | private static final Logger log = Logger.getLogger(ProxyLinux.class); | ||
| 19 | |||
| 20 | private ProxyDBImpl proxyStore=new ProxyDBImpl(); | ||
| 21 | |||
| 22 | private Vector<ProxyBean> pbs; | ||
| 23 | |||
| 24 | private boolean found; | ||
| 25 | |||
| 26 | private String json; | ||
| 27 | |||
| 28 | private Object obj; | ||
| 29 | |||
| 30 | private ProxyBean pmp; | ||
| 31 | |||
| 32 | public void ApplyConfig (String protocol, String p, Object v) { | ||
| 33 | |||
| 34 | |||
| 35 | pbs=proxyStore.selectDb(); | ||
| 36 | |||
| 37 | found=false; | ||
| 38 | |||
| 39 | if (p.equals("object")) { | ||
| 40 | |||
| 41 | for(ProxyBean pb : pbs) { | ||
| 42 | if(pb.getProtocol().equals(protocol)) { | ||
| 43 | |||
| 44 | found=true; | ||
| 45 | |||
| 46 | switch(p) { | ||
| 47 | |||
| 48 | case "host":pb.setHost((String)v); break; | ||
| 49 | case "port":pb.setPort(Integer.valueOf((String)v)); break; | ||
| 50 | case "login":pb.setLogin((String)v); break; | ||
| 51 | case "pass":pb.setPass((String)v); break; | ||
| 52 | case "enabled":pb.setEnabled(Boolean.valueOf((String)v)); break; | ||
| 53 | case "useauth":pb.setUseAuth(Boolean.valueOf((String)v)); break; | ||
| 54 | |||
| 55 | } | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 60 | if (!found) { // Добавляем в файл ProxyBeans | ||
| 61 | if (checkProrocol(protocol)) { | ||
| 62 | |||
| 63 | ProxyBean pb=new ProxyBean(); | ||
| 64 | pb.setProtocol(protocol); | ||
| 65 | pb.setHost(""); | ||
| 66 | pb.setPort(0); | ||
| 67 | pb.setLogin(""); | ||
| 68 | pb.setPass(""); | ||
| 69 | pb.setEnabled(false); | ||
| 70 | pb.setUseAuth(false); | ||
| 71 | |||
| 72 | pbs.add(pb); | ||
| 73 | }else { | ||
| 74 | log.error(protocol+" - неизвестный тип протокола"); | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | } else { | ||
| 79 | |||
| 80 | |||
| 81 | obj = JsonReader.jsonToJava((String)json); | ||
| 82 | pmp = (ProxyBean) obj; | ||
| 83 | |||
| 84 | for(ProxyBean pb : pbs) { | ||
| 85 | if(pb.getProtocol().equals(pmp.getProtocol())) { | ||
| 86 | |||
| 87 | pb.setHost(pmp.getHost()); | ||
| 88 | pb.setPort(pmp.getPort()); | ||
| 89 | pb.setLogin(pmp.getLogin()); | ||
| 90 | pb.setPass(pmp.getPass()); | ||
| 91 | pb.setEnabled(pmp.getEnabled()); | ||
| 92 | pb.setUseAuth(pmp.getUseAuth()); | ||
| 93 | |||
| 94 | } | ||
| 95 | break; | ||
| 96 | } | ||
| 97 | |||
| 98 | } | ||
| 99 | |||
| 100 | Config(pbs); | ||
| 101 | |||
| 102 | |||
| 103 | } | ||
| 104 | |||
| 105 | private boolean checkProrocol(String value) { | ||
| 106 | |||
| 107 | if (value.equals("http")|| value.equals("ftp") || value.equals("https")) { | ||
| 108 | return true; | ||
| 109 | } | ||
| 110 | |||
| 111 | return false; | ||
| 112 | } | ||
| 113 | |||
| 114 | public void InfoByName(String protocol) { | ||
| 115 | |||
| 116 | pbs=proxyStore.selectDb(); | ||
| 117 | |||
| 118 | if (pbs!=null) { | ||
| 119 | |||
| 120 | for(ProxyBean pb : pbs) { | ||
| 121 | if(pb.getProtocol().equals(protocol)) { | ||
| 122 | json = JsonWriter.objectToJson(pb); | ||
| 123 | System.out.println(json); | ||
| 124 | } | ||
| 125 | } | ||
| 126 | } | ||
| 127 | } | ||
| 128 | private void Config(Vector<ProxyBean> pbs) { | ||
| 129 | try { | ||
| 130 | File file = new File("/etc/environment"); | ||
| 131 | |||
| 132 | if (!file.exists()) { | ||
| 133 | file.createNewFile(); | ||
| 134 | } | ||
| 135 | |||
| 136 | FileWriter fw = new FileWriter(file.getAbsoluteFile()); | ||
| 137 | BufferedWriter bw = new BufferedWriter(fw); | ||
| 138 | |||
| 139 | bw.write("#Generated by ProxyManager \n"); | ||
| 140 | bw.write("\n"); | ||
| 141 | |||
| 142 | for (ProxyBean pb:pbs) { | ||
| 143 | |||
| 144 | if (pb.getEnabled()) { | ||
| 145 | if (pb.getUseAuth()) { | ||
| 146 | bw.write(pb.getProtocol()+"_proxy"+"='"+"http://"+pb.getLogin()+":"+pb.getPass()+"@"+pb.getHost()+":"+pb.getPort()+"'\n"); | ||
| 147 | }else { | ||
| 148 | bw.write(pb.getProtocol()+"_proxy"+"='"+"http://"+pb.getHost()+":"+pb.getPort()+"'\n"); | ||
| 149 | } | ||
| 150 | } | ||
| 151 | } | ||
| 152 | bw.write("\n"); | ||
| 153 | bw.close(); | ||
| 154 | proxyStore.updateDb(pbs); | ||
| 155 | |||
| 156 | } catch (IOException e) { | ||
| 157 | log.error(e.getMessage()); | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | |||
| 162 | |||
| 163 | |||
| 164 | |||
| 165 | } | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| 1 | package org.emercit.proxymanager; | ||
| 2 | |||
| 3 | import junit.framework.Test; | ||
| 4 | import junit.framework.TestCase; | ||
| 5 | import junit.framework.TestSuite; | ||
| 6 | |||
| 7 | /** | ||
| 8 | * Unit test for simple App. | ||
| 9 | */ | ||
| 10 | public class AppTest | ||
| 11 | extends TestCase | ||
| 12 | { | ||
| 13 | /** | ||
| 14 | * Create the test case | ||
| 15 | * | ||
| 16 | * @param testName name of the test case | ||
| 17 | */ | ||
| 18 | public AppTest( String testName ) | ||
| 19 | { | ||
| 20 | super( testName ); | ||
| 21 | } | ||
| 22 | |||
| 23 | /** | ||
| 24 | * @return the suite of tests being tested | ||
| 25 | */ | ||
| 26 | public static Test suite() | ||
| 27 | { | ||
| 28 | return new TestSuite( AppTest.class ); | ||
| 29 | } | ||
| 30 | |||
| 31 | /** | ||
| 32 | * Rigourous Test :-) | ||
| 33 | */ | ||
| 34 | public void testApp() | ||
| 35 | { | ||
| 36 | assertTrue( true ); | ||
| 37 | } | ||
| 38 | } |
SSHCmdExecutor/.classpath
0 → 100644
| 1 | <?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | <classpath> | ||
| 3 | <classpathentry kind="src" output="target/classes" path="src/main/java"> | ||
| 4 | <attributes> | ||
| 5 | <attribute name="optional" value="true"/> | ||
| 6 | <attribute name="maven.pomderived" value="true"/> | ||
| 7 | </attributes> | ||
| 8 | </classpathentry> | ||
| 9 | <classpathentry kind="src" output="target/test-classes" path="src/test/java"> | ||
| 10 | <attributes> | ||
| 11 | <attribute name="optional" value="true"/> | ||
| 12 | <attribute name="maven.pomderived" value="true"/> | ||
| 13 | </attributes> | ||
| 14 | </classpathentry> | ||
| 15 | <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"> | ||
| 16 | <attributes> | ||
| 17 | <attribute name="maven.pomderived" value="true"/> | ||
| 18 | </attributes> | ||
| 19 | </classpathentry> | ||
| 20 | <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> | ||
| 21 | <attributes> | ||
| 22 | <attribute name="maven.pomderived" value="true"/> | ||
| 23 | </attributes> | ||
| 24 | </classpathentry> | ||
| 25 | <classpathentry kind="output" path="target/classes"/> | ||
| 26 | </classpath> |
SSHCmdExecutor/.gitignore
0 → 100644
| 1 | /target |
SSHCmdExecutor/.project
0 → 100644
| 1 | <?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | <projectDescription> | ||
| 3 | <name>SSHCmdExecutor</name> | ||
| 4 | <comment></comment> | ||
| 5 | <projects> | ||
| 6 | </projects> | ||
| 7 | <buildSpec> | ||
| 8 | <buildCommand> | ||
| 9 | <name>org.eclipse.jdt.core.javabuilder</name> | ||
| 10 | <arguments> | ||
| 11 | </arguments> | ||
| 12 | </buildCommand> | ||
| 13 | <buildCommand> | ||
| 14 | <name>org.eclipse.m2e.core.maven2Builder</name> | ||
| 15 | <arguments> | ||
| 16 | </arguments> | ||
| 17 | </buildCommand> | ||
| 18 | </buildSpec> | ||
| 19 | <natures> | ||
| 20 | <nature>org.eclipse.jdt.core.javanature</nature> | ||
| 21 | <nature>org.eclipse.m2e.core.maven2Nature</nature> | ||
| 22 | </natures> | ||
| 23 | </projectDescription> |
SSHCmdExecutor/pom.xml
0 → 100644
| 1 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| 2 | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| 3 | <modelVersion>4.0.0</modelVersion> | ||
| 4 | |||
| 5 | <groupId>NetConfig</groupId> | ||
| 6 | <artifactId>SSHClient</artifactId> | ||
| 7 | <version>0.0.1-SNAPSHOT</version> | ||
| 8 | <packaging>jar</packaging> | ||
| 9 | |||
| 10 | <name>SSHClient</name> | ||
| 11 | <url>http://maven.apache.org</url> | ||
| 12 | |||
| 13 | <properties> | ||
| 14 | <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| 15 | </properties> | ||
| 16 | |||
| 17 | <dependencies> | ||
| 18 | <dependency> | ||
| 19 | <groupId>junit</groupId> | ||
| 20 | <artifactId>junit</artifactId> | ||
| 21 | <version>3.8.1</version> | ||
| 22 | <scope>test</scope> | ||
| 23 | </dependency> | ||
| 24 | |||
| 25 | <dependency> | ||
| 26 | <groupId>com.jcraft</groupId> | ||
| 27 | <artifactId>jsch</artifactId> | ||
| 28 | <version>0.1.51</version> | ||
| 29 | </dependency> | ||
| 30 | |||
| 31 | |||
| 32 | |||
| 33 | <dependency> | ||
| 34 | <groupId>log4j</groupId> | ||
| 35 | <artifactId>log4j</artifactId> | ||
| 36 | <version>1.2.17</version> | ||
| 37 | </dependency> | ||
| 38 | |||
| 39 | |||
| 40 | <dependency> | ||
| 41 | <groupId>net.sourceforge.argparse4j</groupId> | ||
| 42 | <artifactId>argparse4j</artifactId> | ||
| 43 | <version>0.6.0</version> | ||
| 44 | </dependency> | ||
| 45 | |||
| 46 | |||
| 47 | </dependencies> | ||
| 48 | </project> |
| 1 | package org.emercit.sshcmdexecutor; | ||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 | import java.io.IOException; | ||
| 6 | import java.nio.ByteBuffer; | ||
| 7 | import java.nio.channels.ByteChannel; | ||
| 8 | import java.nio.file.Files; | ||
| 9 | import java.nio.file.Path; | ||
| 10 | import java.nio.file.Paths; | ||
| 11 | import java.nio.file.StandardOpenOption; | ||
| 12 | import java.security.MessageDigest; | ||
| 13 | import java.security.NoSuchAlgorithmException; | ||
| 14 | |||
| 15 | import net.sourceforge.argparse4j.ArgumentParsers; | ||
| 16 | import net.sourceforge.argparse4j.impl.Arguments; | ||
| 17 | import net.sourceforge.argparse4j.inf.ArgumentParser; | ||
| 18 | import net.sourceforge.argparse4j.inf.ArgumentParserException; | ||
| 19 | import net.sourceforge.argparse4j.inf.Namespace; | ||
| 20 | |||
| 21 | import org.apache.log4j.Logger; | ||
| 22 | import org.apache.log4j.ConsoleAppender; | ||
| 23 | import org.apache.log4j.Level; | ||
| 24 | import org.apache.log4j.PatternLayout; | ||
| 25 | import org.apache.log4j.RollingFileAppender; | ||
| 26 | |||
| 27 | |||
| 28 | public class App | ||
| 29 | { | ||
| 30 | private static final Logger log = Logger.getLogger(App.class); | ||
| 31 | |||
| 32 | private static SSHCmdExecutor executor=new SSHCmdExecutor(); | ||
| 33 | |||
| 34 | public static void main( String[] args ) | ||
| 35 | { | ||
| 36 | ArgumentParser parser = ArgumentParsers.newArgumentParser("prog"); | ||
| 37 | |||
| 38 | try { | ||
| 39 | |||
| 40 | parser.description("Выполнение команд через подключении по SSH") | ||
| 41 | .version("Version:1.0.0.0") | ||
| 42 | .defaultHelp(true); | ||
| 43 | |||
| 44 | parser.addArgument("-login") | ||
| 45 | .type(String.class) | ||
| 46 | .help("Имя пользователя"); | ||
| 47 | |||
| 48 | parser.addArgument("-pass") | ||
| 49 | .type(String.class) | ||
| 50 | .help("Пароль"); | ||
| 51 | |||
| 52 | parser.addArgument("-host") | ||
| 53 | .type(String.class) | ||
| 54 | .help("Адрес"); | ||
| 55 | |||
| 56 | parser.addArgument("-port") | ||
| 57 | .type(Integer.class) | ||
| 58 | .help("Порт"); | ||
| 59 | |||
| 60 | parser.addArgument("-cmd") | ||
| 61 | .type(String.class) | ||
| 62 | .help("Команда для выполнения"); | ||
| 63 | |||
| 64 | parser.addArgument("-log") | ||
| 65 | .type(Integer.class) | ||
| 66 | .setDefault(0) | ||
| 67 | .help("Логирование. 0-выкл.; 1-консоль; 2-файл"); | ||
| 68 | |||
| 69 | Logger rootLogger = Logger.getRootLogger(); | ||
| 70 | PatternLayout layout = new PatternLayout("%d{ISO8601} [%t] %-5p %c %x - %m%n"); | ||
| 71 | |||
| 72 | //rootLogger.setLevel(Level.OFF); | ||
| 73 | rootLogger.setLevel(Level.DEBUG); | ||
| 74 | |||
| 75 | rootLogger.addAppender(new ConsoleAppender(layout)); | ||
| 76 | try { | ||
| 77 | RollingFileAppender fileAppender = new RollingFileAppender(layout, "/var/log/devtools/SSHCmdExcecutor.log"); | ||
| 78 | rootLogger.addAppender(fileAppender); | ||
| 79 | } catch (IOException e) { | ||
| 80 | log.error(e.getMessage()); | ||
| 81 | } | ||
| 82 | |||
| 83 | Namespace res = parser.parseArgs(args); | ||
| 84 | |||
| 85 | executor.setLogin(res.getString("login")); | ||
| 86 | executor.setPassword(res.getString("pass")); | ||
| 87 | executor.setHost(res.getString("host")); | ||
| 88 | executor.setPort(res.getInt("port")); | ||
| 89 | executor.setCmd(res.getString("cmd")); | ||
| 90 | |||
| 91 | executor.Exec(); | ||
| 92 | |||
| 93 | }catch(Exception e){ | ||
| 94 | log.error(e.getMessage()); | ||
| 95 | if(e instanceof ArgumentParserException){ | ||
| 96 | log.error(e.getMessage()); | ||
| 97 | } else { | ||
| 98 | parser.handleError((ArgumentParserException) e); | ||
| 99 | log.error(e.getMessage()); | ||
| 100 | System.exit(1); | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | |||
| 108 | |||
| 109 | |||
| 110 | |||
| 111 | |||
| 112 | |||
| 113 | |||
| 114 | |||
| 115 | |||
| 116 | |||
| 117 | |||
| 118 | |||
| 119 | |||
| 120 | |||
| 121 | |||
| 122 |
| 1 | package org.emercit.sshcmdexecutor; | ||
| 2 | |||
| 3 | |||
| 4 | import java.util.Properties; | ||
| 5 | |||
| 6 | import com.jcraft.jsch.Channel; | ||
| 7 | import com.jcraft.jsch.ChannelExec; | ||
| 8 | import com.jcraft.jsch.JSch; | ||
| 9 | import com.jcraft.jsch.Session; | ||
| 10 | |||
| 11 | import java.io.InputStream; | ||
| 12 | import java.io.OutputStream; | ||
| 13 | |||
| 14 | import org.apache.log4j.Logger; | ||
| 15 | |||
| 16 | |||
| 17 | public class SSHCmdExecutor { | ||
| 18 | |||
| 19 | private static final Logger log = Logger.getLogger(SSHCmdExecutor.class); | ||
| 20 | |||
| 21 | Properties config = new Properties(); | ||
| 22 | |||
| 23 | private String login; | ||
| 24 | |||
| 25 | private String password; | ||
| 26 | |||
| 27 | private String host; | ||
| 28 | |||
| 29 | private int port; | ||
| 30 | |||
| 31 | private String cmd; | ||
| 32 | |||
| 33 | public SSHCmdExecutor() { | ||
| 34 | config.put("StrictHostKeyChecking", "no"); | ||
| 35 | } | ||
| 36 | |||
| 37 | public void setLogin(String value) { | ||
| 38 | this.login=value; | ||
| 39 | } | ||
| 40 | |||
| 41 | public void setPassword(String value) { | ||
| 42 | this.password=value; | ||
| 43 | } | ||
| 44 | |||
| 45 | public void setHost(String value) { | ||
| 46 | this.host=value; | ||
| 47 | } | ||
| 48 | |||
| 49 | public void setPort(int value){ | ||
| 50 | this.port=value; | ||
| 51 | } | ||
| 52 | |||
| 53 | public void setCmd(String value) { | ||
| 54 | this.cmd=value; | ||
| 55 | } | ||
| 56 | |||
| 57 | public void Exec() throws Exception { | ||
| 58 | |||
| 59 | JSch jsch = new JSch(); | ||
| 60 | Session session=jsch.getSession(this.login, this.host, this.port); | ||
| 61 | session.setPassword(password); | ||
| 62 | session.setConfig(config); | ||
| 63 | session.connect(); | ||
| 64 | |||
| 65 | Channel channel=session.openChannel("exec"); | ||
| 66 | |||
| 67 | ((ChannelExec)channel).setCommand("sudo -S -p '' "+cmd); | ||
| 68 | |||
| 69 | InputStream in=channel.getInputStream(); | ||
| 70 | OutputStream out=channel.getOutputStream(); | ||
| 71 | ((ChannelExec)channel).setErrStream(System.err); | ||
| 72 | |||
| 73 | channel.connect(); | ||
| 74 | |||
| 75 | out.write((password+"\n").getBytes()); | ||
| 76 | out.flush(); | ||
| 77 | |||
| 78 | byte[] tmp=new byte[1024]; | ||
| 79 | while(true){ | ||
| 80 | |||
| 81 | while(in.available()>0){ | ||
| 82 | int i=in.read(tmp, 0, 1024); | ||
| 83 | if(i<0)break; | ||
| 84 | System.out.print(new String(tmp, 0, i)); | ||
| 85 | } | ||
| 86 | if(channel.isClosed()){ | ||
| 87 | break; | ||
| 88 | } | ||
| 89 | try{Thread.sleep(1000);}catch(Exception ee){} | ||
| 90 | } | ||
| 91 | channel.disconnect(); | ||
| 92 | session.disconnect(); | ||
| 93 | |||
| 94 | } | ||
| 95 | } |
| 1 | package NetConfig.SSHClient; | ||
| 2 | |||
| 3 | import junit.framework.Test; | ||
| 4 | import junit.framework.TestCase; | ||
| 5 | import junit.framework.TestSuite; | ||
| 6 | |||
| 7 | /** | ||
| 8 | * Unit test for simple App. | ||
| 9 | */ | ||
| 10 | public class AppTest | ||
| 11 | extends TestCase | ||
| 12 | { | ||
| 13 | /** | ||
| 14 | * Create the test case | ||
| 15 | * | ||
| 16 | * @param testName name of the test case | ||
| 17 | */ | ||
| 18 | public AppTest( String testName ) | ||
| 19 | { | ||
| 20 | super( testName ); | ||
| 21 | } | ||
| 22 | |||
| 23 | /** | ||
| 24 | * @return the suite of tests being tested | ||
| 25 | */ | ||
| 26 | public static Test suite() | ||
| 27 | { | ||
| 28 | return new TestSuite( AppTest.class ); | ||
| 29 | } | ||
| 30 | |||
| 31 | /** | ||
| 32 | * Rigourous Test :-) | ||
| 33 | */ | ||
| 34 | public void testApp() | ||
| 35 | { | ||
| 36 | assertTrue( true ); | ||
| 37 | } | ||
| 38 | } |
-
Please register or sign in to post a comment