Randomness

This was copied from the Johnny Chadda blog.

=================================

 

When you are out traveling or just at work, the need sometimes arises when you have to access your computer at home. This article will show you how to access your computer in a simple and secure way. The article is focused on Ubuntu, but can just as well be used with the remote desktop features of Windows XP.

We first of all want to create a secure channel between your current computer and your machine at home, and what better tool for the job than OpenSSH. In Ubuntu, you may install the SSH server by installing the packet called “openssh-server” like so:

sudo apt-get install openssh-server

This will enable remote connections into your computer on port 22, which is the standard SSH port. If your computer sits behind a router, you must forward the port to the computer from the router.

The next step is to enable the remote desktop facilities of the Gnome desktop in Ubuntu, by opening the “System” menu, “Preferences” and then selecting “Remote desktop”. You will be presented with a dialog like the following:

gnome-vnc.png
The options are self-explanatory, and since you will be connecting to it when you yourself are not around, be sure to untick “Ask you for confirmation”. This will start the VNC server at port 5900, which you should not expose to the internet. Instead, here is where SSH comes into play.

The primary purpose of SSH is to allow secure text-based remote management capabilities to servers. It can however tunnel arbitrary traffic over it too, and VNC traffic is of course not an exception. The endpoint should be to be able to connect to port 5900 which runs the VNC server, so we need to tell SSH to create a tunnel from the current computer to the home computer and then on to the VNC server. Let’s select port 50000 for the start of the tunnel, but it can be any free port between 1024 and 65535. If you are fortunate enough to be running Linux on your work computer, connect to your home computer using the following command:

ssh -l username -L 50000:localhost:5900 your.home.pc

Let’s take a minute to analyze this command. You issue the ssh command, and -l lets you specify which user-name to connect as. -L is the magic part where we specify endpoints of our SSH tunnel. The tunnel starts at port 50000 of the local computer, then travels to the SSH server on the other side. It then looks at the endpoint which is localhost port 5900, which happens to be the VNC server! The last part is to specify a host-name or IP-address to your home computer. It can of course also be done in Windows using the popular PuTTY terminal:

putty-host.png

putty-forward.png

On the first screenshot we specify which host to connect to, and the tunnel is set using the tunnel settings. The screenshots should be self-explanatory. Just don’t forget to click the add button to actually enable the tunnel.

After logging into the server either using a Linux or Windows machine, the time has come to actually try this out by connecting a VNC client to the server. Ubuntu users have a VNC client installed under “Applications” – “Internet” – “Terminal Server Client”, and Window users may for example download UltraVNC viewer. If you don’t want to install anything, choose the “Ultr@VNC Standalone Viewer” package, which only contains a zipped viewer.

To connect, we only need to specify the start of the SSH tunnel and it should take the data to its destination automatically.

ultravnc.png

Connect to localhost port 50000 like we specified for the start of the tunnel earlier. Notice the extra colon – we need to write this way since we are not connecting to a particular display. Remember to set the quick options in UltraVNC to “MEDIUM” if you have slow connection to make the interface relatively snappy and useful.

Click connect, and if everything works you should be prompted for the password you specified when configuring the VNC server earlier.

The SSH tunnel can of course also be used to secure Windows Remote Desktop access. It uses port 3389 by default and you might not have an SSH server running on a Windows client, but perhaps you have another Linux machine on your network? In that case, you can connect to the Linux server using SSH and set the endpoint of the tunnel to your Windows client. The following is an example in the Linux SSH notation:

ssh -l username -L 50000:192.168.0.10:3389 my.home.linux.server.pc

It works just like before, but instead of just looping the connection to localhost, it will forward the connection to 192.168.0.10 port 5900 on your network, and if that happens to be your Windows client you should be able to connect.

Advertisements

Linux.com

Everything Linux and Open Source

Turn Vim into a bash IDE

June 11, 2007 (9:01:00 PM) – 4 months, 3 weeks ago By: Joe ‘Zonker’ Brockmeier

