Archive

Archive for January, 2010

Pipe tar file to remote server w/ ssh

January 28th, 2010

jesterj@jesterj-laptop:~$ tar zcvf - donkey* | ssh jesterj@donkey.dnsdojo.com “cat > ~/testarchive3.tar”By far the easiest way to tar a file and send it to a remote location is to simply have two commands on one line. The first to create the tar file, the other to scp the command. In this example, I am not prompted for a passphrase since i have my ssh key saved on the remote server for this user.

Easy:

jesterj@jesterj-laptop:~$ tar -cvf testarchive.tar donkey*; scp testarchive.tar jesterj@remoteserver:~
donkey
donkey1.zip
donkey2.txt
donkey2.zip
donkey.tar
donkey.txt
testarchive.tar                               100%   20KB  20.0KB/s   00:00
jesterj@jesterj-laptop:~$

Pipe the file…More complicated:

jesterj@jesterj-laptop:~$ tar zcvf - donkey* | ssh jesterj@donkey.dnsdojo.com "cat > ~/testarchive3.tar"

source: http://www.cyberciti.biz/faq/howto-use-tar-command-through-network-over-ssh-session/

Linux, Networking

Disabling service on startup in ubuntu

January 26th, 2010

Need to install debian-helper-scripts.

root@pse07:/var/lib# service apparmor off
The program 'service' can be found in the following packages:
* debian-helper-scripts
* sysvconfig
Try: apt-get install <selected package>
bash: service: command not found
root@pse07:/var/lib# sudo apt-get install debian-helper-script
root@pse07:/var/lib# service apparmor stop
Unloading AppArmor profiles : done.

Uncategorized

Changing data dir for mysql & dealing with Apparmor

January 26th, 2010

In ubuntu there is program called apparmor that verifies certain programs are configured appropriately, mysql included. Just recently the partition that holds all the data (/var/lib/mysql) filed up. Since I was at 100% and this is a production box I needed to fix this quickly. My options were as follows:

1. Setup a sym link from /var/lib/mysql to /home/mysql, move data here OR
2. Simply create a new dir call /home/mysql, move data here

To do this edit /etc/mysql/my.cnf
...
datadir =/home/mysql
#datadir = /var/lib/mysql
...

Save the file and restart mysql and you will get the following error:

root@slave:/etc/mysql# /etc/init.d/mysql2 start
* Starting MySQL database server mysqld [fail]

The problem here is that apparmor is dissallowing mysql from running in any other location than /var/lib/mysql. You will need to edit the apparmor file for mysql.

