Linux

Using the command line with java

Posted in Development, Java, Linux, Resourcen on September 20th, 2010 by admin – Be the first to comment

If you are looking for a simple way to use the command line via Java use this wrapper class. It offers a simple solution for a simple task. KISS!

How to use it:

System.out.println(new CommandLine("/var").exec("ls"));

And here is the class:

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
 
public class CommandLine {
 
	private String path ;
 
	public CommandLine() {
		path = "/";
	}
 
	public CommandLine(String path) {
		this.path = path;
	}
 
	public String getPath() {
		return path;
	}
 
	public void setPath(String path) {
		this.path = path;
	}
 
	public String exec(String cmd) {
		String s = null;
		String ret = "";
		File workDir = new File(path);
		try {
			Process p = Runtime.getRuntime().exec(cmd, null, workDir);
			int i = p.waitFor();
			if (i == 0){
				BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
				while ((s = stdInput.readLine()) != null) {
					ret = ret + s + "\n";
				}
			}
			else {
				BufferedReader stdErr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
				while ((s = stdErr.readLine()) != null) {
					ret = ret + s + "\n";
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}
 
}

Wikipedia teilweise dowloaden

Posted in Linux on September 17th, 2010 by admin – Be the first to comment

Ich schreibe gerade meine Projektarbeit über Softwareentwicklung. Teile davon versuche ich während meiner täglichen Zugfahren zu schreiben, jedoch ist es relativ schwierig wenn man dort weder Bücher noch Wikipedia zur Verfügung hat.

Wikipedia komplett herunterzuladen fand ich ein wenig übertrieben, schließlich sind es mehr als 2Gb, die ich zu 99,9% nicht brauchen werde. Daher hab ich mich nach einer Alternative umgesehen und eine Option von wget entdeckt:

wget -m -p -e robots=off –wait=1 http://de.wikipedia.org/wiki/Softwaretechnik

Jetzt heißt es warten – solange bis es scheint, dass alle relevanten Artikel auf der Platte gelandet sind. Dann einfach wget mit Ctrl+C abbrechen. Zwar funktionieren nun die Links nicht wirklich (man muss ein wenig tricksen und das “file:///” wegnehmen), aber man hat alle relevanten Artikel in einem Ordner, inklusive aller Bilder und Grafiken.

Paranoia für Linux-Admins

Posted in Development, Linux, Sicherheit, Webdevelopment on September 13th, 2010 by admin – Be the first to comment

Heute: Welche Programme lauschen an welchen Ports?

netstat -anp|grep LISTEN

netstat -anp|grep LISTEN