Wednesday, July 11, 2012

SSH in scripts whit password using expect (not using keys)

To use ssh in scripts, its sure the most secure way to use ssh-keys. But some times that isn't possible. In that case the security focuset aprocach in ssh to not accept plain passwords in a other way than direkt keyboard imput is a pain in the ass. I tryed different aprochs to go around this like using sshpass or fd0ssh but it wasn't realy working for me. Using expect was working:

Install expect
sudo apt-get install expect
Then you can write a expect script like that
#!/usr/bin/expect -f

# Do not time out
set timeout -1

# Run the SSH comand (As example: open a tunnel for rsync)
spawn ssh -fNL localhost:1873:localhost:873 USER@EXAMPLE.com

# When the passwort prompt is displayed, type and send the password
expect "*?assword:" { send "PASSWORD\r"}

# We need to wayt a little bit
sleep 2
Or the same in a shell script:
#!/bin/sh
export "SSHPASS=PASSWORD"

/usr/bin/expect <<EOD
set timeout -1
spawn ssh -fNL localhost:1873:localhost:873 USER@EXAMPLE.com
expect "*?assword:" { send "$SSHPASS\r"}
sleep 2
EOD

Tuesday, May 22, 2012

Track empty directories whit GIT

According to the GIT FAQ, there is currently no way of adding a empty directory to a GIT repository. But there are workarounds whit placing a dummy file in it and ad this file to the repository. I don't like dummy files, so this is the best solution for me:

Put a file .gitignore in every directory which should be empty. If all other Files in this directory should be ignored add this two lines to the file:

*
!.gitignore

This is specially helpful to add directories which can contain temporary date (eg. tmp dirs).

Sunday, April 8, 2012

MySQL disable key

Some times you just need do to some quick editing but you have some foreign keys which prevent that. MySQL just tells:

Cannot delete or update a parent row: a foreign key constraint fails

The most easy way is to temporally disable the foreign keys whit:
SET FOREIGN_KEY_CHECKS=0;

DELETE or what ever

SET FOREIGN_KEY_CHECKS=1;

Tuesday, March 20, 2012

Link to an RSS feed in HTML (whit CakePHP)

Link to an RSS feed for a Webpage. The Browser should display the RSS Logo in the URL Field or elsewhere if you include this line in the of the HTML:

If you use CackePHP, you can generate this easy whit
echo $this->Html->meta('rss', '/.rss', array('title' => 'Space Pirates News'));
Of course, you want to change the URL and the Title ;-)

Tuesday, March 13, 2012

Show all errors in PHP applications

During developing whit PHP, I often run in to just empty page whit out any hint what was going wrong. Using Apache and have set the options "AllowOverride Options" or "AllowOverride All" in the Apache config (done usually by default) you can simply add this lines to the .htaccess file in the directory of the script:
php_value display_errors 1
php_value display_startup_errors 1
php_value error_reporting 2147483647

Wednesday, February 22, 2012

Get raid of SVN

While getting started to continue on old code, I had to remove all the .svn directories. This delete all the .svn directories in this and the sub directories:

rm -rf `find . -type d -name .svn`

Tuesday, February 21, 2012

Run a command until it is successful

During my travel, I was often in places whit bad Internet connections. Specially my uploads of pictures where often interrupted. I wrote a simple script which just executes the same command until it ends whit a success. It is very basic but did the job for me:

#!/bin/sh
tries=0

while [ "$exit" != "0" ]
do
 tries=`expr $tries + 1`
 echo -n "Try $tries on "
 date
 rsync -avzP "source" "destination"
 exit=$?
 echo $tries Exit $exit
 sleep 5
done

Save it to a file, upload.sh for example and make it executable:

chmod a+x upload.sh

Some times I used Windows for the upload, so here is almost the same as a batch file:

@echo off

:run
rsync.exe -avzP "source" "destination"
if ERRORLEVEL 1 goto run

UPDATE: I wrote a updated, all purpose script which I named tryuntilsuccess. It takes all parameters as the command to actually run for as many times until it returns 0.

#!/bin/sh
# tryuntilsuccess v 1.0
# http://codeatrandom.blogspot.com/2012/02/run-command-until-it-is-successful.html

tries=0
echo
echo Try: $@

while [ "$exit" != "0" ]
do
 tries=`expr $tries + 1`
 echo -n "Try $tries on "
 date
 $@
 exit=$?
 echo Try $tries give Exit $exit
 [ $exit != 0 ] && sleep 5
done

echo -----------------