Copying files between linux hosts

There are many ways to copy the file from one linux box to the other one.

netsend

First on the target machine (host: 192.168.12.34, place where the file should be copied to)

netsend -T machine tcp receive > filename.txt

and on the source machine (where the file is located)

netsend -T machine tcp transmit filename.txt 192.168.12.34

scp

scp remoteUser@remoteHost:/home/remoteUser/file.txt ~/file.txt

How to set git branch in prompt

add this code to ~/.bashrc file

# git branch in prompt

function parse_git_branch {
git branch –no-color 2> /dev/null | sed -e ‘/^[^*]/d’ -e ‘s/* \(.*\)/\1/’
}

function parse_git_dirty {
git diff –quiet || echo “⚡”
}

function parse_git {
local branch=$(parse_git_branch)
if [[ -n “$branch” ]]; then
echo ” <$branch$(parse_git_dirty)>”
fi
}

function prompt {
local LIGHT_RED=”\[33[1;31m\]”
local NORMAL=”\[33[00m\]”
local BOLD=”\[33[1m\]”
local OFF=”\[33[m\]”

PS1=’${debian_chroot:+($debian_chroot)}\[33[01;32m\]\u@\h\[33[00m\]:\[33[01;34m\]\w\[33[00m\]\[33[1;31m\]$(parse_git)\[33[m\]\$ ‘
PS2=”${BOLD}>>${OFF} ”
}

prompt

get IP address with maven and groovy

<project xmlns=”http://maven.apache.org/POM/4.0.0&#8243; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”&gt;
<modelVersion>4.0.0</modelVersion>
<groupId>example.maven.groovy</groupId>
<artifactId>groovy-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>groovy-test</name>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>get-local-ip</id>
<phase>initialize</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<classpath>
<element>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.4</version>
</element>
</classpath>
<source>
def addr = InetAddress.getLocalHost();
java.net.InetAddress[] addresses=InetAddress.getAllByName(addr.getHostName());
for (address in addresses) {
if (!(address.getHostAddress().startsWith(“0”))) {
println “Your IP address is: ”
println address;
project.properties.localIP=address.getHostAddress();
}
}
</source>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

the output should contain something like:

Your IP address is:
my-computer-name12345/123.123.123.123

 

Parsing command line arguments in java with Apache Commons CLI

Simple way to parse command line arguments in java using Apache Commons CLI

first define Options for application

final Options options = new Options();
options.addOption(“d”, “database”, true, “jdbc URL to database, REQUIRED”);
options.addOption(“u”, “dbuser”, true, “database user name, REQUIRED”);
options.addOption(“p”, “dbpass”, true, “database password, REQUIRED”);
options.addOption(“v”, “verbose”, false, “Enable verbose output, OPTIONAL, default is disabled”);

next parse arguments and check it with options

final org.apache.commons.cli.CommandLineParser parser = new PosixParser();
cmd = parser.parse(options, args);

use boolean verbose = cmd.hasOption(“v”)  to check if parameter was passed in command line

use String user = cmd.getOptionValue(“u”); to get the parameter value

automatically generate the help statement

final HelpFormatter formatter = new HelpFormatter();
final StringBuilder usageSb = new StringBuilder();
usageSb.append(“java -jar my-application.jar [-v] -d <jdbc url> -u <db user> -p <db password>”);
final StringBuilder header = new StringBuilder();
header.append(“\nmy-application\n”);
header.append(“Description of the application:\n”);
header.append(“more info on the webpage http://my.application.com.example&#8221;);
formatter.printHelp(usageSb.toString(), header.toString(), options,
“\nFor more information please check webpage http://my.application.com.example&#8221;);

Reading bytes from InputStream

final InputStream is = httpResponse.getEntity().getContent();
final Writer writer = new StringWriter();
final char[] buffer = new char[1024];
try {
final Reader reader = new BufferedReader(new InputStreamReader(is, “UTF-8”));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
is.close();
}
System.out.println(“OUTPUT : ” + writer.toString());

or

StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer, encoding);
String theString = writer.toString();