Pages

2009-09-29

Input Error: There is no script engine for file extension ".vbs".

I associated Notepad++ with the VBS extension which was a BIG mistake - cscript and wcsript denied execution of vbs scripts with the following message:

Input Error: There is no script engine for file extension ".vbs".

Running Windows 7 I wasn't sure whether it helps, but followed the instuctions on this link, the registration of the dll didn't change much, but the fix of the vbs file association under this link did the magic. Regedit of Windows 7 complained however that now all information could be written in the registry, but wcscript and csript accepted to run vbs scripts again.


That's it!

Shell function to print "du -s" output in ascending order of the size

I ran in a problem where a du -sh output showed me the disk usage in human readable format (GB, MB, etc) but the output order was based on file names and not on the size. So I wrote this little awk (gawk) wrapper:

Declaration:

function duh_sorted {
    du -s $*|sort -n|\
    awk 'BEGIN { m = 1024; g = m*m; t=g*m} { \
    \
    if($1>t) { human_size=$1/t; unit="TB"; } \
    else { \
        if($1>g) { human_size=$1/g; unit="GB"; } \
        else { \
            if($1>m) { human_size=$1/m; unit="MB"; } \
            else { human_size=$1; unit="KB";} \
            } \
    } \
    printf("%4ld %c\t%s\n", human_size,unit,$2)
    }'
}

Usage:

