Local

..:: Home
..:: Legal
..:: Contact
..:: About
..:: RSS
..:: Log in

Links

..:: Fake Bill Gates
..:: Tap the Hive

A Random Quote

"Quidquid latine dictum sit, altum sonatur." -- Whatever is said in Latin sounds profound.

Archive for the ‘Code’ Category

Linux Tutorial

Sunday, November 5th, 2006

This was previously a consolidation of many guides on the web including some stuff from ubuntuguide. Ubuntuguide is now a wiki, so for basic ubuntu tricks, head to there. This is now a haven for intermediate to advanced linux tutorials, although they generally favor ubuntu. Info here is licensed under the GNU GPL, the GNU FDL, and Attribution-ShareAlike 2.0 where applicable.

..:: DISCLAIMER & WARNING

  • This information is provided without any implied or expressed promises or warranty (without even the implied warranty of merchantibility or fitness for a particular purpose)
  • Some commands here may break your system
  • Some commands here may violate your country’s laws or company policy. For instance, your country may have laws that prohibit encryption usage (which is used by SSH/putty). It is your responsibility to be aware and abide by your country’s laws.
  • By using, referring to, or even acknowledging existence of this guide, you agree to hold yourself exclusively responsible for your actions, and any damages which may arise in relation to using or even not using this guide and any software mentioned here.
  • In other words IF SOMETHING BAD HAPPENS BECAUSE YOU USE THIS, IT’S YOUR FAULT, NOT MINE.

..:: Mount an iso file

mount -o loop -t iso9660 file.iso /mnt/test

..:: Mount a fat(32) partition

sudo mount /dev/your_fat_partition /media/where_you_want_to_mount -t vfat -o iocharset=utf8,umask=0000

..:: General SSH usage (adminstration and file transfer)

  • SSH is a remote terminal. To connect to a computer running the ssh server type any one of these lines
    ssh your_username@server_hostname
    ssh your_username@server_ip
    ssh server_hostname -l your_username
    ssh server_ip -l your_username
  • For example, my username is djlosch, and i want to connect to a box that i know as foo.djlosch.com and also 127.0.0.2. I can use any of these commands to connect to it.
    ssh djlosch@foo.djlosch.com
    ssh djlosch@127.0.0.2
    ssh foo.djlosch.com -l djlosch
    ssh 127.0.0.2 -l djlosch
  • In addition to providing complete terminal functionality, you can also transfer files between the remote machine and your current machine. To do retrieve a file from the remote computer use:
    scp -r username@192.168.0.1:/home/username/remotefile.txt .
  • To send a file to the remote machine use:
    scp -r localfile.txt username@192.168.0.1:/home/username/

..:: Use ssh from a Windows computer [windows]

  1. To use SSH terminal from a windows machine, you will need putty:
    1. right click this file and choose save as: http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe
    2. save it wherever you like to save your programs.
    3. type in the hostname or ip of the computer you want to connect to in the “Host Name (or IP address)” box
    4. click the SSH radio button in the “Protocol” selection.
    5. click “Open” at the bottom of the window
  2. To use SSH file transfer from a windows machine, you will need winscp:
    1. download winscp from here: http://prdownloads.sourceforge.net/winscp/winscp376.exe?download
    2. save it wherever you like to save your programs.
    3. click “New” and then type in the hostname or ip of the computer you want to connect to in the “Host Name (or IP address)” box
    4. type your username in the “User name” box, and verify that the “Port number” is set to 22.
    5. click “Login” at the bottom of the window

..:: Create a command to control your PCM volume

  1. type
    sudo wget http://www.djlosch.com/source/volmute -O /usr/bin/volmute
  2. example usage
    volmute increase #increase PCM volume
    volmute decrease #increase PCM volume
    volmute mute #mute PCM volume
    volmute unmute #unmute PCM volume
    volmute toggle #toggle muting of PCM volume
  3. use this in conjunction with your multimedia key listener daemon, or a voice control daemon to easily control your PCM volume

..:: Use gaim to connect to Gmail IM

In gaim click Tools > Accounts > Add
Use these settings:

