Archive for the ‘Documentation’ Category

Redirect certain subnets to a different site in Apache.

I did this from the httpd.conf where I have all my virtual directories defined. I just added the following within one of those virtual directory definitions and reloaded httpd. This will redirect anybody on the 192.168.0.0/24 subnet to http://google.com, and the others will proceed to the directory (/my/website/directory).


RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} !^192.168.0.*
RewriteRule .* http://google.com [R=302,L]

In an .htaccess file you would just need to add the following:

RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} !^192.168.0.*
RewriteRule .* http://google.com [R=302,L]

If you need to redirect the cgi-bin, elsewhere:


AllowOverride All
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} !^192.168.0.*
RewriteCond %{REMOTE_HOST} !^172.16.0.*
RewriteCond %{REMOTE_HOST} !^10.0.0.*
RewriteRule .* http://www.google.com [R=302,L]
Options ExecCGI
Options FollowSymLinks
Order allow,deny
Allow from all

Sending email from PHP using mail().

To send text email from PHP:

tomailaddress‘;
$from = ‘frommailaddress‘;
$subject = ‘Here is my subject.’;
$message = ‘Some text in the body of the message.’;

$headers .= ‘X-Priority:1 (Highest)’ . “\r\n”;
$headers .= ‘From: ‘ . $from . “\r\n”;
mail( $to, $subject, $message, $headers) or print ‘Could not send mail’;
?>

To send HTML email from PHP:

tomailaddress‘;
$from = ‘frommailaddress‘;
$subject = ‘Here is my subject.’;
$message = ‘
<HTML>
<BODY>
<B>
Some bold text
</B>
<P>
Some other HTML stuff.
</BODY>
</HTML>
‘;

$headers = ‘MIME-Version: 1.0’ . “\r\n”;
$headers .= ‘Content-type: text/html; charset=iso-8859-1’ . “\r\n”;
$headers .= ‘X-Priority:1 (Highest)’ . “\r\n”;
$headers .= ‘From: ‘ . $from . “\r\n”;
mail( $to, $subject, $message, $headers) or print ‘Could not send mail’;
?>

Windows and RDP key mappings

The Windows key has become much more important now that Windows 8 has been released. For someone who primarily uses Windows via RDP, it can make things a bit more difficult if you don’t know the some key combinations. I have listed a few that I commonly use below.

Windows Key: Alt-Home

Ctrl-Alt-Del: Ctrl-Alt-End

Alt-Tab: Alt+Page Up

Another interesting thing I found that has annoyed me in Windows 8 when using RDP is how to close metro applications. You can do this using a mouse. When in the metro application, move the mouse to the very top of the screen until the mouse pointer turns into a hand. Then, click and pull the window down to the bottom of the screen. This definitely beats what I was going before. I using Task Manager to kill the application.

Mutt – delete messages by date range

Finally got fed up when faced with how to delete 9,000 of 10,000+ email message using mutt in linux. The key is to get the messages tagged. To tag messages, hit “T” (capital T). Then, you will be presented a “Tag messages matching:” prompt.

Tag the messages by date:

Tag messages matching: ~d dd/mm/yyyydd/mm/yyyy

For example:

Tag messages matching: ~d 01/01/2012-31/12/2012

Tagged messages will now have an “*” (asterik).

Delete all the tagged messages:
Hit the “;” (semi-colon). You will have a “tag-” prompt. Then, just hit “d”.

Configure Windows 2012 server to use NTP server.

I used the following procedure to configure my Windows 2012 server to use NTP running on my linux server.

Configure the Window Time service to use the NTP server:

PS > w32tm /config /manualpeerlist:NameOrIPAddress /syncfromflags:MANUAL

Example:

PS > w32tm /config /manualpeerlist:192.168.0.1 /syncfromflags:MANUAL

Stop the Windows Time service:

PS > Stop-Service W32Time

Start the Windows Time service:

PS > Start-Service W32Time

Query the Windows Time service:

PS > w32tm /query /status

How to use minicom to access Cisco equipment console

This is another one of those things that I do not do very often. And when it comes up, I have re-invent the wheel. No more. I will have here.

This was done from a CentOS 6.x machine connected via a Cisco console cable to a 35xx switch. This configuration has worked for any Cisco equipment I have worked with as well.

If minicom is not installed:

# yum install minicom

Setup minicom.

# minicom -s

Configure the serial port by selecting “Serial port setup” from the menu.

Select the serial port using A:
A (ttyS0)

Configure the port connection by selecting E:
E
C (9600)
V (Bits)
L (Parity)
W (Stopbits)

9600 8N1

Toggle the hardware flow control:
Hardware Flow Control (off):
F

This will save the configuration as the default configuration:

Save setup as dfl

Ensure that user accounts that need to access the console are added to the dialout group.

# vigr

dialout:x:18:jim