$ duh_sorted /data/nfs/*

   8 K  /data/nfs/scripts
  32 K  /data/nfs/extensions
 151 M  /data/nfs/patches
 197 M  /data/nfs/logs
   3 G  /data/nfs/filerepo
  14 G  /data/nfs/mondo

 
$ duh_sorted /
 44 G  /


That's it!

2009-09-28

Parse XML file with Xpath in Java

Parse XML file MyxmlFile.xml with Xpath "/File/Tag[@name='myAttName']/SubTag" and wrtie the result in mySubTagContent:


DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
doc = docBuilder.parse (new File("MyxmlFile.xml"));


// normalize text representation
doc.getDocumentElement().normalize ();


Node myNode = SelectNode("/File/Tag[@name='myAttName']/SubTag");


if(myNode!=null) String mySubTagContent = myNode.getTextContent());


private Node SelectNode(String path) throws XPathExpressionException {
    Node child; XPathFactory xpf = XPathFactory.newInstance();
    XPath xp = xpf.newXPath();
    child = (Node) (xp.evaluate(path,doc, XPathConstants.NODE));
    return child;
}

That's it!

RegExp pattern matching with Java

Match String s with regular expression r:

import java.util.regex.Pattern;

public boolean RegMatch(String s, String r) {
    Pattern p = Pattern.compile(r);
    return p.matcher(s).matches();
}


That's it!

Get PID of an application by its listening TCP port using Win32 API

--- PidByPort.cpp Start ---

#include <winsock2.h>
#include <malloc.h>
#include "iphlpapi_2.h"
#include <stdio.h>

long GetPIDbyPort(long port) {


    long ret = 0; PVOID pTCPTable = NULL; DWORD size = 0; DWORD result = 0; result = pGetExtendedTcpTable(NULL, &size, true, AF_INET, TCP_TABLE_OWNER_MODULE_ALL, 0); while(result == ERROR_INSUFFICIENT_BUFFER){
      if(pTCPTable != NULL){
        free(pTCPTable);
      }
      pTCPTable = malloc(size); result = pGetExtendedTcpTable(pTCPTable, &size, true, AF_INET, TCP_TABLE_OWNER_MODULE_ALL, 0); if(result != NO_ERROR){
        printf("Failed to get TCP Table %s\n", GetLastError()); free(pTCPTable); return ret;
      }
    } if(result != NO_ERROR){
      printf("Failed to get size estimation %s\n", GetLastError()); return ret;
    } for(DWORD i = 0; i < ((PMIB_TCPTABLE_OWNER_MODULE)pTCPTable)->dwNumEntries; i++){
      MIB_TCPROW_OWNER_MODULE module = ((PMIB_TCPTABLE_OWNER_MODULE)pTCPTable)->table[i]; if(port==htons((short)module.dwLocalPort)) {
        #ifdef DEBUG printf("PID = %d, ", module.dwOwningPid); printf("State = %s\n ", g_mibTCPState[module.dwState]); printf(" LocalAddress = %d.%d.%d.%d, ", (htonl(module.dwLocalAddr) >> 24) & 0xFF, (htonl(module.dwLocalAddr) >> 16) & 0xFF, (htonl(module.dwLocalAddr) >> 8)& 0xFF, (htonl(module.dwLocalAddr)) & 0xFF); printf("LocalPort = %u,\n ", htons((short)module.dwLocalPort)); if(module.dwState != MIB_TCP_STATE_LISTEN){
          printf(" RemoteAddress = %d.%d.%d.%d, ", (htonl(module.dwRemoteAddr) >> 24) & 0xFF, (htonl(module.dwRemoteAddr) >> 16) & 0xFF, (htonl(module.dwRemoteAddr) >> 8) & 0xFF, (htonl(module.dwRemoteAddr)) & 0xFF); printf("RemotePort = %u\n ", htons((short)module.dwRemotePort));
        } printf(" TimeStamp = %I64d, ", module.liCreateTimestamp); printf(" ULONGLONG = UnDocumentedMeaning\n"); printf("\n"); #endif ret=module.dwOwningPid;
      }
    } if(pTCPTable != NULL) {
      free(pTCPTable);
    } return ret;
} --- PidByPort.cpp End -- --- iphlpapi_2.h Start --- #include <Iprtrmib.h> char* g_mibTCPState[] = {
    "???", "MIB_TCP_STATE_CLOSED", "MIB_TCP_STATE_LISTEN", "MIB_TCP_STATE_SYN_SENT", "MIB_TCP_STATE_SYN_RCVD", "MIB_TCP_STATE_ESTAB", "MIB_TCP_STATE_FIN_WAIT1", "MIB_TCP_STATE_FIN_WAIT2", "MIB_TCP_STATE_CLOSE_WAIT", "MIB_TCP_STATE_CLOSING", "MIB_TCP_STATE_LAST_ACK", "MIB_TCP_STATE_TIME_WAIT", "MIB_TCP_STATE_DELETE_TCB" };
typedef enum {
    TCP_TABLE_BASIC_LISTENER, TCP_TABLE_BASIC_CONNECTIONS, TCP_TABLE_BASIC_ALL, TCP_TABLE_OWNER_PID_LISTENER, TCP_TABLE_OWNER_PID_CONNECTIONS, TCP_TABLE_OWNER_PID_ALL, TCP_TABLE_OWNER_MODULE_LISTENER, TCP_TABLE_OWNER_MODULE_CONNECTIONS, TCP_TABLE_OWNER_MODULE_ALL } TCP_TABLE_CLASS, *PTCP_TABLE_CLASS;
DWORD (WINAPI *pGetExtendedTcpTable)(
    PVOID pTcpTable, PDWORD pdwSize, BOOL bOrder, ULONG ulAf, TCP_TABLE_CLASS TableClass, ULONG Reserved );
--- iphlpapi_2.h End--- --- Compiling --- cl -c PidByPort.cpp /D"_CRT_SECURE_NO_DEPRECATE" /EHcs /DDEBUG

That's it!

2009-09-27

Allow root to login in GDM

To configure GDM to allow root login, the /etc/gdm/gdm.conf-custom file needs to be modified. If the file doe not exist, just make a copy of /etc/gdm/gdm.conf

The reason to use this file instead of the /etc/gdm/gdm.conf file is the the latter can be changed during an update, and in later versions of GDM, the gdm.conf-custom file has been provided for user overrides of settings.

Add the following entry in the [security] section:
AllowRoot=true
PS: If you want to login as root using a remote X session, just add the following entry:

AllowRemoteRoot=true

Restart GDM, that's it.

Create an application shortcut in Gnome Application menu

Suppose you want to run an third party application from the Gnome Application panel which was compiled from a tarball for instance and does not appear in the menu.

Create a text file /usr/share/applications/<APP_NAME>.desktop with your favoured editor (e.g. vi) with the following content:

[Desktop Entry]
Name=Shortcut's name
Comment=Shortcut's tool tip text
Exec=Absolute path to the executable
Icon=Absolute path to the application icon file (e.g. PNG)
Terminal=false (or true if you want to run it in a terminal)
Type=Application
Categories=Application;Network;

Categories can be AudioVideo, FileTransfer, etc. To get an idea of avaliable categories, just grep in the existing shortcuts for categories:

# grep Categories /usr/share/applications/*.desktop

That's it!

Flash Player plugin for Iceweasel in Debian

It's a matter of the correct symbolic link to enable the flash player
plugin of your choice.
Just symlink the libflashplayer.so to the plugin directory of the
browser, e.g.:
ln -s /etc/alternatives/libflashplayer.s /usr/lib/iceweasel/plugins
That's it!

Enable Firefox Java plugin in Solaris

Suppose you installed a recent Firefox e.g. from http://www.sunfreeware.com and want to use it with a Java plugin, e.g. for Navisphere, BEA Admin Console or other management UI's.
Get a JRE which suits with your needs and install it on your Solarix
box. Then create a symbolic link to the plugins directory of you Firefox
installation, for instance:
# ln -s <JRE>/plugin/sparc/ns7/libjavaplugin_oji.so /opt/sfw/firefox/plugins
Start your Firefox and connect to the URL which needs the Java Plugin.
That's it!

Don't display last user name in Windows Vista & Windows 7

To make Windows Vista and Windows 7 forget the name of the last logged in user (e.g. to improve the security on your box), you have to set the corresponding local group policy.

Run secpol.msc in Administrator mode and navigate to Security Settings -> Local Policies -> Security Options and set the policy:

"Interactive Logon: Don't display last user name" to "Enable"

That's it.

PS: You can enable CTRL-ALT-DELETE before the logon mask in the same area. Just set the policy :

"Interactive logon: Do not require CTRL+ALT+DEL" to "Disable"

Enable administrative shares in Windows Vista & Windows 7

The administrative shares are disabled in Windows Vista & Windows 7 by default. In order to access your drives remotely, addressing them like \\hostname\C$ or \\hostname\D$, you have to add a key to the policy hive in the registry:
  • run regedit.exe (in Administrator mode)
  • navigate to
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\system

  • create a new DWORD value and name it LocalAccountTokenFilterPolicy
  • set the value of LocalAccountTokenFilterPolicy to 1
  • reboot and try to access your administrativ shares (e.g. \\localhost\c$)
That's it!

Allow ping in Windows Vista & Windows 7

The ICMP echo request (ping) is blocked in the Windows Firewall in Windows Vista and Windows 7 by default. The quickest way to add end exception is to run netsh.exe from command propmpt rather than clicking your way through wf.msc

Open up command prompt (cmd.exe) in Administrator mode and type in the following command:
> netsh firewall set icmpsetting 8 enable

That's it, your box should be pingabble from now on.