By itself, Vim is one of the best editors for shell scripting. With a little tweaking, however, you can turn Vim into a full-fledged IDE for writing scripts. You could do it yourself, or you can just install Fritz Mehner’s Bash Support plugin.

To install Bash Support, download the zip archive, copy it to your ~/.vim directory, and unzip the archive. You’ll also want to edit your ~/.vimrc to include a few personal details; open the file and add these three lines:

let g:BASH_AuthorName   = 'Your Name'
let g:BASH_Email        = 'my@email.com'
let g:BASH_Company      = 'Company Name'

These variables will be used to fill in some headers for your projects, as we’ll see below.

The Bash Support plugin works in the Vim GUI (gVim) and text mode Vim. It’s a little easier to use in the GUI, and Bash Support doesn’t implement most of its menu functions in Vim’s text mode, so you might want to stick with gVim when scripting.

When Bash Support is installed, gVim will include a new menu, appropriately titled Bash. This puts all of the Bash Support functions right at your fingertips (or mouse button, if you prefer). Let’s walk through some of the features, and see how Bash Support can make Bash scripting a breeze.

Header and comments

If you believe in using extensive comments in your scripts, and I hope you are, you’ll really enjoy using Bash Support. Bash Support provides a number of functions that make it easy to add comments to your bash scripts and programs automatically or with just a mouse click or a few keystrokes.

When you start a non-trivial script that will be used and maintained by others, it’s a good idea to include a header with basic information — the name of the script, usage, description, notes, author information, copyright, and any other info that might be useful to the next person who has to maintain the script. Bash Support makes it a breeze to provide this information. Go to Bash -> Comments -> File Header, and gVim will insert a header like this in your script:

#!/bin/bash
#===============================================================================
#
#          FILE:  test.sh
#
#         USAGE:  ./test.sh
#
#   DESCRIPTION:
#
#       OPTIONS:  ---
#  REQUIREMENTS:  ---
#          BUGS:  ---
#         NOTES:  ---
#        AUTHOR:  Joe Brockmeier, jzb@zonker.net
#       COMPANY:  Dissociated Press
#       VERSION:  1.0
#       CREATED:  05/25/2007 10:31:01 PM MDT
#      REVISION:  ---
#===============================================================================

You’ll need to fill in some of the information, but Bash Support grabs the author, company name, and email address from your ~/.vimrc, and fills in the file name and created date automatically. To make life even easier, if you start Vim or gVim with a new file that ends with an .sh extension, it will insert the header automatically.

As you’re writing your script, you might want to add comment blocks for your functions as well. To do this, go to Bash -> Comment -> Function Description to insert a block of text like this:

#===  FUNCTION  ================================================================
#          NAME:
#   DESCRIPTION:
#    PARAMETERS:
#       RETURNS:
#===============================================================================

Just fill in the relevant information and carry on coding.

The Comment menu allows you to insert other types of comments, insert the current date and time, and turn selected code into a comment, and vice versa.

Statements and snippets

Let’s say you want to add an if-else statement to your script. You could type out the statement, or you could just use Bash Support’s handy selection of pre-made statements. Go to Bash -> Statements and you’ll see a long list of pre-made statements that you can just plug in and fill in the blanks. For instance, if you want to add a while statement, you can go to Bash -> Statements -> while, and you’ll get the following:

while _; do
done

The cursor will be positioned where the underscore (_) is above. All you need to do is add the test statement and the actual code you want to run in the while statement. Sure, it’d be nice if Bash Support could do all that too, but there’s only so far an IDE can help you.

However, you can help yourself. When you do a lot of bash scripting, you might have functions or code snippets that you reuse in new scripts. Bash Support allows you to add your snippets and functions by highlighting the code you want to save, then going to Bash -> Statements -> write code snippet. When you want to grab a piece of prewritten code, go to Bash -> Statements -> read code snippet. Bash Support ships with a few included code fragments.

Another way to add snippets to the statement collection is to just place a text file with the snippet under the ~/.vim/bash-support/codesnippets directory.

