Actually the real question would be why would you want to do that? Well it a long story and I didn’t have many other options but here’s how I did it.

First a shell script that looks something like this:

#!/bin/bash

filename="needed.xml"
pathto="userdirectory/datadirectory/"
hostname="123.123.123.123"
username="username"
password="password"

cd /directory/where/the/file/goes/
ftp -n $hostname < quote USER $username
quote PASS $password

binary
cd $pathto
get $filename
quit
EOF

cool... then a cron job that calls that code every 10 minutes:

*/10 * * * * /home/user/diectory/mygetfile.sh

Yes to Mercurial

Dec, 29 -- Categories: Linux, Mac

Mercurial is a very nice alternative to svn. I have several projects in it now and I’m pretty happy with it. Here are some starter commands. These should be executed from the command line in the directory that you want to control.

$ hg init # creates .hg

$ hg add # add those ‘unknown’ files
$ hg commit # commit all changes into a new changeset, edit changelog entry
hg commit -m ‘updated bad links to style sheet and changed directory name’

$ hg parents # see the currently checked out revision (or changeset)

$ hg status # show all non-ignored files

$ hg help

# export your current repo via HTTP with browsable interface on port 8000
$ hg serve -n “My repo”

hg revert –all -r 268033ec7859
Backing out changes

Reverting the whole tree to a known-good revision
It’s easy, like using a sledgehammer is easy. But this is usually overkill.

$ hg pull -u
$ hg revert –all -r a0193d83c208 # use your known-good revision id here
$ hg commit # be kind, include the revision id in your commit message
$ hg push

There’s a more precise alternative:
Backing out a single changeset
Suppose changeset f8f4360bf155 broke something.
$ hg pull -u
$ hg backout f8f4360bf155 # use the revision id of the bad change here
This creates and commits a new changeset that reverts all the changes in that revision.

Capture top output

Nov, 21 -- Categories: Linux, Mac

Output 1 sample to a file
top -n 1 -b > top-output.txt

Output 1 sample to an email
top -n 1 -b | mail -s “Here’s my top” me@jamesborder.com

here is the mac version
top -l 1 | mail -s “Here’s my top mac” me@jamesborder.com

Command Line email

Nov, 10 -- Categories: Linux, Mac

Send email while your at the teminal….
echo "The body of your email here" | mail -s "The Subject of your email here" me@jamesborder.com

A couple of “useful examples”?

send the last 100 line of the error log to yourself (or to a client that needs to see them but doesn’t know how to get to them)
tail -100 /opt/local/apache2/logs/error_log | mail -s "From error_log" me@jamesborder.com

send the last 10 lines of your command line history to your self
history | tail -10 | mail -s "Howd I do that" me@jamesborder.com

PHP Short tags

Oct, 10 -- Categories: Apache, Linux, PHP

enable by setting the flag in .htaccess

php_flag short_open_tag on

Open php.ini ( /etc/php.ini or /usr/local/etc/php.ini), enter:
# vi php.ini

Set short_open_tag to On:
short_open_tag = On

Save and close the file. Restart webserver:
# service httpd restart
or
# /etc/init.d/httpd graceful

Cron notes & examples

Aug, 10 -- Categories: Linux

1 minute (0-59)
2 hour (0-23)
3 day of month (1-31)
4 month (1-12, or name such as jan, feb, etc)
5 day of week ( 0-6 (6 = Sunday) or name such as mon, tue,etc)
6 command to run

MAILTO=myemailaddress@example.com
every 10 minutes
*/10 * * * * /home/user/cmd.sh

every hour @ XX:59
59 * * * * /home/user/cmd.sh

every 3rd hr @ XX:59
59 */3 * * * /home/user/cmd.sh

everyday @ 4:59am
59 4 * * * /home/user/cmd.sh

every saturday @ 4:59am
59 4 * * 5 /home/user/cmd.sh

weekdays hourly from 9-5
59 09-17 * * 1-5 /home/user/cmd.sh

first of every month @4:59 am
59 4 1 * * /home/user/cmd.sh

first wednesday of every month @4:59 am
59 04 1-7 * 2 /home/user/cmd.sh

on reboot
@reboot /home/user/cmd.sh

Reset Mysql Root Password

Aug, 3 -- Categories: Linux, MySQL

Stop Mysql:
/etc/init.d/mysqld stop

Create a script to run the reset query
vi /root/mysql.reset.sql

add the reset query:
UPDATE mysql.user SET Password=PASSWORD(‘YOUR-NEW-MYSQL-PASSWORD’) WHERE User=’root’;
FLUSH PRIVILEGES;

execute the script:
# mysqld_safe –init-file=/root/mysql.reset.sql &

Sample output:
nohup: ignoring input and redirecting stderr to stdout
Starting mysqld daemon with databases from /var/lib/mysql
mysqld_safe[20970]: started

# killall mysqld
# /etc/init.d/mysql start

Using the wall command

Aug, 3 -- Categories: Linux, Mac

sh-3.2# wall
Type your message here
wall supports up to 20 lines

type control d to close

That will result in everyone on the system seeing your message regardless of wether mesg is enabled or not

Broadcast Message from me@JBook.local
(/dev/ttys001) at 21:13 CST...

Type your message here
wall supports up to 20 lines

