Wednesday, June 24, 2009

Replacing folders on a network with psexec and xcopy

rem We can run a process remotely with psexec
rem
rem cmd /C allows us to run a process in a cmd window and it automatically terminates

for /l %a in (1,1,50) do psexec \\station-%a cmd /C "move c:\path\to\folder c:\path\to\backup"

rem Now lets replace that folder with a new one using xcopy
rem
rem We use our local copy of the folder c:\path\to\folder and copy it to each station
rem
rem /S /E /H /I switches for xcopy ensure that we get hidden files, sub directories, and empty directories. /I assumes that the destination is a folder.

for /l %a in (1,1,50) do start cmd /k "xcopy /S /E /H /I c:\path\to\folder \\station-%a\c$\path\to\folder"

Wednesday, June 17, 2009

Get mac serial no from terminal

system_profiler SPHardwareDataType | grep "Serial Number" | sed 's/Serial Number://'

Tuesday, June 16, 2009

Php basics : error handling, tests, array functs, classes, and scope

Checking if a file exists

if (file_exists($some_file)){
...
}

Basic Error Handling

function($var) or exit("some error");


Printing an array


print_r();


Creating a class

class SomeClass{

private some_var;

public function SomeClass(){}

public function new_fun($r){
print_r($r);
echo $this->some_var;

}

}


Defining an array

$my_array = array('elem1','elem2');

Adding an array element

array_push($array,$elem)


Checking if a key exists

array_key_exists("some_name",$r)


Using a global var in a function

global $global_var;

JQuery basics : working with divs and text boxes, and checking for null elements

Adding text to a div's innerHTML

$("div#some_div").append(s);


Getting text from a div

var text = $("div#some_div" ).html();


Getting/Setting value from text box

var text = $("#some_textbox").val();
$("#some_textbox").val("bla");


Checking if document element is null

if (!$("#some_elem").length){
...

}

Monday, June 15, 2009

Get the distance between two latitude/longitude points in km (example)

Here is a python script to convert lat/lng point to meters. It expects the points to be entered in as decimal degrees. It outputs the distance in km.

For example:

python distlatlng.py 36.12 -86.67 33.94 -118.40
the distance in km is 2887.25995061



#uses the haversine formula
# based on code from http://www.movable-type.co.uk/scripts/latlong.html
import math, sys

def main(*args):
if len(args) < 5:
print args[0]," lng1 lat1 lng2 lat2 (finds distance between two lat/lng pairs)"
return
lat1 = float(args[1])
lng1 = float(args[2])
lat2 = float(args[3])
lng2 = float(args[4])
radius = 6372.8 # earth's mean radium in km
dlat = math.radians(lat2-lat1)
dlng = math.radians(lng2-lng1)
a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1)) * math.cos(math.radians(lat2)) * math.sin(dlng/2)*math.sin(dlng/2)
c = 2*math.atan2(math.sqrt(a),math.sqrt(1-a))
dist = radius*c
print "the distance in km is " , dist


if __name__ == "__main__":
main(*sys.argv)


Python basics : math functions, main funct

Math functions
#Unfortunately, you need to import math, but python has a nice library of math functions



import math
math.pow(2,3)
math.cos(math.pi/2) #<-- radians!
math.pi
math.sqrt(4)



Main function

#Making a main function in Python is a little tricky. Here is an example:



def main(*args):
if len(args) < 4:
return

print "main : args[0] ", args[0], " args[1]", args[1]
print "(main) : args[1] ", args[2]
print "(main) : args[3] ", args[3]

if __name__ == "__main__":


Sunday, June 14, 2009

Mysql -- get table engine type

Get table engine type

You have to go to the mysql system database 1st

This allows you to see which type of table engine you're using

Common types:

myisam: doesn't support the features listed below for innodb
innodb (default) : supports r-trees (good for spatial extensions), enforces foreign keys, and supports transactions


select table_name, engine from tables where table_schema = "vsn";

Friday, June 12, 2009

Silent install of msp patch

msiexec /p mspfile.msp /qn

Thursday, June 11, 2009

Checking versions if Office 2004 is at 11.5.5 and Acrobat is at 9.1.2

This is a trick to check version of some software on a group of computers


Here we're checking that Office 2004 11.5.5 update is installed


for ((i=1;i < 21;i++)); do if [[ $i -lt 10 ]]; then echo "station $i" ; ssh station-0${i}.local " ls -l /Applications/Microsoft\ Office\ 2004/Updater\ Logs/11.5.5\ Update\ Log.txt "; else ssh station-${i}.local " ls -l /Applications/Microsoft\ Office\ 2004/Updater\ Logs/11.5.5\ Update\ Log.txt "; fi; done

Here we're checking that Acrobat reader and acrobat are at 9.1.2


for ((i=1;i < 21;i++)); do if [[ $i -lt 10 ]]; then echo "station $i" ; ssh station-0${i}.local "system_profiler SPSoftwareDataType SPApplicationsDataType | grep '9.1.2' "; else ssh station-${i}.local " system_profiler SPSoftwareDataType SPApplicationsDataType | grep '9.1.2' "; fi; done