Running and debugging scripts

Once you have a script ready to go, and it’s testing and debugging time. You could exit Vim, make the script executable, run it and see if it has any bugs, and then go back to Vim to edit it, but that’s tedious. Bash Support lets you stay in Vim while doing your testing.

When you’re ready to make the script executable, just choose Bash -> Run -> make script executable. To save and run the script, press Ctrl-F9, or go to Bash -> Run -> save + run script.

Bash Support also lets you call the bash debugger (bashdb) directly from within Vim. On Ubuntu, it’s not installed by default, but that’s easily remedied with apt-get install bashdb. Once it’s installed, you can debug the script you’re working on with F9 or Bash -> Run -> start debugger.

If you want a “hard copy” — a PostScript printout — of your script, you can generate one by going to Bash -> Run -> hardcopy to FILENAME.ps. This is where Bash Support comes in handy for any type of file, not just bash scripts. You can use this function within any file to generate a PostScript printout.

Bash Support has several other functions to help run and test scripts from within Vim. One useful feature is syntax checking, which you can access with Alt-F9. If you have no syntax errors, you’ll get a quick OK. If there are problems, you’ll see a small window at the bottom of the Vim screen with a list of syntax errors. From that window you can highlight the error and press Enter, and you’ll be taken to the line with the error.

Put away the reference book…

Don’t you hate it when you need to include a regular expression or a test in a script, but can’t quite remember the syntax? That’s no problem when you’re using Bash Support, because you have Regex and Tests menus with all you’ll need. For example, if you need to verify that a file exists and is owned by the correct user ID (UID), go to Bash -> Tests -> file exists and is owned by the effective UID. Bash Support will insert the appropriate test ([ -O _]) with your cursor in the spot where you have to fill in the file name.

To build regular expressions quickly, go to the Bash menu, select Regex, then pick the appropriate expression from the list. It’s fairly useful when you can’t remember exactly how to express “zero or one” or other regular expressions.

Bash Support also includes menus for environment variables, bash builtins, shell options, and a lot more.

Hotkey support

Vim users can access many of Bash Support’s features using hotkeys. While not as simple as clicking the menu, the hotkeys do follow a logical scheme that makes them easy to remember. For example, all of the comment functions are accessed with \c, so if you want to insert a file header, you use \ch; if you want a date inserted, type \cd; and for a line end comment, use \cl.

Statements can be accessed with \a. Use \ac for a case statement, \aie for an “if then else” statement, \af for a “for in…” statement, and so on. Note that the online docs are incorrect here, and indicate that statements begin with \s, but Bash Support ships with a PDF reference card (under .vim/bash-support/doc/bash-hot-keys.pdf) that gets it right.

Run commands are accessed with \r. For example, to save the file and run a script, use \rr; to make a script executable, use \re; and to start the debugger, type \rd. I won’t try to detail all of the shortcuts, but you can pull up a reference using :help bashsupport-usage-vim when in Vim, or use the PDF. The full Bash Support reference is available within Vim by running :help bashsupport, or you can read it online.

Of course, we’ve covered only a small part of Bash Support’s functionality. The next time you need to whip up a shell script, try it using Vim with Bash Support. This plugin makes scripting in bash a lot easier.

Every Monday we highlight a different extension, plugin, or add-on. Write an article of less than 1,000 words telling us about one that you use and how it makes your work easier, along with tips for getting the most out of it. If we publish it, we’ll pay you 0. (Send us a query first to be sure we haven’t already published a story on your chosen topic recently or have one in hand.)

<!– Ad block?? –>

Read in the original layout at: http://www.linux.com/articles/114359

Orginal post can be found here;

Lorenzo Ferrara’s Blog

==============================================

VMware Player is the new free (as in beer) great tool released a few days ago by VMware.
It can be used to run virtual machines made with VMware Workstation, VMware GSX Server, and VMware ESX Server but it can’t create them.

The 24th October Eliot Phillips wrote about how to create virtual machines with VMware Player.

