Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

flip - newline conversion tool

export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
export IFS=$' \t\n'

# download & install flip
cd ~/Desktop
curl -L -O http://ccrma-www.stanford.edu/~craig/utility/flip/flip.osx
mv ~/Desktop/flip.osx ~/Desktop/flip
/usr/bin/sudo /bin/cp -ip ~/Desktop/flip /usr/bin
/usr/bin/sudo /usr/sbin/chown root:wheel /usr/bin/flip
/usr/bin/sudo /bin/chmod +x /usr/bin/flip
ls -l /usr/bin/flip

flip
flip -t /path/to/file     # display current file type
flip -u /path/to/file     # convert to Unix: "\n"
flip -d /path/to/file     # convert to MS-DOS/Windows: "\r\n"
flip -m /path/to/file     # convert to Macintosh (OS 9): "\r"


#----------------------------------


# create a test file

testfile="${HOME}/Desktop/testfile.txt"

function createfile() {
   testfile="${HOME}/Desktop/testfile.txt"
   /usr/bin/jot -b 'sample text' 10 | /bin/cat -n > "$testfile"
   # append a line with a '\r\n' line separator
   printf "%s\r\n\r\n\r\r\n" "sample text" >> "$testfile"      
   # append a last line without a terminating '\n'
   printf "%s" "sample text" >> "$testfile"          
   return 0
}


function odcfile() {

/usr/bin/od -A n -c < "$@" | /usr/bin/sed -E -e 's/^[[:space:]]{11}//' \
       -e s/[[:space:]]{4}/$'\001'/g \
       -e 's/[[:space:]]+//g' | \
       /usr/bin/tr -d '\n' | /usr/bin/tr '\001' ' ' | \
       /usr/bin/sed -e s/\\\\n/$'\\\\\\n\\\n'/g 

return 0
}


createfile
odcfile "$testfile"
flip -t "$testfile"     # display current file type

flip -d "$testfile"     # convert to MS-DOS/Windows: "\r\n"
flip -t "$testfile"
odcfile "$testfile"

flip -m "$testfile"     # convert to Macintosh (OS 9): "\r"
flip -t "$testfile"
odcfile "$testfile"

flip -u "$testfile"     # convert to Unix: "\n"
flip -t "$testfile"
odcfile "$testfile"

Cross-platform file encryption with TrueCrypt

# first download & install the free, open-source TrueCrypt program from http://www.truecrypt.org/downloads.php

ls -ld /Applications/TrueCrypt.app
ls -l /Applications/TrueCrypt.app/Contents/MacOS/TrueCrypt

open -a TrueCrypt

1. click "Create Volume"
2. TrueCrypt Volume Creation Wizard: select "Create a file container"
3. click Next
4. Volume Type: select "Standard TrueCrypt volume"
5. click Next
6. Volume Location: click "Select File ..."
7. navigate to ~/Desktop with the file browser, then click "New Folder"
8. enter "Name of new folder": TrueCrypt, then click Create
9. enter in "Save As": MyTrueCrypt, then click Save
10. Volume Location: the "Select File ..." field should contain /PATH/TO/Desktop/TrueCrypt/MyTrueCrypt
11. click Next
12. Encryption Options:
    Encryption Algorithm: AES
    Hash Algorithm: RIPEMD-160
13. click Next
14. Volume Size: 3 MB
15. click Next
16. Volume Password: *********************  (more than 20 characters recommended)
17. click Next
18. Format Options: Filesystem Options: Filesystem type: select FAT
19. click Next
20. Volume Format: Move your mouse as randomly as possible within this window
21. click Format
22. "The TrueCrypt volume has been successfully created."
23. click OK
24. Volume Created
25. click Exit
26. click Close (to quit TrueCrypt)

open -a TrueCrypt

1. click "Select File ..."
2. navigate with the file browser to /PATH/TO/Desktop/TrueCrypt/MyTrueCrypt
3. click Open
4. click Mount and enter your password: *********************
5. double-click: Slot 1 : Volume: /PATH/TO/Desktop/TrueCrypt/MyTrueCrypt  Size: ...

