Friday, December 13, 2013

Sending UDP from cmdline

Useful if you are testing something

echo "HELLO" | socat - UDP4-DATAGRAM:192.168.1.0:19838,sp=19835,broadcast,range=192.168.1.5/32



watch echo "HELLOA `date`" | socat - UDP4-DATAGRAM:192.168.1.0:19838,sp=19835,broadcast,range=192.168.1.5/32

Thursday, December 12, 2013

Automating tasks over telnet.

Ok. So it is a Stone Age and no one knows what SSH is. Everybody is using telnet.

Let's automate a service restarting task.

Cool and easy as 1,2,3.

{ \
 sleep 2; \
 echo "root"; \
 sleep 2; \
 echo "password"; \
 sleep 2; \
 echo "ps ax | grep my_cool_service"; \
 sleep 2; \
 echo "OLD_SERVICE_PID=\`ps ax | grep my_cool_service | awk '{print \$1}'\` && kill \$OLD_SERVICE_PID"; \
 sleep 2; \
 echo "service my_cool_service start && ps ax |grep my_cool_service"; \
 sleep 4; \
 } | telnet 10.10.10.10

  

Sunday, December 8, 2013

Gradle over proxy

Ok. Everyone knows that. This time it's a note for me-self.

vi ~/.gradle/gradle.properties
systemProp.http.proxyHost=www.proxyhost.org  systemProp.http.proxyPort=8080  systemProp.http.proxyUser=userid  systemProp.http.proxyPassword=password  systemProp.http.nonProxyHosts=*.nonproxyrepos.com|localhost
Have fun.

Tuesday, October 29, 2013

Building iOS apps for Simulator with Xcode 5 with command line

Things a little changed since Xcode 4

If you want to build your project for simulator 
and want to do it with the latest and the greatest version of Xcode - version 5.x
and want to do it with command line utility xcodebuild, 
because you need this for Continuous Integration or someth, 
then: 

1. In your Build Scheme for Simulator in project's "Build Settings" in section "Code Signing" set:
  • "Don't Code Sign" for "Code Signing Identity"
  • "None" for "Provisioning Profile"