Verify your terminal emulation. Mine was set to xterm, and none of the menuing worked. Obviously, this would need to be done in the user environment.

# export TERM=vt100

Another useful setting is the toggle to enable logging. This is very useful, especially when messing a router or switch configuration.

From the menu (CTRL-A Z), enable capture using L.

Recover a Cisco switch password

Used this procedure to get back into a Cisco 35xx switch that I forgot the enable password.

Unplug switch.
Push and hold the mode button on the front of the switch, and plug the switch in.

At the switch prompt, enter flash_init

switch: flash_init
Initializing Flash…
flashfs[0]: 456 files, 6 directories
flashfs[0]: 0 orphaned files, 0 orphaned directories
flashfs[0]: Total bytes: 32514048
flashfs[0]: Bytes used: 9057280
flashfs[0]: Bytes available: 23456768
flashfs[0]: flashfs fsck took 13 seconds.
…done Initializing Flash.
Boot Sector Filesystem (bs) installed, fsid: 3
Setting console baud rate to 9600…

Once back at the switch prompt, enter load_helper.

switch: load_helper

Temporarily, rename the current config.

switch: rename flash:config.text flash:config.backup

switch: boot

Would you like to terminate autoinstall? [yes]: yes

— System Configuration Dialog —

Would you like to enter the initial configuration dialog? [yes/no]: no

Enable with no pasword:

Switch>enable
Switch#

Rename the config renamed earlier:

Switch#rename flash:config.backup flash:config.text
Destination filename [config.text]?

Now, copy it to be your running config:

Switch#copy flash:config.text system:running-config
Destination filename [running-config]?

Fix your mistake:

Zswitch(config)#no enable secret
Zswitch(config)#enable password mysupersecretpassword

Copy the updated running config to be the startup:

Zswitch(config)#exit
Zswitch#copy running-config startup-config
Destination filename [startup-config]?

I reloaded the switch to verify I could get in now.

Zswitch#reload
Proceed with reload? [confirm]

Proxy web traffic through another server using ssh.

Totally swiped this from a Linux Journal podcast, but it was so good I wanted to write it down for future reference:

# ssh -N -D someportnumber username@somesshserver

Then, enter your password and configure your browser to use SOCKS host version 4 or 5 with localhost and the port you used.

For example:

# ssh -N -D 1080 jim@11.22.33.44

Browser configuration:

ProxySettings

Convert UTF-16 file to UTF-8

A lot of times, I find myself creating output files in Windows that I want to use linux to parse because I am more comfortable scripting in linux than in Windows. Most times I the output UTF-16 encoded. You can tell by using the file command. I used this to convert a file generated in Windows 2003 server, so that I could work with it in CentOS 6.x.

$ file myfilename.txt
myfilename.txt: Little-endian UTF-16 Unicode text, with CRLF line terminators

Then, to convert the file:

$ iconv -f utf-16 -t utf-8 myfilename.txt > mynewfilename.txt

Delete or mark as read all Google Voice messages.

What led me down this path? Well, for some reason the geniuses at Google decided to make it very difficult to delete or select all of your messages. Using a browser, you are only able to select 10 at a time. On a mobile device, using an application, it is even more painful. I did some digging around and found pygooglevoice. It allows you to utilize the Google Voice API via Python. Most these will only operate on 10 messages at a time, however that is easily rectified with multiple calls or some better python programming than I know.

You need to have python installed to execute these scripts. I used these on a CentOS 6 system.

Software: http://code.google.com/p/pygooglevoice/

Installation/Configuration:

# yum install python python-setuptools
# easy_install simplejson
# easy_install -U pygooglevoice
# cd /usr/lib/python2.6/site-packages/pygooglevoice-0.5-py2.6.egg/googlevoice
# vi settings.py

# JGZ – Updated login URL since default did not work
#LOGIN = ‘https://www.google.com/accounts/ServiceLoginAuth?service=grandcentral’
LOGIN = ‘https://accounts.google.com/ServiceLogin?service=grandcentral’

Script samples to use the module:

Delete all read messages from your inbox:

from googlevoice import Voice

voice = Voice()
voice.login(‘GOOGLEVOICELOGIN’, ‘PASSWORD’)

for message in voice.inbox().messages:
if message.isRead:
message.delete()

Delete all read messages from your trash:

from googlevoice import Voice

voice = Voice()
voice.login(‘GOOGLEVOICELOGIN’, ‘PASSWORD’)

for message in voice.trash().messages:
if message.isRead:
message.delete()

Mark all unread messages as read:

from googlevoice import Voice,util

voice = Voice()
voice.login(‘GOOGLEVOICELOGIN’, ‘PASSWORD’)

while True :
folder = voice.search(‘is:unread’)
if folder.totalSize <= 0 : break util.print_(folder.totalSize) for message in folder.messages: util.print_(message) message.delete(1)

Script sample usage:

# python gvscriptname

Return top

INFORMATION