type control d to close

another quick line is to

echo "Your message here" | wall

Disable Linux User Shell Account

Jul, 29 -- Categories: Linux

Type the following command to disable shell access for tom:

# chsh -s /sbin/nologin {username}
# chsh -s /sbin/nologin tom

Sample Outputs:

Changing shell for tom
Shell changed.

Find command basics

Jul, 10 -- Categories: Linux, Mac

find by name
find /directory -name “filename.ext”
find /directory -name filename.*
find /directory -name *.php

ignore case
find /directory -iname “filename.ext”

invert results (list files that don’t end with .php)
find /home/test ! -name “*.php”

search only 1 directory deep
find /home/test -maxdepth 2 -name “*.php”

only 2 directories deep
find /home/test -maxdepth 3 -name “*.php”

Find all directories
# find . -type d

Find only the normal files
# find . -type f

Find all the hidden files
# find . -type f -name “.*”

Find all the hidden directories
# find -type d -name “.*”

Find files bigger than the given size
# find ~ -size +100M

Find files smaller than the given size
# find ~ -size -100M

Find files that matches the exact given size
# find ~ -size 100M

Accessed files

Find based on minutes (60 minutes)
find /home -amin -60

Long list the files which are accessed within the last 1 hour.
find /home -amin -60 -exec ls -l {} \;

Find based on day(s) (24 hours)
find /home -atime -1

Long list the files which are accessed within the last 1 day.
find /home -atime -1 -exec ls -l {} \;

Modified files

Find based on minutes (60 minutes)
find /home -mmin -60

Long list the files which are accessed within the last 1 hour.
find /home -mmin -60 -exec ls -l {} \;

Find based on day(s) (24 hours)
find /home -mtime -1

Long list the files which are accessed within the last 1 day.
find /home -mtime -1 -exec ls -l {} \;

hide errors (Permission denied)
find -name “*.txt” 2>>/dev/null

To exclude php for example package when using “yum update”

Open /etc/yum.conf file:
# vi /etc/yum.conf

Append following line under [main] section, enter:
exclude=php*

or

at the command line

# yum –exclude=package* update

Yikes ssh login error

May, 19 -- Categories: Linux

When I tried to log in I got this:

ERROR
PTY allocation request failed on channel 0

The only way I could get in was to log in like this:
ssh myusername@myhost “/bin/bash -i”;

After a bit of Googling this was the fix I found that there is a device ptmx that might be missing with this error message. SO:

# /sbin/MAKEDEV -d /dev ptmx

restart ssh

/etc/init.d/sshd restart (reload would work to I would think)

More SSH login goodness

Apr, 22 -- Categories: Linux

Save a text file as “login.command” or whatever but thats what I’m calling mine.

Enter this:

#!/bin/sh
ssh yourusername@yourdomain.com

Then you’ll need to make the file executable
“cd” to wherever your file is then run

chmod +x login.command

You may also have to do a get info and then select “Terminal” under “Open With”. You should now be able to double click the file and then the terminal will open and login to your server. If you drag that file under your “Places” in the sidebar your now one click away.

 

 

SSH login without password

Apr, 15 -- Categories: Linux

You need to know if you didn’t already this means: anybody that has access to your computer or access to the to rsa keys has complete access to your server without knowing the password.

I REPEAT:
Anybody that has access to your computer or access to the to rsa keys has complete access to your server without knowing the password.

Got it?

But this also means you can now log in without being prompted for username/password and that is sweet. Just maintain some good computer hygiene & keep your computer and your keys safe.

When you google this there are a lot of different instructions, and a lot of different ways for it to go badly. But the shot of the deal is you need to get these two files in your .ssh directory on your local computer:

id_rsa
id_rsa.pub

Then you need to get a copy of the id_rsa.pub saved into the .ssh as authorized_keys, or get the line from your id_ra.pub added to that file if it already exists. Then if you have the permissions right etc. etc. no more being prompted for your password.

So on your local machine do this:

If the .ssh directory doesn’t exist:

mkdir ~/.ssh
chmod 700 ~/.ssh

else

cd ~/.ssh
ssh-keygen -t rsa

Running the key generator looks like this for me:

Generating public/private rsa key pair.
Enter file in which to save the key (/Users/me/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/me/.ssh/id_rsa.
Your public key has been saved in /Users/me/.ssh/id_rsa.pub.
The key fingerprint is:
XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX me@xxxxxpro.local
The key's randomart image is:
+--[ RSA 2048]----+
| &!..()= .|
| . . o.+d@+|
| E * + oo.+*|
| . * . …=|
| S . |
| |
| |
| |
| |
+-----------------+

just hit return for no pass phrase thats what we’re going for here.

Now your half way there. You need to get the public half of the key to the REMOTE computer and user account you want to log in TO:

scp ~/.ssh/id_rsa.pub you@yoursite.com:/home/youruser/id_rsa.pub

then login

ssh you@yoursite.com
Password:
mkdir .ssh
chmod 700 .ssh
cat id_rsa.pub >> .ssh/authorized_keys
chmod 644 .ssh/authorized_keys

then login to make sure it worked. I have done this may times and every time it didn’t work like I expected it was related to permissions or ownership some where.

then:
rm id_rsa.pub