root@slave:/etc/apparmor.d# grep "/var/lib/mysql" usr.sbin.mysqld
/var/lib/mysql/ r,
/var/lib/mysql/** rwk,

To correct this problem replace all instances of /var/lib/mysql with /home/mysql. Open the file with file and do a search and replace. See that I am escaping the slashes!

:%s /\/var\/lib\/mysql/\/home\/mysql/

Reload appamor and mysql should start up with the new config in your my.cnf file!

root@slave:/etc/apparmor.d# /etc/init.d/apparmor restart
Reloading AppArmor profiles : done.
root@slave:/etc/apparmor.d# /etc/init.d/mysql2 start
* Starting MySQL database server mysqld

Networking, Uncategorized

Listing Ports and Active connections w/ Netstat

January 14th, 2010

Who’s connected to my server?

root@donkey:~# netstat -ntla | grep "80"
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN
tcp        0      0 192.168.1.5:80          206.169.197.253:20277   ESTABLISHED

Grep port for host.

root@donkey:~# lsof -i tcp:80 | grep "20277"
apache2 20114 www-data    9u  IPv4 9576072       TCP donkey.local:www->206-169-197-253.static.twtelecom.net:20277 (ESTABLISHED)

Kill Connection

kill -9 20114

Uncategorized

Managing Processes with job, fg, bg, screen.

January 14th, 2010

Let’s talk about managing processes. If you want to run a script or scripts and then continue with other tasks while they run w/o using cron then you can use the ‘jobs’ command.

First, let create a script that will run indefinitely so we have time to work with our examples.

#!/usr/bin/python
x=0
while x == 1:
z=1

The above script will loop indefinitely since ‘x’ will never equal 1.

Next, run the script from the command line. Adding a & to the end of the script will send this jobs to the background.

jesterj@jesterj-laptop:~$ python loop.py &
[1] 4221

This sends loop.py to the background  with process id ‘4221′. You can view the process id stats by doing the following:

jesterj@jesterj-laptop:~$ ps -ef | grep ‘4221′
jesterj   4221  3083 99 16:37 pts/1    00:02:40 python loop.py

To view all jobs running in the background type ‘jobs’.

jesterj@jesterj-laptop:~$ jobs
[1]+  Running                 python loop.py &

To add another instance of the script to the background, repeat.

jesterj@jesterj-laptop:~$ python loop.py &
[2] 4333
jesterj@jesterj-laptop:~$ jobs
[1]-  Running                 python loop.py &
[2]+  Running                 python loop.py &

To quit the process use kill and the job number.

jesterj@jesterj-laptop:~$ kill %1
jesterj@jesterj-laptop:~$ jobs
[1]-  Terminated              python loop.py
[2]+  Running                 python loop.py &

To bring a process out of jobs to the foreground use, fg:

jesterj@jesterj-laptop:~$ fg %2
python loop.py

Linux

Vi Editing Basics

January 14th, 2010

Basic Commands

:w  - write file

:wq - write and quit

:q! - exit, ignore changes

:set number - show line numbers

:set nonumber - turn off line numbers.


Text Mode

dd - Delete lines

2dd - delete 2 lines

p  - paste buffer lines

w - skip to mext word

a - append after cursor

i - insert at cursor

o - open new line after current line

r - replace character.

yy - yank current line

Searching

/string - search forward for string

?string - search back for string.

/tel[a-z] - search for ‘tel’ with last letter a-z

Find and Replace - ‘g’ stand for global, all lines in document.

:s/OLDWORD/NEWWORD/g

Search and replace,escaping slashes.

To replace the phrase /var/lib/mysql with /home/mysql you need to escape each ‘/’ with a ‘\’. For example…

:%s/\/var\/lib\/mysql/\/home\/mysql/

Regular Expressions

/Hello/ - search for word ‘Hello’ in line.

/^Hello$/ - search and matches if Hello is only word on line.

Recording Macros

qv - start record mode for macro ‘a’. second letter should be lower case character.

q - stop record

@v - to paste recorded text

9 @v - repeat v macro 9 times.
Save Settings

You can save persistent changes for vi by editing ~/.exrc

set number
….

Uncategorized, mysql

Working with TAR and ZIP files in Python

January 7th, 2010

>>>import os,tarfile

>>> tarfile=tarfile.open(’/data/archive-1-1-1.tgz’,'r’)
>>> for x in tarfile:
…     print x

<TarInfo ‘./tmp/’ at 0xb7d9588c>
<TarInfo ‘./tmp/biteoff/’ at 0xb7d9558c>
<TarInfo ‘./tmp/biteoff/bite_offload.tgz’ at 0xb7d959cc>
<TarInfo ‘./tmp/biteoff/aesid.log’ at 0xb7d95a2c>
<TarInfo ‘./tmp/biteoff/lruid.xml’ at 0xb7d95a8c>
<TarInfo ‘./tmp/biteoff/BITEdump.sql’ at 0xb7d95aec>
<TarInfo ‘./tmp/biteoff/offload.cfg’ at 0xb7d95b4c>

ZIP

>>> import os, zipfile
>>> zipfile=zipfile.ZipFile(’/home/jesterj/foo.zip’,'r’)
>>> for x in zipfile.namelist():
…     print x

index.php

Python