2. In command line use iphonesimulator7.0 as desired SDK
(even if you have downloaded SDK 6.1, it won't help. not without other voodoo dark magic)

/usr/bin/xcodebuild -verbose \
  -project MyCoolProject/MyCoolProject.xcodeproj \
  -scheme MyCoolProjectSimulator \
  -configuration Debug \
  -arch i386 \
  -sdk iphonesimulator7.0 \
  TARGETED_DEVICE_FAMILY=2 \
  clean build

where TARGETED_DEVICE_FAMILY is iPhone (1) or iPad (2)


Thursday, October 17, 2013

SSH Port Forward multiple hops

Let's use VNC server two hops away.

ssh -4 -v -A -t -L 15902:localhost:15902 user1@host1 \
ssh -4 -v -A -L    15902:localhost:5900  user2@host2

vncviewer localhost:15902

Wednesday, October 16, 2013

Enable VNC Server on Mac OS X remotely

Thanks Apple, they use shell.

ssh to Mac OS X

sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -activate -configure -configure -clientopts -setvnclegacy -vnclegacy yes -setvncpw -vncpw YOUR_PASSWORD  -access -on -users admin -privs -all -restart -agent -menu

Src: don't rememmber

Decompiling Java classes on Linux with Krakatu just for fun


To execute:
python decompile.py -path libs/lib1.jar -path libs/lib2.jar -out decompiled.dir/ jar_to_decompile.jar

Parse JUnit XML reports and output them in HTML

Just add following to ant's task:

<junitreport todir="build/reports">
    <fileset dir="build/reports">
        <include name="TEST-*.xml"/>
    </fileset>
    <report format="frames" todir="build/reports/html"/>
</junitreport>


Auto build project with ant in Eclipse

Who needs Eclipse if there is a shell?

In Eclipse, go to Project's properties and add a new builder of type ant.

On tab Main provide:
  • Build file
  • Base Directory
  • Desired arguments that will be passed to ant ( -Dproperty=value style)

On tab Targets provide ant tasks for targets Auto Build, After a Clean.

Now you can just change sources and save the source file, 
Eclipse will execute "Auto Build" target right away.

Saturday, October 12, 2013

Install libimobiledevice on Mac OS X

So you'd like to manage your iOS devices by command line?
You can use libimobiledevice tools for that.
Easy on Linux. Let's see how it is on Mac OS X.

You'll need to:
1. install Xcode
2. start Xcode and agree to it's license
3. install Xcode command line tools (Start Xcode, Go to Preferences -> Downloads tab)
4. install homebrew by this command (sudo password will be prompted)

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

5. run following


brew install -s --HEAD libimobiledevice ideviceinstaller

6. ready to go


That's all, folks. Just a little more steps than in GNU/Linux.
So Mac OS X can really be of use by people.

Enable VNC Server on Mac remotely

Thanks, Apple! You've made life of many Unix admins easier by integrating some necessary stuff in OS X out of box.

To enable VNC Server on Mac when you have only SSH, fire this command.

sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -activate -configure -configure -clientopts -setvnclegacy -vnclegacy yes -setvncpw -vncpw YOUR_PASSWORD  -access -on -users admin -privs -all -restart -agent -menu 


Src - don't rememmber. If i'll find it, I'll add.

Disable hibernate in Windows 7 and delete hyberfil.sys

1. powercfg.exe -h off
2. rm hyberfil.sys

Uninstall MacPorts from Mac


"Uninstalling MacPorts can be a drastic step" so they say. That's why my choice is HomeBrew!

So here it is. Might be your's will be with a little deviation.

%% sudo port -fp uninstall installed

%% sudo rm -rf \
    /opt/local \
    /Applications/DarwinPorts \
    /Applications/MacPorts \
    /Library/LaunchDaemons/org.macports.* \
    /Library/Receipts/DarwinPorts*.pkg \
    /Library/Receipts/MacPorts*.pkg \
    /Library/StartupItems/DarwinPortsStartup \
    /Library/Tcl/darwinports1.0 \
    /Library/Tcl/macports1.0 \
    ~/.macports




Tuesday, October 1, 2013

How much memory can i insert into my Linux box

One could use dmidecode tool to get info about memory installed

$ dmidecode -t memory

# dmidecode 2.11
SMBIOS 2.7 present.

Handle 0x0007, DMI type 16, 23 bytes
Physical Memory Array
        Location: System Board Or Motherboard
        Use: System Memory
        Error Correction Type: None
        Maximum Capacity: 16 GB
        Error Information Handle: Not Provided
        Number Of Devices: 2

Handle 0x0008, DMI type 17, 34 bytes
Memory Device
        Array Handle: 0x0007
        Error Information Handle: Not Provided
        Total Width: 64 bits
        Data Width: 64 bits
        Size: 4096 MB
        Form Factor: SODIMM
        Set: None
        Locator: ChannelA-DIMM0
        Bank Locator: BANK 0
        Type: DDR3
        Type Detail: Synchronous
        Speed: 1333 MHz
        Manufacturer: Hynix/Hyundai
        Serial Number: 0654A654
        Asset Tag: None
        Part Number: HMT351S7C5R6C-PB  
        Rank: Unknown
        Configured Clock Speed: 1333 MHz

Handle 0x0009, DMI type 17, 34 bytes
Memory Device
        Array Handle: 0x0007
        Error Information Handle: Not Provided
        Total Width: 64 bits
        Data Width: 64 bits
        Size: 8192 MB
        Form Factor: SODIMM
        Set: None
        Locator: ChannelB-DIMM0
        Bank Locator: BANK 2
        Type: DDR3
        Type Detail: Synchronous
        Speed: 1333 MHz
        Manufacturer: 029E
        Serial Number: 00000000
        Asset Tag: None
        Part Number: CMSO8GX4M1A1666C9 
        Rank: Unknown
        Configured Clock Speed: 1333 MHz

Wednesday, September 25, 2013

Capturing Screen shots on Android via ADB

Let's use screencap tool on Android to capture a screen shot and save it to a file on Android's internal /sdcard.

adb shell screencap -p /sdcard/myScreenShot.png

Ok, lets capture a series of screen shots - a movie.

adb shell mkdir -p /sdcard/moovie1; 
for i in `seq -w 1 1 155`; do \
  adb shell screencap -p /sdcard/moovie1/cap_$i.png && \
  echo -e "cap_$i.png"; \
done;

Now let's download the images:

for i in `seq -w 1 1 155`; do \
  adb pull /sdcard/moovie1/cap_$i.png && \
  adb shell rm /sdcard/moovie1/cap_$i.png && \
  echo -e "cap_$i.png\n"; \
done;

Ok, now let's make a movie with help of well-known imagemagick tool::
Scale images to 50% size and convert them to GIF format.

for i in `ls`; \
  do convert $i -scale 50% $i.gif; \
done;

convert -delay 100 -loop 0 cap*.gif animated_screen.gif

Also one could use following script to save captured images directly to computer

 adb shell screencap -p | sed 's/\r$//' > screen.png 
or 
adb shell screencap -p | perl -pe 's/\x0D\x0A/\x0A/g' > screen.png

to cat images directly to a file on a computer (src)
(but for series of screen shots that will be a bit slower).


adb shell screencap -h
usage: screencap [-hp] [-d display-id] [FILENAME]
   -h: this message
   -p: save the file as a png.
   -d: specify the display id to capture, default 0.
If FILENAME ends with .png it will be saved as a png.
If FILENAME is not given, the results will be printed to stdout.


Tuesday, September 24, 2013

A strange way to populate SSH keys from Jenkins to Jenkins Slave node

Using sshpass tool as a workaround for ssh interactive password prompt. :-)