touch '/Volumes/NO NAME/file.txt'    # or copy a text file to '/Volumes/NO NAME'
echo 'This is a test!' >> "$_"
cat '/Volumes/NO NAME/file.txt'
ls -l "$_"
# hdiutil unmount '/Volumes/NO NAME'

6. click Dismount
7. click Close


# References:
# Beginner's Tutorial, http://www.truecrypt.org/docs/?s=tutorial
# http://www.truecrypt.org/faq.php
# http://blogs.oreilly.com/digitalmedia/2008/03/truecrypt-51-open-source-file.html
# http://www.askstudent.com/security/a-step-by-step-guide-on-encrypting-files-using-truecrypt/
# http://techvj.blogspot.com/2007/03/secure-email-attachments-with-truecrypt.html


#------------------------------------------------


# download & install OSXCrypt, http://www.osxcrypt.org
man ocutil
ocutil
ocutil create -help

kextstat | grep OSXCrypt
ioreg -lw 0 | grep -i osxcrypt


# Example Usage

# Create a 200M volume, FAT formatted, using Seprent-Twofish-AES
ocutil create -verbose -fat -algorithm Serpent-Twofish-AES -size 200M MyCryptedVolume.img

# The same, but using also keyfiles
ocutil create -verbose -fat -keyfiles MyKeyFile1,MyKeyFile2,MyKeyFile3 -algorithm Serpent-Twofish-AES -size 200M MyCryptedVolume.img

# The same, but only using the keyfiles
ocutil create -verbose -fat -nopass -keyfiles MyKeyFile1,MyKeyFile2,MyKeyFile3 -algorithm Serpent-Twofish-AES -size 200M MyCryptedVolume.img

# Create an hidden volume of 100M, FAT formatted, using Serpent-AES, in a previously created volume
ocutil create -verbose -hidden -fat -algorithm Serpent-AES -size 100M MyCryptedVolume.img

# If the outer volume requires keyfiles you'll have something like ...
ocutil create -verbose -hidden -fat -algorithm Serpent-AES -keyfiles MyKeyFile1,MyKeyFile2,MyKeyFile3 -size 100M MyCryptedVolume.img

# If you want to add keyfiles to the hidden volumes to an outer volume with keyfiles, your command will be something like ...
ocutil create -verbose -hidden -fat -algorithm Serpent-AES -keyfiles MyKeyFile1,MyKeyFile2,MyKeyFile3 -size 100M -hidden_keyfiles MyHiddenKeyFile MyCryptedVolume.img

# The same as before but only with keyfiles for the hidden volume
ocutil create -verbose -hidden -fat -algorithm Serpent-AES -keyfiles MyKeyFile1,MyKeyFile2,MyKeyFile3 -size 100M -hidden_nopass -hidden_keyfiles MyHiddenKeyFile MyCryptedVolume.img

# Attaching a volume
ocutil attach -verbose MyCryptedVolume.img

# If the volume requires keyfiles
ocutil attach -verbose -keyfiles MyKeyFile1,MyKeyFile2,MyKeyFile3 MyCryptedVolume.img

# The same as before but only with keyfiles
ocutil attach -verbose -nopass -keyfiles MyKeyFile1,MyKeyFile2,MyKeyFile3 MyCryptedVolume.img

# Attaching an hidden volume
ocutil attach -hidden -verbose MyCryptedVolume.img

# Attaching a volume protecting its hidden volume
ocutil attach -protect_hidden -verbose MyCryptedVolume.img

# Detaching a volume
ocutil detach -verbose MyCryptedVolume.img
#ocutil detach -verbose device-name

# Generating one or more keyfiles
ocutil genkey -verbose output1.key [output2.key ...]

Log website response times with cURL in windows

This code snippet should be saved as a batch file and run in Windows. It can be set up as a scheduled task to log response times at a fixed interval. It takes one argument, the URL, which should be enclosed in quotes or Windows will barf on URLs with = (equals) signs in. If you don't supply any arguments, you will be prompted. Binary versions of curl are available via google.