Wednesday, June 10, 2009

How to set up Jogre for the web

Set up jdk, apache tomcat, and hsqldb
==============================

apt-get install sun-java6-jdk
apt-get install tomcat5.5 tomcat5.5-wepapps tomcat5.5-admin
apt-get install hsqldb-server

Set up jogre
=========

wget http://voxel.dl.sourceforge.net/sourceforge/jogre/jogre_beta_0.3_bin.zip
cd /opt
unzip ~/jogre_beta_0.3.bin.zip
adduser jogre
chown -R jogre:jogre jogre
su jogre
cd
echo "export CLASSPATH=$CLASSPATH:/usr/share/java/hsqldb.jar:." >> .profile
source .profile
java org.hsqldb.Server &
cd /opt/jogre/server
chmod a+x *.sh


Configuration
==========

./server.sh &

#I prefer the gui; it's easier to just change the db settings with this tool
./administrator.sh &
#Login with admin, admin
#Change the database from xml to hsqldb
#The path to the db is jdbc:hsqldb:hsql://localhost/jogre_hsqldb

#restart the jogre server
killall server.sh
./server.sh &


#With apache tomcat admin (http://localhost:8180/admin), add jogreweb.war

Tuesday, June 9, 2009

How to start another X session

You already have X running and want to run another one.

1. Login into another virtual console
2. Open the terminal
3. startx -- :1

Monday, June 8, 2009

Tricks with the Echo command in XP

rem generate list with echo
for /l %a in (1,1,9) do echo "station-0%a"

rem Generate a file with echo to a room of computers
for /l %a in (1,1,50) do echo "station-%a" > \\station-%a\c$\myfile.txt

Printing from the command line in XP

rem Print from command line


notepad /p textfile.txt

Sunday, June 7, 2009

Java - convert byte array to String



int len = (int)c.getLength();

if (len > 0) {
int actual = 0;
int bytesread = 0 ;
byte[] data = new byte[len];
while ((bytesread != len) && (actual != -1)) {
actual = is.read(data, bytesread, len - bytesread);
bytesread += actual;

}

//assuming utf-8 encoding
String test = new String(data,0,data.length,"UTF8");
System.out.println(test);


Java - how to make an anonymous thread

Creating an anonymous thread is pretty easy. If you want to make a thread that performs a simple operation anonymous threads can be convenient.

Java threads perform their task in the run() function, which is initiated with the start method.

Here is an example:



Thread t = new Thread( new Runnable(){
public void run(){
System.out.println(" do some work");


}
});

t.start();

Saturday, June 6, 2009

SVN commands

Creating a new repository for SVN

svnadmin create /path/to/svn/newrepository


Importing

svn --username user import http://svnsite.com/path/to/svn/newrepository -m "initial import"

#note about importing
#create a local folder called newrepository and put whatever code you want to import into newrepository
#run svn from newrepository


Permission denied when importing

# Do this on the server's svn directory

sudo chown -R www-data:www-data svn

Hello world Midlet


import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;


public class LoginScreen extends MIDlet {

public LoginScreen() {
// TODO Auto-generated constructor stub
}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub

}

protected void pauseApp() {
// TODO Auto-generated method stub

}

protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub

Form f = new Form("Login Form");
Display d = Display.getDisplay(this);
d.setCurrent(f);

f.append("Hello");

}

}

Flash tricks - global vars, functions, timers

Creating global var-
_global.myvar

Creating a timer-
var starttime = getTimer();
var atime=0;

while (atime < _global.timestop+starttime){

atime = getTimer();

if (_global.conn_state == true){
break;
}
}

Creating a function-
function populateJoin(){

gamelist.data = _global.currentmsg.split(_global.SEP);
gamelist.labels = _global.currentmsg.split(_global.SEP);


}

Photoshop Tricks - alpha channels and actions

Creating an alpha channel (ps cs2)-
magic lasso outside of the object
inverse select
go to channels
click save selection as channel
Now you can save with the alpha channel


Creating an action (ps cs2)-
Click the actions tab
Click new actions and click record
Do what you want to record and press stop

Automatically start action (ps cs2)-
go to file ->scripts
click scripts events manager
check the enable events to run scripts/actions
associate your action with an event

Java Me Notes

Links

Main site

http://java.sun.com/javame/index.jsp

Download

http://java.sun.com/products/sjwtoolkit/download.html

Eclipse plugin

http://download.eclipse.org/dsdp/mtj/updates/0.9/stable/

Need to use eclipse to export jad and jar for the project (which is in deploy)


Conventions

startApp() --> like main

import javax.microedition.lcdui.*; //required to use display

Display // interface with phone screen

Form //set up the screen then attach it to display

myform.append("some text"); //a label

Friday, June 5, 2009

Setting up hsqldb

hsqldb is a java based database server

Install it:

apt-get install hsqldb-server

Create the server.properties and sqltool.rc files in your home folder (sample files should be where hsqldb was installed)

