Wednesday, November 24, 2010

Converting shapefiles (SHP) to a different projection

I've written a small perl script to handle converting the coordinates in shapefiles to a different coordinate set. You must specify the source EPSG projection and the destination EPSG projection, and the script will walk through the data and create a new shapefile with the coordinates transformed by using the new projection. The new files are saved next to the original files, with the projection number appended to the file names.

Currently the script supports POINT data and LINESTRING data. All other geometries are ignored.

In order to use this script you will need to have the following packages installed (example for ubuntu): shapelib, proj-bin, gdal-bin

Sample usage:
adrianp@frost:~/bin$ ./convertSHPProjection.pl epsg:4326 epsg:31700 ofm_fiber.shp
Going to save data as ofm_fiber_31700.[shp/shx/dbf]
Depending on your data size, this may take a while...
Copying dbf file...
Code:

#!/usr/bin/perl
use strict;
use warnings;

# Author: Adrian Popa
# License: GPLv2/3

# This script reprojects a shape file from a source projection to a destination projection by
# reprojecting each coordinate in the file. The destination file will be named like the source
# file with a suffix containing the projection code.

# This script requires the following binaries to be available (adjust the path to fit your system):
# On ubuntu you can get these binaries by running sudo apt-get install shapelib proj-bin gdal-bin
my $shpcreate = '/usr/bin/shpcreate';
my $shpadd = '/usr/bin/shpadd';
my $cs2cs = '/usr/bin/cs2cs';
my $ogrinfo = '/usr/bin/ogrinfo';

if(scalar (@ARGV) != 3){
    print "Incorrect number of arguments\n";
    usage;
}

my $epsg_in = $ARGV[0];
my $epsg_out = $ARGV[1];
my $in_file = $ARGV[2];

#validate parameters

if($epsg_in !~/^epsg:[0-9]+$/i){
    print "epsg_in is invalid\n";
    usage;
}

my $epsg_out_code = "0";
if($epsg_out!~/^epsg:([0-9]+)$/i){
    print "epsg_out is invalid\n";
    usage;
}
else{
    $epsg_out_code = $1;
}

if(! -f $in_file){
    print "$in_file is not a file\n";
    usage;
}

#determine shp file type

my @output = `$ogrinfo -so "$in_file" 2>&1`;
my $geometryType = undef;
foreach my $line (@output){
#    print "$line";
    if($line=~/\s\(Line String\)/){
        $geometryType = "linestring";
    }
    if($line=~/\s\(Point\)/){
        $geometryType = "point";
    }
    if($line=~/Unable to open datasource/){
        die "$in_file is not supported: $line\n";
    }
}

die "Unsupported geometry for $in_file. The only supported geometries are Line string and Point\n" if(!defined $geometryType);

#prepare destination file
my $file_base = $in_file;
$file_base=~s/\.shp$//i; #cut out the extension (if any)
my $out_file = $file_base;
$out_file.="_${epsg_out_code}"; #append the destination projection code

print "Going to save data as ${out_file}.[shp/shx/dbf]\n";
if($geometryType eq 'linestring'){
    print `$shpcreate "$out_file" arc 2>&1`;
}
if($geometryType eq 'point'){
    print `$shpcreate "$out_file" point 2>&1`;
}


print "Depending on your data size, this may take a while...\n";
@output = `$ogrinfo -al "$in_file" 2>&1`;
foreach my $line (@output){
    if($geometryType eq 'linestring'){
        if($line=~/^\s+LINESTRING \((.*)\)$/){
            my $linestring = $1;
#            print $linestring;
            my $projected = reproject($linestring);
           
            #add the line to the file
            print `$shpadd "$out_file" $projected 2>&1`;
           
        }
    }
    if($geometryType eq 'point'){
        if($line=~/^\s+POINT \((.*)\)$/){
            my $point = $1;
#            print $linestring;
            my $projected = reproject($point);
           
            #add the line to the file
            print `$shpadd "$out_file" $projected 2>&1`;
           
        }
    }
}

#we're almost done. Copy the dbf file unchanged (the order of the records is the same)
print "Copying dbf file...\n";
print `cp "$file_base.dbf" "$out_file.dbf" 2>&1`;