If you have ISA:
REM measure response times for a site:
@echo off
IF a%1 == a (
  SET /P varHost=Enter the address, e.g. http://google.com: 
) ELSE (
  SET varHost=%1
)
SET startTime=%date% %time%
curl.exe --proxy-ntlm --proxy yourISAproxy:8080 --proxy-user username:password -s %varHost% > fulloutput.txt
echo %startTime%,%date% %time%,%varHost% >> respTimeLog.txt


If you have no proxy:
REM measure response times for a site:
@echo off
IF a%1 == a (
  SET /P varHost=Enter the address, e.g. http://google.com: 
) ELSE (
  SET varHost=%1
)
SET startTime=%date% %time%
curl -s %varHost% > fulloutput.txt
echo %startTime%,%date% %time%,%varHost% >> respTimeLog.txt

[SAMBA] exclude file/directory from shared folder

// i have shared a directory and in this directory is one
// which i dont want to share! how can i exclude this?

[myshare]
  path = /var/test
  veto files = /var/test/private


// where /var/test/private can be a file or directory.

Disconnect Windows 2000 RDP session from command line

I use SSH to gain access to my Windows 2000 command line, so that will be reflected in the below steps.


Step #1: Connect to the server if you are not sitting at it
ssh username@win2000server -i privatekey

Step #2: Find out which sessions are currently active
query session

 SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE
>console                                     0  Conn    wdcon
 rdp-tcp                                 65536  Listen  rdpwd
 rdp-tcp#7         fakeuser                  1  Active  rdpwd

Step #3: We want to kill the connection with an ID of 1 (we can tell by the username and that it is active)
tsdiscon 1 /v

Disconnecting sessionID 1 from sessionname RDP-Tcp#7

That's it :) Easy eh?

Quick Links in Texter

Some examples of spiffy shortcuts for hyperlinks using Texter:

// hotstring = replacement text

aref  = <a href="%c">%|</a>   // a quick html link with clipboard contents, and placing the cursor between the opening/closing tags)

bbref = [url=%c]%|[/url]      // same thing for bbcode

tref = "%|":%#8221;           // same thing for textile



TxtSnippets doesn't seem to know how to format that but you get the idea.

view saved passwords to shares, in windows


view saved passwords in windows
rundll32.exe keymgr.dll, KRShowKeyMgr

Configure proxy in Windows command shell (cmd.exe)

For use with gems, cpan, etc. Inside cmd.exe, type

set HTTP_proxy=http://my.proxy.server:8000

Windows' command prompt : How to DEFINITELY change default codepage

Want to use WinLatin1 (1252) instead of DOSLatin1 (850, default when cmd.exe is started) ?

You want to apply the new codepage to :
- the current opened command prompt
C:\> chcp 1252

- all the opened command prompt in the future
Start->Run->regedit
Go to [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Nls\CodePage]
And change the "OEMCP" value to "1252"

You need to restart your computer to see the changes.

So you'll be able to display extended characters (such as accents and so on)

==

Don't know how to check the command prompt's codepage ?
C:\> chcp


==

Want to see the result in practice ?
Edit a codepage.txt file and type "à la bonne heure, ça marche ! (peut être)", then open a command prompt and check this out :
C:\> type codepage.txt

kill remote application

'probably 2k and xp only

'not my own code. Borrowed from
'http://groups.google.com/group/microsoft.public.scripting.vbscript/browse_thread/thread/f197a145c8c9f1e6

Option Explicit

Dim sComputer, sProcess, oShell
Const WindowStyle = 0
Const WaitOnReturn = True

sComputer = "XXXXXXX" ' remote machine
sProcess = "XXXXX.exe" ' app on remote machine
Set oShell= CreateObject("WScript.Shell")
oShell.Run "TaskKill /s " & strComputer & " /im " & strProcess & " /f", WindowStyle, WaitOnReturn
Set oShell = Nothing