The main jar file is /usr/share/java/hsqldb.jar, which should be in your CLASSPATH when working with hsqldb

Place a link in /usr/share/tomcat5.5/common/lib if you want to use it with tomcat 5.5.

There is a swing utility to test and manage the databases.

Starting the server:

java -cp /path/to/hsqldb.jar org.hsqldb.Server


Database URL:

jdbc:hsqldb:hsql://localhost/mydb

See list of mac file servers on network

This is quick way to get a list of appletalk machines on your network when you only have access to the terminal.

atlookup

Set remote desktop from command line

reg add "\\computer\hklm\system\currentcontrolset\control\terminal server" /v "fDenyTSConnections" /d 0 /t REG_DWORD /f

0 is off and 1 is on

Wednesday, June 3, 2009

Generate a command window for each iteration of a loop

rem This is good for those commands that take a lot of time.
rem Each iteration generates a new cmd window

for /l %a in (1,1,10) do start cmd /k psexec \\computer-%a time-consuming.exe

Force a live update from command line

This will perform a silent live update of Symantec Antivirus 10.2

"c:\Program Files\Symantec AntiVirus\VPDN_LU.exe" /fUpdate /s

Adding a printer with vbscript from a network share

rem Add the connection
cscript prnmngr.vbs -ac -p \\server\printer_share


rem Set it as a default
cscript prnmngr.vbs -t -p "\\server\printer name"

rem If you don't know the name
cscript prnmngr.vbs -l

Keeping track of station numbers with a text file

rem Usually I keep track of the configurations associated with a station by its mac
rem Without the macs, one can keep track of what configuration to use by using a counter
rem
rem This works well with a network or a flash drive


for /f %%a in ('type \\server\share\count.txt') do SET temp=%%a
rem echo %temp%
SET /A temp+=1
echo %temp% > \\server\share\count.txt

rem The counts of count.txt is simply a number and can be used in a script

Adding an extension to a bunch of files

#I made a bunch of text files for Windows, but XP cannot open them without an extension
#This was a quick fix

find . -type f -exec mv {} {}.txt \;

Tuesday, June 2, 2009

Installing Apache Tomcat 5.5

#make sure you add unstable entries to sources.list
apt-get install sun-java6-jdk

apt-get install tomcat5.5
apt-get install tomcat5.5-admin
apt-get install tomcat5.5-webapps

$CATALINA_HOME/webapps/ROOT/index.jsp
/usr/share/tomcat5.5-webapps/ROOT/index.jsp



#it will be listening on port 8180

Debian package manager

#useful Debian/Ubuntu package commands

apt-get install package
apt-get remove package
apt-get update #good for updating your database after editing sources.list
apt-cache search search_string

dpkg --list
dpkg --install program.deb
dpkg --remove program




#Sometimes you have to add this to /etc/apt/sources.list
# etch is a version of Debian, replace with your version

deb http://security.debian.org/ etch/updates main contrib
deb-src http://security.debian.org/ etch/updates main contrib

deb http://ftp.debian.org/debian/ etch main
deb-src http://ftp.debian.org/debian/ etch main

#these are good for install outside packages like java

deb http://ftp.debian.org/debian/ unstable non-free
deb-src http://ftp.debian.org/debian/ unstable non-free

Power configuration from command line with xp

powercfg /setactive "always on"

powercfg /query

net time commands

net time (time from domain server)

net time /querysntp

net time /set \\another_pc

net time /setsntp:time.apple.com

Monday, June 1, 2009

Change a computer name with vbscript

Scripting the changing of computer names in windows is straight-forward with a vbscript.

You can change a computer via the registry, so this is the approach used here.

Set the computer name you want in the sNewName variable.



sNewName = "new_pc_name"

Set oShell = CreateObject ("WSCript.shell")



sCCS = "HKLM\SYSTEM\CurrentControlSet\"

sTcpipParamsRegPath = sCCS & "Services\Tcpip\Parameters\"

sCompNameRegPath = sCCS & "Control\ComputerName\"



With oShell

.RegDelete sTcpipParamsRegPath & "Hostname"

.RegDelete sTcpipParamsRegPath & "NV Hostname"



.RegWrite sCompNameRegPath & "ComputerName\ComputerName", sNewName

.RegWrite sCompNameRegPath & "ActiveComputerName\ComputerName", sNewName

.RegWrite sTcpipParamsRegPath & "Hostname", sNewName

.RegWrite sTcpipParamsRegPath & "NV Hostname", sNewName

End With ' oShell

VBScript tricks : no logo, filesystem, control blocks, argument


'When running from the console, here is how to hide the logo

cscript //Nologo myscript.vbs

'How to reference an argument
WScript.Arguments.Item(0)


'Here is how to read a file

set f = fso.opentextfile(source, 1,false)



if fso.fileexists(source) then



while not f.atendofstream



inputline = f.readline



msgbox inputline

wend

end if

Inserting a blank line with sed

#I found this to be a bit tricky

sed -e '/pattern/i\
\ # <-- this is the key
' input_file > output_file