May 23 2007

Linux quick recipes

Tag:tepezcuintle @ 18:43

I sometimes need quick reminders for commands that I type on the Linux console. Some weeks go by where I don’t have to run any of these commands so it is always good to have them listed here in case I need a refresher.

1.-Problem: You are getting too many remote tcp connections and you want to know
what IP address might causing a DOS attack:

you can cut and paste this string.

netstat -anp | grep "tcp\|udp" | awk '{ print $5 }' | cut -d: -f1 | sort | uniq -c | sort -n

now you can firewall those ip addresses using
this little script.

create a little script

pico -w /sbin/block.sh

then paste this .

#!/bin/sh
### Block ip addresses ####

/sbin/iptables -I INPUT -s $1 -j DROP

then save the file and chmod 700 /sbin/block.sh

After you save the file you can just type

/sbin/block.sh Type_IP_Address_here

example

/sbin/block.sh 61.61.233.134

now if you run iptables –list

[root@penguincares root]# iptables –list
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP all — 61-61-233-134.cm.ubbn.net anywhere
acct_int all — anywhere anywhere
acct_ext all — anywhere anywhere

As you can see I have blocked the above ip using that little script.


May 15 2007

A few mysql tips. Restore from dump

Tag:tepezcuintle @ 21:37

How do I restore a MySQL database from an sql dump?

If your MySQL user has a password:

mysql -h localhost -u [MySQL user, e.g. root] -p[database password] [name of the database] < [name of your sql dump, e.g. sqldump.sql]

Please note: there's no space between -p and the password!

If there's no password:

mysql -h localhost -u [MySQL user, e.g. root] [name of the database] < [name of your sql dump, e.g. sqldump.sql]

How do I make my MySQL server use TCP connections instead of Unix sockets?

Comment out the option skip-networking in the file my.cnf which typically lies under /etc or /etc/mysql. Then restart your MySQL server:

/etc/rc.d/init.d/mysql restart

or

/etc/init.d/mysql restart

With

netstat -ta

you can check if MySQL is reachable via TCP.

How do I make an sql dump of a MySQL database on the command line?

If your MySQL user has a password:

mysqldump -h localhost -u [MySQL user, e.g. root] -p[database password] -c --add-drop-table --add-locks --all --quick --lock-tables [name of the database] > sqldump.sql

Please note: there’s no space between -p and the password!

If there’s no password:

mysqldump -h localhost -u [MySQL user, e.g. root] -c –add-drop-table –add-locks –all –quick –lock-tables [name of the database] > sqldump.sql


May 15 2007

Useful Linux commands

Tag:tepezcuintle @ 21:19

Useful commands for the Linux command line

This short guide shows some important commands for your daily work on the Linux command line.

arch

Outputs the processor architecture.

$ arch

i686

cat

Outputs the contents of a file.

$ cat lorem.txt

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Continua”Useful Linux commands”


May 15 2007

Things to do when your server refuses to work.

Tag:tepezcuintle @ 16:29
Table of contents

Continua”Things to do when your server refuses to work.”


May 15 2007

How to transfer home directory . Penguincares brains moved to a new drive

Tag:tepezcuintle @ 16:13

HOW TO TRANFERED HOME TO A NEW DRIVE.

Ok so last night I decided to finally upgrade my Penguincares server.

I had a 40 gig hard drive that contained all the partitions of my server
I also had a backup drive which was installed inside the machine and it was
a small drive 6 gigs.

Basically I had to remove the small drive and then install an 80 gig hard drive
which I was going to use to move the /home directory. the home directory is where
I keep all the most important files on my server.

Continua”How to transfer home directory . Penguincares brains moved to a new drive”


May 10 2007

How to redirect output and errors to /dev/null?

Tag:tepezcuintle @ 15:47

How to redirect output and errors to /dev/null?

I have a few scripts which I’m never interested in the output from: They run in a cron job, and if they fail every once in a while, I’m not too interested in the output.

When you run said scripts or programs from the command line, the standard remedy is to add >/dev/null to redirect output to the bitbucket instead.

However, when I run these from a cron job, any output that the script would send to stderr is sent as a mail to the owner of the script, which can be a tad annoying if the script runs regularily, with output you don’t need or want, or can do anything about.

The graceful way to handle this in any shell is as follows: Redirect the output of stderr to stdout, and then redirect this combined output to /dev/null:

./example.pl > /dev/null 2>&1 What happens here is as follows:

We send standard output to /dev/null, using > /dev/null.
2>&1 - ensures that you send the standard error (file descriptor 2), to wherever standard output (file descriptor 1) is going, which is, as already established, to the bitbucket.
Note that this is not an actual question that has showed up in my server logs — rather, this is something that I tend to forget between every time I need it, and so I have to spend time looking for it. The answers to this were found in Unix Power Tools, 3rd Edition

here is nohup perfect for java applications.

nohup java -jar foo.jar >/dev/null 2>>/dev/null &


May 05 2007

Sed More Tips

Tag:tepezcuintle @ 22:32

sed tutorial
• sed introduction
• sed examples #1 • sed commands
• regular expressions
• more sed examples
intro home - top of the page - email

sed - Stream EDitor - works as a filter processing input line by line.
Sed reads one line at a time, chops off the terminating newline, puts what is left into the pattern space (buffer) where the sed script can process it (often using regular expressions), then if there anything to print - sed appends a newline and prints out the result (usually to stdout).
• www.engin.umich.edu/htbin/mangate?manpage=sed - sed

There are many tutorials and FAQs - search google for sed tutorial or sed faq