There is also another, maybe more simple, way to create virtual machines using VMware Player.The problem is to understand how the vmx file works. A vmx file is the configuration file used by the VMware Player to launchvirtual machines. This article explains how to create a vmx configuration file, a hard disk image and how to install a GNU/Linux OS using VMware Player. At the end of the article you can find a tar.gz file containing a complete working configuration with clean hd images ready to be used.

To create an hard disk image you need qemu-img for GNU/Linux or qemu-img.exe for Windows. Just run:

qemu-img create -f vmdk hda.vmdk 100M

to create a file called hda.vmdk of 100 Megs. If you want, say a 5 Gigs hard disk, just use

qemu-img create -f vmdk hda.vmdk 5G

Next thing you need is a new configuration file so VMware Player can install your new GNU/Linux distribuntion on the hard disk image. The file can be created using a normal text editor. This is a sample of a working configuration file:

------------------------------------------------------------------------------
## VMware Player Configuration File

## This indicates the version of the virtual machine.
## Compatible VMs are the ones created by Workstation 4.x and 5.x, GSX Server
## 3.x, and ACE 1.x.
## Valid entires are 7 and 8. We will use 8.
config.version = "8"

## This seems to indicate the version of the VMware hardware.  Valid values
## are from 0 to 4. But to set value 4 you need to make the configuration
## file not writable because VMware Player will change it automaticaly to 3.
virtualHW.version = "3"

## This will specify how much ram will be used by VMware Player.
memsize = "128"

## This variables are used to configure the primary master ide channel.
## You must assign to ide0:0.fileName the name of the hard disk image created
## with qemu-img. ide0:0.present = "TRUE" means that the device shoul be
## connected on system startup.
ide0:0.present = "TRUE"
ide0:0.fileName = "hda.vmdk"
ide0:0.deviceType = "disk"
ide0:0.redo = ""

## No configuration for the primary slave ide channel.
## ide0:1.present = "TRUE"
## Configure the secondary master ide channel.
## This time you want to configure a cdrom.
## This configuration uses the cdrom.iso file as cdrom drive.
## If you want to use your phisical cdrom drive set
##      ide1:0.filename = "auto detect"
##      ide1:0.deviceType = "cdrom-raw"
ide1:0.present = "TRUE"
ide1:0.fileName = "cdrom.iso"
ide1:0.deviceType = "cdrom-image"
ide1:0.autodetect = "TRUE"

## No configuration for the secondary slave ide channel
## ide1:1.present = "TRUE"

## This section configures the floppy drive.
##  change "/dev/fd0" to "A:" if you run VMware Player on Windows.
## You could also use an image of a floppy disk.
floppy0.present = "FALSE"
floppy0.fileName = "/dev/fd0"

## Ethernet configuration.
## Activate on startup and set a nat connection.
ethernet0.present = "TRUE"
ethernet0.connectionType = "nat"
ethernet0.addressType = "generated"
ethernet0.generatedAddress = "00:0c:29:7f:26:09"
ethernet0.generatedAddressOffset = "0"

## Usb configuration.
## Activate usb on system startup.
usb.present = "TRUE"

## This configuration makes VMware Player emulate an es1371 sound card.
sound.present = "TRUE"
sound.virtualDev = "es1371"

## This is just to set tile of the VMware Player window.
displayName = "Virtual OS"

## Here you need to specify the guest os, that is the os that
## VMware Player  will run. Possible choices are:
##  "dos"
##  "freebsd" ............ generic freebsd distribution
##  "netware6" ........... netware 6
##  "other24xlinux" ...... generic distribution with the linux 2.4.x kernel
##  "other26xlinux ....... generic distribution with the linux 2.6.x kernel
##  "otherlinux" ......... generic distribution with the linux kernel
##  "redhat" ............. red hat linux (fedora?)
##  "solaris10" .......... solaris 10
##  "suse" ............... suse linux
##  "win95" .............. windows 95
##  "win98" .............. windows 98
##  "winme" .............. windows me
##  "winnt" .............. windows nt
##  "winnetstandard" ..... windows server 2003 standard edition
##  "winxphome" .......... windows xp home
##  "winxppro" ........... windows xp professional
guestOS = "otherlinux"