mkdir tmpz
cd tmpz
tar -zxvf sshpass.tgz

cd sshpass-1.05
./configure
make
./sshpass -p "hudson"  ssh-copy-id -i ~/.ssh/id_rsa.pub hudson@10.116.65.165

Tuesday, September 17, 2013

Use perl to edit multiple lines XML

Ok, here is a strange request - to edit some xml file over ssh on Mac OS X.

One needs to change false to true in following

<key>system.privilege.taskport</key> <dict> <key>allow-root</key> <false/>

Luckily, Mac OS X has perl (not sure if it was installed along with XCode). So let's use perl for that:

ssh user@192.168.1.2 "set -x; \
echo 'Password' | sudo -S \
perl -p -0777 -i -e 's@<key>system.privilege.taskport</key>.*?<dict>.*?<key>allow-root</key>.*?<false/>@<key>system.privilege.taskport</key>\n\t\t<dict>\n\t\t\t<key>allow-root</key>\n\t\t\t<true/>@sg' /etc/authorization"

Breaking this down:
  • -p tells it to loop through the input and print
  • -0777 tells it to use the end of file as the input separator, so that it gets the whole thing in in one slurp
  • -e means here comes the stuff I want you to do

And the substitution itself:
  • use @ as a delimiter so you don't have to escape /
  • use *?, the non-greedy version, to match as little as possible, so we don't go all the way to the last occurrence of </xyz> in the file
  • use the s modifier to let . match newlines (to get the multiple-line tag values)
  • use the g modifier to match the pattern multiple times
Perl expression was constructed with help of this answer from stackoverflow.

Friday, September 13, 2013

Insert a text at pattern using regexp in VIM

Say one is editing a text file in vim:


blah-blah234qqqqqqqqqq
blah-blah345234qqqqqqqqqq

and would like to insert a space at position after blah-blah345[digits] in each line.

One could use a following search-n-replace expression with help of regexp:

:%s/\(blah-blah\d\+\)/\1  /g

\( \) - for group selection
\1 - for first group selected
\d - for digit

Sunday, September 8, 2013

Find elements by XPath with JavaScript (example)


Get elements from action menu on mail page of yandex.ru:

r = document.evaluate("//*[@class='b-toolbar__block b-toolbar__block_chevron']/a", document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);

el = r.iterateNext();

while(el) { 
  if (window.getComputedStyle(el).display == "none") {
    continue; 
  } 
  console.log(el.getAttribute('data-action')); 
  el=r.iterateNext();
}





Friday, September 6, 2013

Using tar pipe for copying directory excluding files or directories.

A nice use case for tar pipe

Say we have a directory, with tons of subdirectories, each of which is a checked subversion project.
Dir structure looks like this.

RootDir / 
  /dir1/
    .svn
    dirs....
  /dir2/
    .svn
    dirs....

One wants to copy everything but .svn dirs.

Let's use tar pipe for that: 

tar --exclude \.svn -c RootDir | tar -C /path/to/destination/ -xv


Monday, September 2, 2013

Reveal Developer options on OS Android >= 4.2

On devices with OS Android >= 4.2  menu item "Developer options" appears to be hidden.

If you ever want to enable "Developer options" on your device, please do the following:

1. Go to Settings -> About tablet 
2. Perform about 7 taps on item Build number (the last menu item on the list)

Then "{} Developer options" will appear.


Jenkins + Gradle + Proxy

To make Gradle use HTTP Proxy when executing on Jenkins with Gradle plugin installed,
one has to use Environment variable JAVA_OPTS to pass proxy servers to Gradle.

For example:

JAVA_OPTS=-Dhttps.proxyHost=192.168.1.1 -Dhttps.proxyPort=3128 -Dhttp.proxyHost=192.168.1.1 -Dhttp.proxyPort=3128

Also one could use gradle.properties or configure gradle build files to use proxies.
Though JAVA_OPTS is a more universal way as one could have multy node configuration of Jenkins. For example: master does not use proxy, and some slave node is located behind proxy, so setting JAVA_OPTS in node environment will do the job.

Friday, August 30, 2013

HomeBrew: Installing right software to Mac OS X

Mac OS X can behave polite - one can install required software using HomeBrew http://brew.sh/

Homebrew installs the stuff you need that Apple didn't.
brew install wget

For example, let's prepare our Mac OS X for Jenkins CI

brew install  -v --devel --build-from-source --fresh wget automake autoconf libtool libzip pkg-config libimobiledevice



HtmlUnit and HtmlUnitDriver

Cool stuff. Give it a try later on, not for testing but just for some apps purposes.

HtmlUnit

This is a Java framework for testing webapps. It's a wrapper around the Jakarta HttpClient and provides a more friendly API. The project is hosted athttp://htmlunit.sourceforge.net/

HtmlUnit Driver

This is currently the fastest and most lightweight implementation of WebDriver. As the name suggests, this is based on HtmlUnit.

Pros

  • Fastest implementation of WebDriver
  • A pure Java solution and so it is platform independent.
  • Supports Javascript

Cons

  • Emulates other browser's JS behaviour (see below)

Javascript in the HtmlUnitDriver

None of the popular browsers uses the javascript engine used by HtmlUnit (Rhino). If you test javascript using HtmlUnit the results may differ significantly from those browsers.
When we say "javascript" we actually mean "javascript and the DOM". Although the DOM is defined by the W3C each browser out there has its own quirks and differences in their implementation of the DOM and in how javascript interacts with it. HtmlUnit has an impressively complete implementation of the DOM and has good support for using javascript, but it is no different from any other browser: it has its own quirks and differences from both the W3C standard and the DOM implementations of the major browsers, despite its ability to mimic other browsers.
With WebDriver, we had to make a choice; do we enable HtmlUnit's javascript capabilities and run the risk of teams running into problems that only manifest themselves there, or do we leave javascript disabled, knowing that there are more and more sites that rely on javascript? We took the conservative approach, and by default have disabled support when we use HtmlUnit. With each release of both WebDriver and HtmlUnit, we reassess this decision: we hope to enable javascript by default on the HtmlUnit at some point.