#take a line of coordinates and convert them. Return a string with the converted coordinates
sub reproject {
    my $string = shift;
    my $converted = "";
    my $coordinates = "";
    my @pairs = $string=~/([-0-9\.]+ [-0-9\.]+),?/g;
    foreach my $pair (@pairs){
#        print "DBG: $pair\n";
        my ($x, $y) = $pair=~/([-0-9\.]+) ([-0-9\.]+)/;
        $coordinates.= "$x $y\n";
    }
#    print "DBG: coordinates: $coordinates\n";
    #convert $coordinates (expensive, but we won't run into shell problems by converting everything at once)
    my $cmd = "echo '$coordinates' | $cs2cs +init=$epsg_in +to +init=$epsg_out -f '%0.6f' 2>&1";
#    print "DBG: $cmd";
    my @output = `$cmd`;
    foreach my $line (@output){
#        print $line;
        if($line=~/([-0-9\.]+)\s+([-0-9\.]+)/){
            next if ($line eq $output[-1]); #always skip the last line. It's not relevant to what we want
            my $x = $1;
            my $y = $2;
            $converted.=" $x $y";
        }
    }
   
#    print "DBG: converted:$converted\n";
    return $converted;
}


sub usage {
    print "Usage: $0 epsg_in epsg_out file.shp

epsg_in is the source EPSG projection code (e.g. epsg:31700)
epsg_out is the destination EPSG projection code (e.g. epsg:4326)
file.shp is the source shape file

Example: $0 epsg:31700 epsg:4326 streets.shp
";
    exit;
}

Saturday, October 16, 2010

Using AnyData ADU-510A under Linux (Ubuntu 10.04)

Using AnyData ADU-510A CDMA modem under Linux is a bit of a challenge because the device will appear like a virtual CD-ROM instead of a modem. You will need to "switch" the device (meaning turning it from being a CD-ROM to a modem), and then connect through PPP to your provider's network.

Here are detailed steps on how to accomplish this on Ubuntu 10.04, using the modem associated with Romtelecom Clicknet Mobile service. It should work the same for other providers.

1. Download Saki's3g - a script that automatically switches your device from a CD-ROM state to a modem. You can get the latest version from http://www.sakis3g.org/ (great work, Saki!)
Even if the site says it doesn't support CDMA networks, it can still be used to switch our modem using the embedded usb_modeswitch.

Download the full version for your architecture and gunzip it somewhere in your home directory (~/bin for example). Also, give execute permissions to the script:
adrianp@stingray:~/bin$ gunzip sakis3g.gz
adrianp@stingray:~/bin$ chmod a+x sakis3g
adrianp@stingray:~/bin$ ls -l sakis3g
-r-x--x--x 1 adrianp adrianp 214619 2010-09-30 18:23 sakis3g

2. Plug in your modem
If you do a lsusb, you should see the following:
adrianp@stingray:~$ lsusb | grep Qual
Bus 006 Device 002: ID 05c6:1000 Qualcomm, Inc.