## Set the Non Volatile Random Access Memory file which contains information
## about BIOS settings.
nvram = "bios.nvram"
-------------------------------------------------------------------------------

Using this file as template you can install any operating system.

If you want a fully functional configuration, just download virtual_machine.tar.gz. The file contains a working configuration and some blank hard disk images created with qemu-img. The “hd/” directory containsthe hard disk images. Each image has different size. Just copy the desired image in the “../hda.vmdk” file.Also, the configuration reads cdrom.iso file as input for the cdrom drive, so, if you want to install your favorite distribution,rename the .iso file in cdrom and place it in the directory containing the VMware Player configuration file. To create aniso image from a cdrom/dvdrom just run

dd if=/dev/cdrom of=cdrom.iso

where /dev/cdrom is you cdrom/dvd reader device. Then launch

vmplayer start_linux.vmx

and install your GNU/Linux distibution.

LINKS:
VMware Player
QEMU for Linux (contains qemu-img)
QEMU for Windows (contains qemu-img.exe)

Original script page;

Asterisk music on hold

This script converts a directory full of MP3s to another directory with the same files in native formats (WAV, GSM and U-Law) – see recipe Music-on-hold without MPG123 for an overview of how this works.

FROMDIR="/root/mp3s"
TODIR="/root/transcodes"
for i in $FROMDIR/*.mp3
do
BASEFILE=$(basename $i .mp3)
echo Converting $BASEFILE
mpg123 -s --rate 44100 --mono $FROMDIR/$BASEFILE.mp3 > $TODIR/$BASEFILE.raw
sox -r 44100 -w -s -c 1 $TODIR/$BASEFILE.raw -r 8000 -c 1 $TODIR/$BASEFILE.wav
sox $TODIR/$BASEFILE.wav -t gsm -r 8000 -b -c 1 $TODIR/$BASEFILE.gsm
sox $TODIR/$BASEFILE.wav -t ul -r 8000 -b -c 1 $TODIR/$BASEFILE.pcm
rm -f $TODIR/$BASEFILE.raw
done

Please set FROMDIR and TODIR as you best see fit (they can also be the same directory).

===============================================

http://astrecipes.net/index.php?from=153&q=astrecipes/music-on-hold+without+mpg123

When playing the music on hold, Asterisk will usually decode MP3 files using the external utility MPG123. This setup is less than perfect, as:
star MPG123 is run as an external process, so it must be launched once for every music class
star MPG123 will sometimes complain of the MP3 data used
star MPG123 will sometimes not loop the sound over, so the caller ends up listening to silence
star MPG123 will sometimes start eating up a lot of CPU with no apparent reason
star Live MP3 decoding is, in any case, an expensive, CPU-hungry activity

In this recipe, we’ll show how to convert the MP3 files to a native WAV format and then use that as a source of sound. The resulting file is surely larger, buy the few megabytes of disk lost are usually well paid-for in Asterisk well-being.

First of all, create a directory for your new music on hold; we may create it as /var/lib/asterisk/moh-default. All files in this directory will be played to the caller, one after the other. Of course, you can also have only one file with your music on hold if you have to.

The following commands will translate the standard file fpm-sunshine.mp3 to a correctly formatted WAV file:

mpg123 -s --rate 44100 --mono /src/mp3/fpm-sunshine.mp3 > fpm-sunshine.raw
sox -r 44100 -w -s -c 1 fpm-sunshine.raw -r 8000 -c 1 fpm-sunshine.wav
rm fpm-sunshine.raw

Repeat as you prefer for all the MP3 files you want to include.

You then have to edit the musiconhold.conf file in order to tell Asterisk to play those files instead of the standard MP3’s:

[default]
mode=files
directory=/var/lib/asterisk/moh-default
random=yes

Create an entry for each of the music classes you want to play (the one above will substitute the default one).

Restart Asterisk and make sure no more MPG123’s are running.

From the Asterisk shell, use the moh classes show command to see which music classes are currently defined and moh files show to see the actual files being played by Asterisk.

If you have a number of files that you want to convert all in one pass, this script to convert music-on-hold to native formats may prove handy.

Further avoiding transcoding
When Asterisk plays back native MOH (as we are doing with this recipe), it checks the sound file by extension and tries to find the one with the minimum cost compared to the current in-use codec, just like it usually does for sound files.
This means that if you have fpm-sunshine.wav and fpm-sunshine.gsm in your moh-default directory, and your telephone is using the GSM codec, Asterisk will try to use the .gsm file in order to avoid any transcoding at all.
While transcoding from SLIN format (the one we used in the WAV file above) is usually the cheapest, if we know that our system uses GSM or U-Law very often, it could be a good idea to transcode to GSM and U-Law too; this is easily obtained with a couple of Sox calls:

sox fpm-sunshine.wav -t gsm -r 8000 -b -c 1 fpm-sunshine.gsm
sox fpm-sunshine.wav -t ul -r 8000 -b -c 1 fpm-sunshine.pcm

The first line transcodes from SLIN to GSM, the second from SLIN to ULaw. This helps keeping the system load to a minimum (playing music-on-hold accounts for a non-trivial portion of most PBXs live activity!)

Most howto’s and screencasts about Ruby on Rails deployment using Capistrano are a little confusing now that 2.0 has come out.

Build a Full Rails Server on Ubuntu

To run the old version

cap _1.4.1_ <command>

More to come on this….

A video I found to help set up Samba.

This is partially reprinted from the Ubuntu community documentaion
https://help.ubuntu.com/community/InstallingANewHardDrive

Format the Partition via Command Line

To format the disk as ext3 filesystem (best for use under Ubuntu):

sudo mke2fs -j /dev/hdd1

Substitute “/dev/hdd1” with your own drive’s path.

====================================

Modify reserved space (optional)

When formatting the drive, 5% of the drive’s total space is reserved for the super-user (Root) so that the operating system can still write to the disk even if it is full. However, for disks that only contain data, this is not necessary.

You can ajust the percentage of reserved space with the “tune2fs” command, like this:

sudo tune2fs -m 1 /dev/hdd1

This example reserves 1% of space – change this number if you wish.

Using this command does not change any existing data on the drive. You can use it on a drive which already contains data.

=====================================

Mount the Drive

You can choose to have the drive mounted automatically each time you boot the computer, or manually only when you need to use it.

Automatic Mount at Boot

You’ll need to edit /etc/fstab:

gksudo gedit /etc/fstab

Add this line to the end:

/dev/hdd1 /media/mynewdrive ext3 defaults 0 2

The “2” at the end instructs your system to run a quick file system check on the hard drive at every boot. Changing it to “0” will skip this. Run ‘man fstab’ for more info here.
You can now run “sudo mount -a” (or reboot the computer) to have the changes take effect.

If you want to allow a normal user to create files on this drive, you can either give this user ownership of the top directory of the drive filesystem: (replace USERNAME with the username)

sudo chown -R USERNAME:USERNAME /media/mynewdrive

or in a more flexible way, practical if you have several users, allow for instance the users in the plugdev group (usually those who are meant to be able to mount removable disks, desktop users) to create files and sub-directories on the disk:

sudo chgrp plugdev /media/mynewdrive
sudo chmod g+w /media/mynewdrive
sudo chmod +t /media/mynewdrive

The last “chmod +t” adds the sticky bit, so that people can only delete their own files and sub-directories in a directory, even if they have write permissions to it (see man chmod).

Manually Mount

Alternatively, you may want to manually mount the drive every time you need it.

For manual mounting, use the following command:

sudo mount /dev/hdd1 /media/mynewdrive

When you are finished with the drive, you can unmount it using:

sudo umount /media/mynewdrive