- www.faqs.org/faqs/editor-faq/sed/ - FAQ (also www.ptug.org/sed/sedfaq.html )
- www.dreamwvr.com/sed-info/sed-faq.html - FAQ
- http://spacsun.rice.edu/FAQ/sed.html - short 1-page intro - examples of usage
- http://spazioweb.inwind.it/seders/tutorials/ - list of links to sed tutorials
- http://spazioweb.inwind.it/seders/tutorials/sedtut_4.txt -original manual (1978 by Lee E. McMahon with the classic “Kubla Khan” example)
- www.math.fu-berlin.de/~leitner/sed/tutorial.html - tutorial by Felix von Leitner

Print out and read the following: - www.dbnet.ece.ntua.gr/~george/sed/sedtut_1.html - good tutorial by Carlos Duarte
- www.dbnet.ece.ntua.gr/~george/sed/1liners.html - one-liners (compiled by Eric Pement) (see also www.cornerstonemag.com/sed/sed1line.txt )
Books:
Sed & Awk, 2d edition, by Dale Dougherty & Arnold Robbins (O’Reilly, 1997)
Mastering Regular Expressions, by Jeffrey E. F. Friedl (O’Reilly, 1997)

Several more sites: - Yao-Jen Chang - Sven Guckes - Felix von Leitner - Yiorgos Adamopoulos - Eric Pement

sed examples #1 home - top of the page - email

Simple commands = pattern + action. If no pattern is given, the action is applied to all lines, otherwise it is applied only to lines matching the pattern. Regular expressions are simimlar to those in Perl. Here is a typical example of usage:
Example: >cat file
I have three dogs and two cats
>sed -e ’s/dog/cat/g’ -e ’s/cat/elephant/g’ file
I have three elephants and two elephants
>
Continua”Sed More Tips”


May 04 2007

Bash The Guide

Tag:tepezcuintle @ 4:30

Quoting in bash

Separators ; & ( ) | ^ < > new-line space tab
Single quotes Within single quotes all characters are quoted, including the backslash. The result is one word
Double quotes Variables are substituted but there is no file name expansion (* and ? are quoted). The result is one word.
Back Quotes Variables are substituted and there is file name expansion (* and ? are expanded). The result is one word resulting from the execution of the command(s) within quotes.

Continua”Bash The Guide”


May 03 2007

Grep for Linguistics

Tag:tepezcuintle @ 16:17


GREP for Linguists

GREP FOR LINGUISTS

Stuart Robinson


1.0 Introduction

1.1 Text: Characters, Strings, and Words… Oh My!

2.0 Getting Started

2.1 Delimiting “Words”: Space Marks vs. Metsequences

2.2 “Or”: Disjunction with Parentheses and Vertical Bars

2.3 Pick A Character, But Not Just Any Character: Square Brackets

2.4 Making Literals out of Metacharacters: The Backslash (Escape
Characte)

2.5 Negation: The Caret in Square Brackets

2.6 Line Starts and Ends: Carets and Dollars

2.7 Summary

3.0 Quantifiers

3.1 Match Many Times or Not at All: The Asterisk

3.2 Match One Time or Not at All

3.3 Match One or More Times: The Plus Mark

3.4 Getting More Specific: Minimums and Maximums

3.5 Summary of Quantifiers

4.0 Some Useful POSIX Conventions

5.0 Conclusion and Summary

Continua”Grep for Linguistics”


May 01 2007

MySQL Cheat Sheet

Tag:tepezcuintle @ 16:14

MySQL - Cheat Sheet

MySQL is a small, fast and highly configurable DBMS.
It supports a number of different table fileformats, depending on the requirements of the user.

These are the main MySQL clients and processes (mysqld):

mysqld - MySQL server daemon
safe_mysqld - Server process monitor
mysqlaccess - Tool for creating MySQL users
mysqladmin - Utility for administering MySQL
mysqldump - Tool for dumping the contents of a MySQL database
mysql - Command line interface to MySQL
mysqlshow - List all MySQL database

Field Types in SQL

INTEGER - A whole number
VARCHAR(10) - Up to 10 characters.
CHAR(10) - Fixed number of characters
DATE - A date
DATETIME - Date and time
FLOAT - Floating point numbers
Field Types specific to MySQL

TEXT - Allows up to 65535 characters
DECIMAL(10,2) - Up to 10 digits before the point, 2 after.
Create a database

$ mysqladmin –user=ravi –password=xxx create database addressdb
Using the database

$ mysql –user=ravi –password=xxx
mysql> USE addressdb
Create a table

mysql> CREATE TABLE p_addr (i INTEGER PRIMARY KEY,address TEXT,email VARCHAR(30),pincode DECIMAL(10),phone DECIMAL(15),website TEXT);
Add a column called “name” to the table

mysql> ALTER TABLE p_addr ADD name VARCHAR(30);
Inserting values into table

mysql> INSERT INTO p_addr VALUES (1,”My, present, address”,”ravi@localhost”,681024,2122536, “http://linuxhelp.blogspot.com”,”Ravi”);
List the contents of the table

mysql> SELECT * FROM p_addr;
Delete a row from the table

mysql> DELETE FROM p_addr WHERE i=1;
Rename a column in the table from “address” to “home_address”

mysql> ALTER TABLE p_addr CHANGE address home_address INTEGER;
Note: You cannot use this method to rename a column which is a primary key.

Change an existing record in the table

mysql> UPDATE p_addr SET name=”Sumitra” WHERE i=2;
Delete the table from the database

mysql> DROP TABLE p_addr;
List the databases

$ mysqlshow –user=ravi –password=xxx
+———–+
| Databases |
+———–+
| addressdb |
| myblog |
| mysql |
| test |
+———–+

List the tables in the database “addressdb”

$ mysqlshow –user=ravi –password=xxx addressdb
Database: addressdb
+———+
| Tables |
+———+
| p_addr |
| mytble |
| phonebk |
+———+