Protocol: Jabber
Screen Name: Your_gmail_screen_name
Server: gmail.com
Resource: Gaim
Password: Your_gmail_password
Use TLS if available: checked
Force old SSL: unchecked
Allow plaintext auth over encrypted streams: unchecked
Port: 5222
Connect Server: talk.google.com
Proxy Type: Use Global Proxy Settings (unless you specifically need to change this)

Alias, Remember Password, and Auto-Login are up to you.

..:: Install a desktop session recorder that supports Beryl/Compiz, AIGLX, XGL (recordmydesktop)

  1. type
    cd
    sudo wget http://www.djlosch.com/source/gtk-recordmydesktop_0.3-1_i386.deb
    sudo wget http://www.djlosch.com/source/recordmydesktop_0.3.0-1_i386.deb
    dkpg -i recordmydesktop_0.3.0-1_i386.deb
    dkpg -i gtk-recordmydesktop_0.3-1_i386.deb
  2. run it by typing
    gtk-recordmydesktop

..:: Enable voice command and control (cvoicecontrol)

  1. type
    wget http://www.kiecza.net/daniel/linux/cvoicecontrol_0.9alpha-1_i386.deb
    sudo apt-get install libncurses
    sudo dpkg -i cvoicecontrol_0.9alpha-1_i386.deb
  2. configure your microphone by typing
    TERM=vt100
    microphone_config
  3. add commands to your voice model by typing
    model_editor
  4. run the speech listener daemon using your voice model file by typing
    cvoicecontrol your_voice_model_file

..:: Install a BitTorrent client (azureus)

  1. Azureus has been added to the extended repositories so you can now get it in apt.
    sudo apt-get install azureus

..:: Install and configure network file server (samba)

  1. type
    sudo apt-get install samba
    sudo nano /etc/samba/smb.conf
  2. edit the open file as necessary. here is an example
    [global]
    workgroup = your_workgroup
    encrypt passwords = yes
    password level = 20
    map to guest = bad user
    socket options = SO_KEEPALIVE IPTOS_LOWDELAY TCP_NODELAY
    security = user
    server string = Samba on %h
    wins support = no
    
    [homes]
    comment = Home Directories allowing R/W access to owner
    valid users = %S
    browseable = no
    read only = No
    create mask = 0640
    directory mask = 0750
    guest ok = no
    
    [archive]
    comment = Archive allowing Read Only access for all
    path = /some/path/to/folder/for/sharing
    public = yes
    writeable = no
    
    [free_serve]
    comment = Archive allowing R/W access for all
    path = /some/path/to/folder/for/sharing
    read only = No
    force user = the_username_you_want_to_own_the_file
    force group = the_groupname_you_want_to_own_the_file
    guest ok = Yes

..:: Enable encryption (SSL) for apache by creating a self signed certificate

  1. type
    sudo apt-get install apache2
    sudo cp /etc/apache2/sites-available/default /etc/apache2/default.default
    sudo /usr/sbin/apache2-ssl-certificate -days 365
    sudo apache2-ssl-certificate
    sudo a2enmod ssl
    cp /etc/apache2/sites-available/default /etc/apache2/sites-available/ssl
    ln -s /etc/apache2/sites-available/ssl /etc/apache2/sites-enabled/ssl
  2. now you need to determine where you want to put your files that will be served using ssl encryption. I put mine in /var/www-ssl/ and will use this as my example. type
    sudo mkdir /var/www-ssl
  3. now comes the trickier part. make these changes to your /etc/apache2/sites-available/default (How to edit using nano). Note that if you paste, the < and > may translate to periods, so change them back to brackets.
    NameVirtualHost *:80
    <VirtualHost *:80>
    DocumentRoot /var/www
  4. make these changes to your /etc/apache2/sites-available/ssl
    NameVirtualHost *:443
    <VirtualHost *:443>
    DocumentRoot /var/www-ssl
  5. add this to your /etc/apache2/ports.conf
    Listen 443
  6. add these to your /etc/apache2/sites-available/ssl after the part that says “Possible values include: debug, info, notice, warn, error, crit…”
    SSLEngine On
    SSLCertificateFile /etc/apache2/ssl/apache.pem
  7. type at the terminal again
    sudo /etc/init.d/apache2 restart