Enabling Javascript

If you can't wait, enabling Javascript support is very easy:
HtmlUnitDriver driver = new HtmlUnitDriver();
driver.setJavascriptEnabled(true);
or
HtmlUnitDriver driver = new HtmlUnitDriver(true);
This will cause the HtmlUnitDriver to emulate IE's Javascript handling by default.

Emulating a Specific Browser

Notwithstanding other considerations above, it is possible to get HtmlUnitDriver to emulate a specific browser. You should not really be doing this, as web-applications are better coded to be neutral of which reasonably recent browser you are using. There are two more constructors for HtmlUnitDriverthat take allow us to indicate a browser to emulate. One takes a browser version directly:
HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_3);
The other uses a broader capabilities mechanism:
HtmlUnitDriver driver = new HtmlUnitDriver(capabilities);

srcs:
https://code.google.com/p/selenium/wiki/HtmlUnit
https://code.google.com/p/selenium/wiki/HtmlUnitDriver

Start process, get PID of just started process, and terminate the process by PID later

Ok, let's say we have a usecase - a job on Jenkins CI that needs to start a background process, remember PID of the process, pass the PID to some later cleanup. And we need something that will work on Linux and on Mac OS X (yes, even this OS can behave polite).

So 1st, let's start a process and remember it's PID. This is done via $! shell environment variable. The $! variable will have PID of latest started process. So here is an example:

nohup tail -f /var/log/syslog > /tmp/$BUILD_TAG.log & export LOGGER_PID=$! 
# $BUILD_TAG - is an environment variable provided by Jenkins CI, it has unique job execution identifier

2nd, let's remember the PID

echo -e "LOGGER_PID=$LOGGER_PID" > /tmp/$BUILD_TAG

3rd, let's use the PID and send SIGTERM to the process if it is still alive. We'll wrap it to a shell script.

#!/bin/bash
. /tmp/$BUILD_TAG  # read to LOGGER_PID env variable

kill_proc(){

  local pid_alive=`ps ax| awk '{print $1}' | grep -e "^$LOGGER_PID$" >>/dev/null; echo $?`;
   # ps ax| awk '{print $1}' - will print to stdout PIDs without surrounding spaces
   # grep -e "^$LOGGER_PID$" >>/dev/null; - will search for the PID, ^ and $ are important, just in case our PID is a substring of some other PID on system
   # echo $? - will echo exit code of last command
  if [ $pid_alive -eq 0 ]; then 
    kill $LOGGER_PID; # sending  SIGTERM to the process
    sleep 10;
  else
    echo "Proc with PID $LOGGER_PID already terminated" 
  fi;  
}

kill_proc;




Thursday, August 29, 2013

Configure log-level of messages sent to console

To configure log-level of messages sent to console one can do as it said in /etc/sysctl.conf

# Uncomment the following to stop low-level messages on console
kernel.printk = 3 4 1 3

The four values in kernel.printk denote:
  • console_loglevel
  • default_message_loglevel
  • minimum_console_loglevel
  • default_console_loglevel respectively.
Following log levels are valid (man syslog):

#define KERN_EMERG    "<0>"  /* system is unusable               */
#define KERN_ALERT    "<1>"  /* action must be taken immediately */
#define KERN_CRIT     "<2>"  /* critical conditions              */
#define KERN_ERR      "<3>"  /* error conditions                 */
#define KERN_WARNING  "<4>"  /* warning conditions               */
#define KERN_NOTICE   "<5>"  /* normal but significant condition */
#define KERN_INFO     "<6>"  /* informational                    */
#define KERN_DEBUG    "<7>"  /* debug-level messages             */