This means that the device is registered as a generic device, or maybe as a CD-ROM (it doesn't register as a CD-ROM for me, though).

3. Switch your device with sakis3g
3a. Start sakis3g as a regular user
adrianp@stingray:~/bin$ ./sakis3g 


3b. From the GUI menu select "Connect with 3G"
The process will switch your modem and try to connect to a GSM network. It will fail, but these errors are not critical in our process.








When the program finishes, click cancel to close the program.
If you do a lsusb now, you should see the following:
adrianp@stingray:~$ lsusb | grep Any
Bus 006 Device 003: ID 16d5:6502 AnyDATA Corporation

The device is on the same bus, but it has changed its signature. Also if you do a dmesg, you will get information about new serial ports that are recognized by the system:
[ 3873.613206] sr 7:0:0:0: [sr1] Media Changed
[ 3873.613215] sr 7:0:0:0: [sr1] Result: hostbyte=DID_OK driverbyte=DRIVER_SENSE
[ 3873.613225] sr 7:0:0:0: [sr1] Sense Key : Unit Attention [current]
[ 3873.613235] sr 7:0:0:0: [sr1] Add. Sense: No additional sense information
[ 3873.613247] sr 7:0:0:0: [sr1] CDB: Read(10): 28 00 00 00 59 f0 00 00 08 00
[ 3873.613272] end_request: I/O error, dev sr1, sector 23024
[ 3873.613283] Buffer I/O error on device sr1, logical block 11512
[ 3873.613291] Buffer I/O error on device sr1, logical block 11513
[ 3873.613298] Buffer I/O error on device sr1, logical block 11514
[ 3873.613304] Buffer I/O error on device sr1, logical block 11515
[ 3873.613673] isofs_fill_super: bread failed, dev=sr1, iso_blknum=16, block=32
[ 3873.651211] sr1: CDROM (ioctl) error, command: Get configuration 46 00 00 00 00 00 00 00 20 00
[ 3873.651240] sr: Sense Key : Hardware Error [current]
[ 3873.651250] sr: Add. Sense: No additional sense information
[ 3873.669146] option: option_instat_callback: error -108
[ 3873.669401] option1 ttyUSB0: GSM modem (1-port) converter now disconnected from ttyUSB0
[ 3873.669451] option 6-2:1.0: device disconnected
[ 3873.670499] option1 ttyUSB1: GSM modem (1-port) converter now disconnected from ttyUSB1
[ 3873.670533] option 6-2:1.1: device disconnected
[ 3873.671481] option1 ttyUSB2: GSM modem (1-port) converter now disconnected from ttyUSB2
[ 3873.671514] option 6-2:1.2: device disconnected
[ 3873.784545] usb 6-2: reset full speed USB device using uhci_hcd and address 3
[ 3873.930278] option 6-2:1.2: GSM modem (1-port) converter detected
[ 3873.930487] usb 6-2: GSM modem (1-port) converter now attached to ttyUSB0
[ 3873.930549] option 6-2:1.1: GSM modem (1-port) converter detected
[ 3873.930680] usb 6-2: GSM modem (1-port) converter now attached to ttyUSB1
[ 3873.930736] option 6-2:1.0: GSM modem (1-port) converter detected
[ 3873.930873] usb 6-2: GSM modem (1-port) converter now attached to ttyUSB2
[ 3873.946147] option: option_instat_callback: error -108
[ 3873.946409] option1 ttyUSB2: GSM modem (1-port) converter now disconnected from ttyUSB2
[ 3873.946457] option 6-2:1.0: device disconnected
[ 3873.946619] option1 ttyUSB1: GSM modem (1-port) converter now disconnected from ttyUSB1
[ 3873.946660] option 6-2:1.1: device disconnected
[ 3873.946808] option1 ttyUSB0: GSM modem (1-port) converter now disconnected from ttyUSB0
[ 3873.946848] option 6-2:1.2: device disconnected
[ 3874.064071] usb 6-2: reset full speed USB device using uhci_hcd and address 3
[ 3874.210264] option 6-2:1.2: GSM modem (1-port) converter detected
[ 3874.210469] usb 6-2: GSM modem (1-port) converter now attached to ttyUSB0
[ 3874.210529] option 6-2:1.1: GSM modem (1-port) converter detected
[ 3874.210661] usb 6-2: GSM modem (1-port) converter now attached to ttyUSB1
[ 3874.210717] option 6-2:1.0: GSM modem (1-port) converter detected
[ 3874.210851] usb 6-2: GSM modem (1-port) converter now attached to ttyUSB2
[ 3874.222146] option: option_instat_callback: error -108
[ 3874.222408] option1 ttyUSB2: GSM modem (1-port) converter now disconnected from ttyUSB2
[ 3874.222457] option 6-2:1.0: device disconnected
[ 3874.222622] option1 ttyUSB1: GSM modem (1-port) converter now disconnected from ttyUSB1
[ 3874.222665] option 6-2:1.1: device disconnected
[ 3874.222817] option1 ttyUSB0: GSM modem (1-port) converter now disconnected from ttyUSB0
[ 3874.222857] option 6-2:1.2: device disconnected
[ 3874.340072] usb 6-2: reset full speed USB device using uhci_hcd and address 3
[ 3874.486264] option 6-2:1.2: GSM modem (1-port) converter detected
[ 3874.486473] usb 6-2: GSM modem (1-port) converter now attached to ttyUSB0
[ 3874.486533] option 6-2:1.1: GSM modem (1-port) converter detected
[ 3874.486665] usb 6-2: GSM modem (1-port) converter now attached to ttyUSB1
[ 3874.486720] option 6-2:1.0: GSM modem (1-port) converter detected
[ 3874.486853] usb 6-2: GSM modem (1-port) converter now attached to ttyUSB2
[ 3874.498145] option: option_instat_callback: error -108
[ 3874.498406] option1 ttyUSB2: GSM modem (1-port) converter now disconnected from ttyUSB2
[ 3874.498455] option 6-2:1.0: device disconnected
[ 3874.498621] option1 ttyUSB1: GSM modem (1-port) converter now disconnected from ttyUSB1
[ 3874.498661] option 6-2:1.1: device disconnected
[ 3874.498812] option1 ttyUSB0: GSM modem (1-port) converter now disconnected from ttyUSB0
[ 3874.498852] option 6-2:1.2: device disconnected
[ 3874.616074] usb 6-2: reset full speed USB device using uhci_hcd and address 3
[ 3874.763247] option 6-2:1.2: GSM modem (1-port) converter detected
[ 3874.763456] usb 6-2: GSM modem (1-port) converter now attached to ttyUSB0
[ 3874.763517] option 6-2:1.1: GSM modem (1-port) converter detected
[ 3874.763653] usb 6-2: GSM modem (1-port) converter now attached to ttyUSB1
[ 3874.763712] option 6-2:1.0: GSM modem (1-port) converter detected
[ 3874.763853] usb 6-2: GSM modem (1-port) converter now attached to ttyUSB2
[ 3874.797988] sr1: CDROM (ioctl) error, command: Xpwrite, Read disk info 51 00 00 00 00 00 00 00 02 00
[ 3874.798019] sr: Sense Key : Hardware Error [current]
[ 3874.798028] sr: Add. Sense: No additional sense information
[ 3874.855226] sr1: CDROM (ioctl) error, command: Get configuration 46 00 00 00 00 00 00 00 20 00
[ 3874.855255] sr: Sense Key : Hardware Error [current]
[ 3874.855265] sr: Add. Sense: No additional sense information
[ 3875.024201] sr1: CDROM (ioctl) error, command: Get configuration 46 00 00 00 00 00 00 00 20 00
[ 3875.024231] sr: Sense Key : Hardware Error [current]
[ 3875.024241] sr: Add. Sense: No additional sense information
[ 3875.198205] sr1: CDROM (ioctl) error, command: Get configuration 46 00 00 00 00 00 00 00 20 00
[ 3875.198236] sr: Sense Key : Hardware Error [current]
[ 3875.198246] sr: Add. Sense: No additional sense information
[ 3875.357209] sr1: CDROM (ioctl) error, command: Get configuration 46 00 00 00 00 00 00 00 20 00
[ 3875.357241] sr: Sense Key : Hardware Error [current]
[ 3875.357251] sr: Add. Sense: No additional sense information
[ 3875.552201] sr1: CDROM (ioctl) error, command: Get configuration 46 00 00 00 00 00 00 00 20 00
[ 3875.552232] sr: Sense Key : Hardware Error [current]
[ 3875.552242] sr: Add. Sense: No additional sense information

The output also shows that the system recognized the virtual CD-ROM (but it may still be unaccesible - it's not needed anyway). In the end, you will get three new files in /dev:
adrianp@stingray:~$ ls -l /dev/ttyUSB*
crw-rw---- 1 root dialout 188, 0 2010-10-16 09:37 /dev/ttyUSB0
crw-rw---- 1 root dialout 188, 1 2010-10-16 09:37 /dev/ttyUSB1
crw-rw---- 1 root dialout 188, 2 2010-10-16 09:37 /dev/ttyUSB2

4. Using gnome-ppp to connect to the Internet
Make sure you have gnome-ppp installed (sudo apt-get install gnome-ppp), or you can use a ppp program of your choice. This step has to be performed as root, since performing it as a regular user has generated PPP authentication problems that I didn't bother to debug.

4a. Configure gnome-ppp (it's done only once, since it can remember the settings):

  • Enter your username/password/number that were provided by your ISP.

  • Click Setup and click "Detect". It should register /dev/ttyUSB2 as a valid modem interface

  • Click "Init Strings" and set the following as Init 2:
ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0

  • Tweak other parameters if you fell you need to, but there should be no need.
  • Click close and return to the main window of gnome-ppp

4b. Turn on the PPP connection by clicking Connect
Make sure that you have other network interfaces turned off (e.g. Wlan, ethernet) because the PPP dialer might conflict with NetworkManager when setting DNS or default routes.

The modem should connect right away. If you get an error 'NO CARRIER', then check that the modem works fine under Windows - I got the error and had to replace the modem because it was faulty.


root@stingray:/home/adrianp/bin# gnome-ppp
WVCONF: /root/.wvdial.conf
GNOME PPP: Connecting...
GNOME PPP: STDERR: --> WvDial: Internet dialer version 1.60
GNOME PPP: STDERR: --> Cannot get information for serial port.
GNOME PPP: STDERR: --> Initializing modem.
GNOME PPP: STDERR: --> Sending: ATZ
GNOME PPP: STDERR: OK
GNOME PPP: STDERR: --> Sending: ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
GNOME PPP: STDERR: ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
GNOME PPP: STDERR: OK
GNOME PPP: STDERR: --> Modem initialized.
GNOME PPP: STDERR: --> Sending: ATM1L3DT#777
GNOME PPP: STDERR: --> Waiting for carrier.
GNOME PPP: STDERR: ATM1L3DT#777
GNOME PPP: STDERR: CONNECT
GNOME PPP: STDERR: --> Carrier detected.  Waiting for prompt.
GNOME PPP: STDERR: ~[7f]}#@!}!}!} }9}"}&} } } } }#}%B#}%}%}&} },}6m}'}"}(}"Li~
GNOME PPP: STDERR: --> PPP negotiation detected.
GNOME PPP: STDERR: --> Starting pppd at Sat Oct 16 09:53:37 2010
GNOME PPP: STDERR: --> Pid of pppd: 16764
GNOME PPP: STDERR: --> Using interface ppp0
GNOME PPP: STDERR: --> local  IP address 92.86.95.69
GNOME PPP: STDERR: --> remote IP address 192.168.50.12
GNOME PPP: STDERR: --> primary   DNS address 193.231.100.120
GNOME PPP: STDERR: --> secondary DNS address 193.231.100.124


If you get errors during the switching procedure or during connecting through PPP, they might be caused by the device - reseat the device in a different USB port and make sure both leds (red/green) are lit before proceeding.

5. Use the Internet/profit - you should get the same performance as you would while running Windows.



Known issues:

  • You will need to do steps 2-5 (skip 4a) each time you plug in your modem
  • If your laptop goes to sleep mode, you might need to unplug and replug the modem before it works again
  • If you disconnect and try to connect immediately, you will get errors saying that the device is busy. Just wait for ~10 seconds before reconnecting

Good luck and happy surfing!

Wednesday, September 22, 2010

Cisco ANA: Change telnet credentials for all VNEs in an AVM

I wrote some scripts that will change the telnet username/password for all VNEs in a specific AVM. This is useful in order to change a password in bulk, or to comply to security policies.

The scripts have been tested with ANA 3.6.7 and only work with VNEs that use telnet access.

Details, examples and bugs can be found inside the README.txt from the scripts package: Download from here

Enjoy

Monday, June 14, 2010

Connecting with remote desktop (rdp) to a linux workstation

If you would like to connect to your Linux workstation the same way you connect to Windows workstations, or if you want to be able to connect from any Windows system to your Linux desktop without any other programs, than you probably want to use Remote Desktop Protocol. The good news is that Linux has an open-source implementation of the RDP protocol!
Basically, the solution uses the following:
  • xrdp to have your own RDP-speaking server - which will allow you to connect using Microsoft Remote Desktop, or any other client implementation (rdesktop, remmina)
  • X11vnc will provide the interface to your desktop (it connects to an active X session, unlike other VNC servers which create a new session)
First, you need to setup X11vnc and make sure it works properly; then you can setup xrdp and make the connection between xrdp and x11vnc. Then you are done.

  1. Setup x11vnc
    • install x11vnc on your system (e.g. apt-get install x11vnc)
    • configure x11vnc to start when you login (via the GUI). Basically, run the following command as part of the user startup scripts (e.g: Xfce Settings Manager -> Session and Startup -> Application autostart):
      • x11vnc -usepw -display :0 -forever -clientdpms -repeat -xkb -gone 'xlock&'
    • Here's what the options do:
      • usepw - first time asks for a password (so make sure you run the command from the command line), then it allows access only if the password is correct. In theory, x11vnc can use system login, but it hasn't worked for me. Look into the man page for other types of authentication
      • display :0 - which XServer to attach to. Leave :0 if unsure
      • forever - after the first client disconnects, the server restarts and accepts other requests
      • clientdpms - try to blank the attached screen(s) on the remote computer - so that others can't see what you are doing (like RDP, and unlike traditional VNC). Please note, there are ways to override this and you are not 100% protected! See x11vnc manual for details
      •  repeat - allow keyboard repeat (otherwise keeping up-arrow pressed generates just one event). Disable this option if you notice you get a lot of keys repeated when pressed only once.
      • xkb - allows you to use your full keyboard layout (without it, sometimes SHIFT doesn't work, so it's better to keep it enabled)
      •  gone - what program to run when the user disconnects - normally this should lock your terminal, or start a screensaver, so that others will not have direct access to your workstation. Replace xlock by your favourite locking program (e.g. xflock4 for xfce)
    • Once you have setup and started x11vnc, do some tests (connect with a VNC viewer on port 5900), and see if everything works as expected. Please note, the configuration above was tested on a dual-display setup (with Xinerama) and both displays are exported and available through VNC.
  2. Setup xrdp
    • install xrdp on your system (e.g. apt-get install xrdp)
    • change the xrdp configuration so that it can connect to x11vnc:
      •  add this to your /etc/xrdp/xrdp.ini as section xrdp1 (so that it will be the default:
      •  [xrdp1]
        name=X11vnc
        lib=libvnc.so
        ip=127.0.0.1
        port=5900
        username=ask
        password=ask
      • Rename or remove the other xrdp* sections from the config file, so there are no overlapping section names.
      • Save the config file and restart xrdp/sessman: /etc/init.d/xrdp restart
      • Now, you should be able to connect to port 3389 (RDP) and you will get a xrdp login window. The login credentials supplied here will be sent to x11vnc for validation. Please note, that in this setup, x11vnc only asks for a password, but xrdp asks for username and password - in this case, the username can be anything.
      • Once the login is successful, you will get a debug window and control is passed to x11vnc. 
      • Once you are done testing, you can add iptables rules to disable access for port 5900 from the network, because it will be accessed only from the loopback interface. Or you can leave it on, in case you want to connect just by using VNC...
xrdp login window
xrdp connection to x11vnc
connected - the remote side is the window with the green desktop

This method has the advantage of bringing a "terminal services" experience to the Linux desktop - you can start an X session, start some programs, leave the session running on your workstation, and connect remotely from anywhere and connect to the actual session, being able to manipulate the same programs you have started. 

Disadvantages and caveats:
  • vnc is usually slow
  • the communication between xrdp and x11vnc is not encrypted (credentials and data is passed in clear-text), but this communication takes place just inside your workstation (local)
  • the communication between your RDP client and xrdp is done either unencrypted or lightly encrypted - so, it would be advisable to use ssh tunnels, or VPNs for the actual encryption.
  • the xrdp server can't resize your desktop to fit the client window (like the windows version can), so it's better to use clients which have scrollbars, or to specify in the x11vnc configuration the scaling of your desktop. As a Linux RDP client I recommend using remmina because it can pan and scroll if your remote desktop is larger than the local one.
  • You can have only one connection/only one user - or you will need to adjust your config (mainly the vnc ports) if you need more than one connection.
Give it a try and happy remoting :)

Wednesday, April 7, 2010

Starting LogMeIn in Safe Mode with Networking

The trouble with having remote access to a computer is that most of the time it fails miserably when you really need to fix that computer. For instance, you can't connect through LogMeIn to a computer started in Safe Mode (with Networking).

I did a test today and if you start a PC in Safe Mode with Networking and you go to the Services application, if you try to right click and start the "LogMeIn" process, it will say it can't be started in safe mode. Bummer.

Thankfully, there is a solution for that - you add the service to the trusted list of processes for Safe Mode, and presto, you can start it.

You just need to add a key in the registry using this rather obscure command (start a new command prompt), and you are almost done:

reg add "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\SafeBoot\Network\LogMeIn" /VE /T REG_SZ /D "Service"

(one line). The output should be like in the next picture


Now, the process can be started in safe mode (with networking) by right clicking it in Services and selecting "Start". Don't worry if the process takes a long time to start or if it generates an error saying that it takes too long to start. Even in this crippled state you can log on remotely and do your thing.

This trick is persistent across reboots. The next time you get in Safe Mode, LogMeIn will be started on boot (bootup may be slower, though). Beware, there is a risk - if for some reason LogMeIn is the cause for Windows not starting in the Normal boot, it will also hang in Safe Mode with Networking!

 Good luck!

Tuesday, February 23, 2010

samplicator system-v startup scripts

Samplicator looks like a great UDP fan-out tool for Linux/Unix.
Unfortunately, it doesn't have proper startup/shutdown scripts.

Here are, for your convenience such startup scripts adapted from apmd (currently tested only on Linux). Dependencies are: GNU grep and perl (to emulate a PID mechanism).


#!/bin/sh
#
# chkconfig: 2345 26 74
# description: samplicator is a daemon to fanout UPD packets
# processname: samplicator-syslog

# Source function library.
. /etc/init.d/functions

RETVAL=0
SERVICE="syslog"
UDPLISTENPORT=514
SOURCEIP="10.0.250.131"
DESTINATIONS="10.0.250.132/514 10.0.250.61/514";
PIDFILE="samplicator-$SERVICE.pid"

start() {
  echo -n $"Starting up samplicator-$SERVICE daemon: "
  daemon --check samplicator-$SERVICE /usr/bin/samplicate -f -p $UDPLISTENPORT -s $SOURCEIP -b 8388608 -S $DESTINATIONS; netstat -upan | grep -P "$SOURCEIP:$UDPLISTENPORT.*samplicate" | perl -e 'my $line=; $line=~/([0-9]+)\//; print "$1\n";' >/var/run/$PIDFILE
  RETVAL=$?
  [ $RETVAL -eq 0 ] && touch /var/lock/subsys/samplicator-$SERVICE
  echo
  return $RETVAL
}

stop() {
  echo -n $"Shutting down samplicator-$SERVICE daemon: "
  killproc samplicator-$SERVICE
  RETVAL=$?
  [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/samplicator-$SERVICE
  echo
  return $RETVAL
}

dostatus() {
  #determine if the program is running based on the UDP port
  STATUS=`netstat -upan | grep -P "$SOURCEIP:$UDPLISTENPORT.*samplicate"|wc -l`;
  if [ $STATUS -eq 1 ]; then
    echo "samplicator-$SERVICE is running, bound on UDP port $UDPLISTENPORT";
  else
  #           echo "samplicator-$SERVICE is not running.";
    status samplicator-$SERVICE
  fi
}

restart() {
  stop
  start
}

condrestart() {
  [ -e /var/lock/subsys/samplicator-$SERVICE ] && restart || :
}

# See how we were called.
  case "$1" in
  start)
    start
  ;;
  stop)
    stop
  ;;
  status)
    dostatus
  ;;
  restart|reload)
    restart
  ;;
  condrestart)
    condrestart
  ;;
  *)
    echo $"Usage: samplicator-$SERVICE {start|stop|status|restart|reload|condrestart}"
    exit 1
  esac

exit $RETVAL 
 
The above example is for a syslog fan-out daemon. It can be easily changed to a snmp-trap or netflow fanout startup script by changing the variables at the top. Multiple concurrent daemons can run on different ports.

Good luck