..:: Install E17 (enlightenment) in Ubuntu

  1. add the respective repository to /etc/apt/sources.list for your release.
    deb http://edevelop.org/pkg-e/ubuntu edgy e17
    deb-src http://edevelop.org/pkg-e/ubuntu edgy e17
  2. replace ‘edgy’ with the version name of ubuntu you’re using (dapper, feisty, gutsy, hardy, etc). then type
    sudo wget "http://lut1n.ifrance.com/repo_key.asc" -O /root/key.asc
    sudo apt-key add /root/key.asc
    sudo apt-get update
    sudo apt-get install enlightenment
  3. logout of gnome (or kde). At the GDM login menu, select your session as enlightenment, login, and explore.

..:: Disable Enlightenment’s desktop scrolling

enlightenment_remote -edge-flip-set 1

..:: Run amarok in E17

For some people, running amarok reveals the splash screen and then nada. Left click the desktop and open Lost Windows. Amarok should be there. Clicking Amarok will open a small tiny window that you can double click on to fullscreen amarok.

..:: Stop E17 from eating windows (like gaim, amarok, azureus)

E17 is set up to retake your close button. A lot of apps have changed the close button to instead minimize the app, and a lot of apps have changed the minimize button to minimize to tray. Open the settings for any app that E17 is eating windows for and disable the minimize to tray, and minimize on close functions. The applications will now minimize correctly.

..:: Enable multimedia keys (xev, xmodmap, and erme)

  1. type
    wget http://www.c7obs.net/~adi/projects/perl/erme.current
    chmod 700 erme.current
    ./erme.current &
    xev
  2. Click the window that xev pops up. Press your multimedia key to see what keycode the key has. Then make a file with keycodes corresponding to XFree named keys. This is mine for my Logitech RF Wireless iTouch that i save in ~/iTouch.conf. I have kept this tip up for archival purposes only. the iTouch has been supported out of the box since somewhere between breezy and gutsy
    keycode 144 = XF86AudioPrev
    keycode 153 = XF86AudioNext
    keycode 129 = XF86Music
    keycode 174 = XF86AudioLowerVolume
    keycode 176 = XF86AudioRaiseVolume
    keycode 160 = XF86AudioMute
    keycode 162 = XF86AudioPlay
    keycode 164 = XF86AudioStop
  3. Save this as your_keyboard.conf and type
    xmodmap whatever_you_saved_your_conf_file_as.conf
  4. I have my music control buttons set as globals in amarok, but volume will need to be set using the PCM volume script. In Erme, add a key. Set the action to ‘exec’, set the key to the name you assigned the keycode in your conf file. Then set the parameters to the function you want to assign it to. For example, my mute button is assigned as such:
    key = XF86AudioMute
    action = exec
    parameters = volmute toggle

..:: Credits

Controlling PCM Volume in Linux Via Command Line

Sunday, November 5th, 2006

Some people want a script to control PCM volume via command line. The guys over at the Gentoo forums gave me something to start with and then I put the rest of this together. You can use this in conjunction with any key or voice listener daemon to control your volume, or just use it directly at the shell.

If you want to wget it, here’s the command to do so:

sudo wget http://www.djlosch.com/source/volmute -O /usr/bin/volmute

And, here’s the script itself:

#!/bin/bash
volsetting=`amixer sget 'PCM' | grep off`
    case "$1" in
    mute)
        amixer sset 'PCM' mute
    ;;
    unmute)
        amixer sset 'PCM' unmute
    ;;
    toggle)
        if [[ x"$volsetting" = x"" ]]; then
            amixer sset ‘PCM’ mute
        else
            amixer sset ‘PCM’ unmute
        fi
    ;;
    increase)
        amixer sset ‘PCM’ 8%+
    ;;
    decrease)
        amixer sset ‘PCM’ 8%-
    ;;
    *)
        echo “This is not an acceptable command!”;
        echo -e “Use \033[01;33mmute\033[01;00;0m, \033[01;33mincrease\033[01;00;0m or \033[01;33mdecrease\033[01;00;0m as options!”;
        echo;
    esac

Once the script is in your $PATH (/usr/bin is for example), here’s the usage:

volmute increase    #increase PCM volume
volmute decrease    #increase PCM volume
volmute mute        #mute PCM volume
volmute unmute      #unmute PCM volume
volmute toggle      #toggle muting of PCM volume

Note that you’ll need to use the toggle function if you’re mapping your keyboard’s mute button, although Ubuntu now has most keyboards’ multimedia keys working out of the box.

MP3 Cue Splitter: splitall.sh

Thursday, September 28th, 2006

The purpose of this script is to take all cue files in a directory and split all mp3 files with matching names to the cue files into smaller mp3s based on the title and times of the mp3 in the cue file. since some DJ’s actually use the PERFORMER tag in cue files, the script automatically looks for the performer tag and adjusts as necessary.

For example, I download DJ GT’s latest release from generation trance. His latest release might be comprised of 3 or 4 sets. If I burn the mp3s straight to CD, I have a single 80 minute track, which sucks. If I burn the CD based on the cue file, there might happen to be a couple songs I don’t like on the set. So, I just run splitall.sh and I get some 40 mp3s to check out.

You may need to extend your repositories, but it’s in there.

sudo apt-get install mp3splt

Here’s the script:

#!/bin/bash
#############
# splitall 2006.3.21
# by david loschiavo
#
# REQUIRES: mp3split
# Put this script somewhere in $PATH like /usr/bin.
# if you run the script with no args, it will split all mp3s based
# on their matched name cues in the current directory.
# you can also pass arg1 as a directory to pull cues/mp3s and split there.
#
# assuming u put the script somewhere in $PATH, some examples are
#    splitall.sh
#             splits all cue/mp3 in the present working directory
#    splitall.sh somedir
#             splits all cue/mp3 in somedir
#
# if you do not put the script in $PATH, you will need to use:
#
#    ./splitall.sh
#             splits all cue/mp3 in the present working directory
#    ./splitall.sh somedir
#             splits all cue/mp3 in somedir
#
#############

echo "splitall.sh: v2006.3.21 by David Loschiavo "
echo "splitall.sh: read beginning of script for help"

if [[ $1 != "" ]]
then
        OLD_DIR=$PWD
        cd $1
else
        echo “splitall.sh: no args specified, executing in present working directory”
fi

has_mp3splt=`whereis mp3splt`
if [[ $has_mp3splt == "mp3splt:" ]]
then
        echo “splitall.sh: Sorry, but this script requires mp3splt, and it could not be located.”
        echo “splitall.sh: Make sure it is in $PATH or try one of these:”
        echo “splitall.sh:   Gentoo: emerge mp3splt”
        echo “splitall.sh:   Ubuntu: sudo apt-get install mp3splt”
        echo “splitall.sh:   Debian: apt-get install mp3splt”
        echo “splitall.sh:   Other: get a better distro and reformat”
        exit 0
fi

for file in *.cue
do
        if [[ $file == "*.cue" ]]
        then
                echo “splitall.sh: no cue files found, exiting”
                exit 0
        fi
        title_length=`expr ${#file} - 4`
        title=${file:0:$title_length}
        perf_count=`grep -i PERFORMER “${title}.cue” | wc -l`
        if [[ $perf_count > 1 ]]
        then
                mp3splt -o “@p - @t” -c “${title}.cue” “${title}.mp3″
        else
                mp3splt -o “@t” -c “${title}.cue” “${title}.mp3″
        fi
done

if [[ $1 != "" ]]
then
        cd $OLD_DIR
fi

echo “splitall.sh: splitting completed”
exit 0

ObjectList.java

Thursday, September 28th, 2006

There are a ton of different implementations of ArrayLinearLists and the likes. This is yet another. It supports a lot of the stack and resizing functions that PHP arrays support natively.

package djlosch;
/*
  ObjectList is an extended version of the common arraylinearlist, but without hashing.
  It supports a large amnt of functions that common ALL's have and a few other.
*/

public class ObjectList
{
  //Attributes
  private Object[] list;
  private int size;

  //Constructors
  public ObjectList()
  {
    //build an empty objl
    this.list = new Object[1];
    this.size = 0;
  }

  public ObjectList(Object[] array)
  {
    //build an objl from an array of objects
    this.list = new Object[array.length];
    if (array.length > 0)
      System.arraycopy(array, 0, this.list, 0, array.length);
    this.size = array.length;
  }

  public ObjectList(int[] array)
  {
    //build an objl from an array of ints
    this.list = new Object[array.length];
    for(int i = 0; i < array.length; i++)
    {
      this.list[i] = new Integer(array[i]);
    }
    this.size = array.length;
  }

  //Functions
  public void add(Object obj)
  {
    //add an obj to the end of an objl
    if (size == 0)
    {
      this.size = 1;
      this.list[0] = obj;
    }
    else
    {
      Object[] newList = new Object[size+1];
      System.arraycopy(list, 0, newList, 0, size);
      this.list = newList;
      this.list[size] = obj;
      this.size++;
    }
  }

  public void remove(int index)
  {
    //remove the obj from an objl at specified index
    if (this.size > 0)
    {
      Object[] theNewList = new Object[this.size];
      int offset = 0;
      for (int i = 0; i < this.size-1; i++)
      {
        if (i != index)
          theNewList[i + offset] = this.list[i];
        else
          offset = -1;
      }
      this.size–;
      if (this.size-1 > 0)
        System.arraycopy(theNewList, 0, this.list, 0, this.size-1);
    }
    else
    {
      this.size = 0;
      this.list[0] = null;
    }
  }

  public void remove(Object obj)
  {
    //remove all occurances of the specified obj from an objl. function tests
    //equality as opposed to virtual pointers
    int i = 0;
    while( i < this.size)
    {
      if (this.peek(i).equals(obj))
        this.remove(i);
      else
        i++;
    }
  }

  public void set(Object obj, int index)
  {
    //change the element at specified index to specified obj
    if ((index >= this.size) || (index < 0))
      System.out.println(”ObjectList.set => Index Invalid: ” + index);
    else
      this.list[index] = obj;
  }

  public int count(Object obj)
  {
    //return the number of occurances of specified obj
    int count = 0;
    for (int i = 0; i < this.size; i++)
    {
      if (list[i].equals(obj))
        count++;
    }
    return count;
  }

  public Object peek(int index)
  {
    //return the obj at specified index.  does not remove obj from objl
    if ((index < this.size) && (index >= 0))
      return this.list[index];
    else
      return null;
  }

  public Object peek()
  {
    //return the last obj in the objl.  does not remove obj from objl
    return peek(size-1);
  }

  public boolean contains(Object obj)
  {
    //returns whether or not an obj occurs in an objl. function tests equality
    //as opposed to virtual pointers
    boolean eval = false;
    int i = 0;
    while ((i < size) && (!eval))
    {
      if (this.list[i].equals(obj))
        eval=true;
      i++;
    }
    return eval;
  }

  public int indexOf(Object obj)
  {
    //returns the index of the first occurance of the specified obj.  returns
    //-1 if the obj is not found. function tests equality as opposed to virtual
    //pointers
    boolean eval = false;
    int i = 0;
    while ((i < size) && (!eval))
    {
      if (this.list[i].equals(obj))
        eval=true;
      i++;
    }
    if (eval == true)
      return (i-1);
    else
      return -1;
  }

  public int[] indicesOf(Object obj)
  {
    //returns an int array of all indices of occurances of the specified obj.
    //function tests equality as opposed to virtual pointers
    int[] indices = new int[this.count(obj)];
    int j = 0;
    for (int i = 0; i < this.size; i++)
    {
      if (this.list[i].equals(obj))
      {
        indices[j] = i;
        j++;
      }
    }
    return indices;
  }

  public void insert(Object obj, int index)
  {
    //inserts specified obj at specified index shifting everything after
    //specified index back 1 index
    if ((index < 0) || (index >= size-1))
      System.out.println(”ObjectList.insert: Index out of bounds: ” + index);
    else
    {
      Object[] newList = new Object[this.size+1];
      System.arraycopy(list, 0, newList, 0, index);
      System.arraycopy(list, index, newList, index+1, size-index);
      this.list = newList;
      this.list[index] = obj;
      this.size++;
    }
  }

  public void print()
  {
    //prints an empty line, each index of an objl and each corresponding value,
    //then another empty line
    System.out.println();
    for (int i = 0; i < this.size(); i++)
      System.out.println(i + “: ” + this.peek(i));
    System.out.println();
  }

  public int size()
  {
    //returns the size of the objl
    return this.size;
  }

  public void size(int index)
  {
    //sets the size of the objl.  setting a size lower than the current size
    //will truncate all additional entries.  setting a larger size than the
    //current size will fill additional entries with a null obj.
    if (index < 0)
      System.out.println(”ObjectList.setSize => Index Invalid: ” + index);
    else
    {
      String[] newList = new String[index];
      if (index < this.size)
        System.arraycopy(this.list, 0, newList, 0, index);
      else
        System.arraycopy(this.list, 0, newList, 0, this.size);
      this.list = newList;
      this.size = index;
    }
  }

  public static void main(String[] args)
  {
    //example code use
    ObjectList theList = new ObjectList();
    theList.add(”stuff”);
    theList.add(new Integer(5));
    theList.add(”more”);
    theList.add(”even more”);
    theList.insert(”ahhhhhhh”, 1);
    theList.insert(”a”, -1);
    theList.print();
    theList.size(-55);
    theList.size(-585);
    theList.insert(”a”, 0);
    theList.add(”even more”);
    theList.insert(”more”, 3);
    theList.print();
    System.out.println(theList.count(”even more”));
    theList.remove(”more”);
    theList.print();
    String[] strs = new String[5];
    strs[0] = “hey”;
    strs[1] = “hi”;
    strs[2] = “hello”;
    strs[3] = “whatup”;
    strs[4] = “homie g funk”;
    ObjectList objStr = new ObjectList(strs);
    objStr.print();
    int[] ints = new int[5];
    ints[0] = 10;
    ints[1] = 11;
    ints[2] = 12;
    ints[3] = 13;
    ints[4] = 14;
    ObjectList objInt = new ObjectList(ints);
    objInt.print();
  }
}

File Input/Output in Java: FileIO.java

Thursday, September 28th, 2006

I also wrote this little object back in undergrad to help fix up the read/writing of files.

import java.io.FileReader;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;

//creating a buffer every time you want to read or write a file can get quite
//tedious. what my class does is read a file and return the file as an
//objectlist based on linebreaks.  it's not incredible code efficiency, but
//it definitely shows benefits of oo.
public class FileIO
{
  //Attributes

  //Constructors
  public FileIO()
  {
       //basic constructor
  }

  //Functions
  public ObjectList read(String fileName)
  {
    //reads file, returning it as an ObjectList where each element is separated
    //by a newline character
    ObjectList list = new ObjectList();
    System.out.println("Reading file: " + fileName);
    try
    {
      FileReader fr = new FileReader(fileName);
      BufferedReader br = new BufferedReader(fr);
      String line;
      while ((line = br.readLine()) != null)
      {
        list.add(line);
      }
      return list;
    }
    catch (Exception e)
    {
      System.out.println("Can't read file: " + fileName);
      return null;
    }
  }

  public void write(String fileName, String toWrite)
  {
    //writes a single string to a file
    try
    {
      File outputFile = new File(fileName);
      FileWriter out = new FileWriter(outputFile);
      out.write(toWrite);
      out.close();
    }
    catch (Exception e)
    {
      System.out.println("Can't write to " + fileName + ": " + e.toString());
    }
  }

  public void write(String fileName, ObjectList toWrite)
  {
    //writes an ObjectList to a file separating each element by linebreaks
    System.out.println("Writing to " + fileName);
    try
    {
      File outputFile = new File(fileName);
      FileWriter out = new FileWriter(outputFile);

      int i = 0;
      while(toWrite.peek(i) != null)
      {
        out.write(toWrite.peek(i) + "n");
        i++;
      }
      out.close();
    }
    catch (Exception e)
    {
      System.out.println("Can't write to " + fileName + ": " + e.toString());
    }
  }

  public static void main(String[] args)
  {
    //example code use
    FileIO fileio = new FileIO();
    ObjectList list = fileio.read(”djlosch/FileIO.java”);
    fileio.write(”djlosch/FileIO.txt”, list);
  }
}