Ignoring the Git
git status and all those files I don't want in version control show up in the 'Untracked files' list. The obvious answer is to create a .gitignore file, but I really don't want to do it by hand, because it just doesn't seem lazy enough. So, I present to you a way to generate the .gitignore file without having to type any of the file names:
git ls-files -o --exclude-standard >> .gitignore It's my pty and I'll cry if I want to.
chrt:webdev2:~/.cpan/build/IO-Tty-1.07# make test
PERL_DL_NONLAZY=1 /usr/bin/perl "-Iblib/lib" "-Iblib/arch" test.pl
1..4
Configuration: -DHAVE_DEV_PTMX -DHAVE_GETPT -DHAVE_GRANTPT -DHAVE_OPENPTY -DHAVE_PTSNAME -DHAVE_PTSNAME_
R -DHAVE_PTY_H -DHAVE_SIGACTION -DHAVE_SYS_STROPTS_H -DHAVE_TERMIOS_H -DHAVE_TERMIO_H -DHAVE_TTYNAME -DH
AVE_UNLOCKPT
Checking for appropriate ioctls: TIOCNOTTY TIOCSCTTY
Checking that returned fd's don't clash with stdin/out/err...
trying getpt()...
pty_allocate(nonfatal): getpt(): No such file or directory at /root/.cpan/build/IO-Tty-1.07/blib/lib/IO/
Pty.pm line 24.
trying openpty()...
pty_allocate(nonfatal): openpty(): No such file or directory at /root/.cpan/build/IO-Tty-1.07/blib/lib/I
O/Pty.pm line 24.
trying /dev/ptmx...
trying grantpt()...
IO::Tty::pty_allocate(nonfatal): grantpt(): No such file or directory at /root/.cpan/build/IO-Tty-1.07/b
lib/lib/IO/Pty.pm line 24.
trying unlockpt()...
trying ptsname_r()...
IO::Tty::open_slave(nonfatal): ptsname_r(): No such file or directory at /root/.cpan/build/IO-Tty-1.07/b
lib/lib/IO/Pty.pm line 24.
trying to open /dev/pts/3...
IO::Tty::open_slave(nonfatal): open(/dev/pts/3): No such file or directory at /root/.cpan/build/IO-Tty-1
.07/blib/lib/IO/Pty.pm line 24.
pty_allocate(nonfatal): open(/dev/ptmx): No such file or directory at /root/.cpan/build/IO-Tty-1.07/blib
/lib/IO/Pty.pm line 24.
trying BSD /dev/pty??...
Cannot open a pty at test.pl line 42
cd /dev
./MAKEDEV pty
mount -t devpts -o rw,gid=5,mode=620 none /dev/pts
Typo Filters
EDIT: I just found some help documentation for the text filters buried within the admin interface at http://yourblog/admin/textfilters
Getting GitHub working with a new repository
So I thought I would try out git and github and see what all the cool kids are talking about. The initial setup was really easy. I got my first repository setup within minutes and was committing without any problems. Then I created a clone on my laptop and continued working. The problem arose when I tried to get this work back into the central repository and then back to my desktop. The git push was fine, but the git pull kept failing giving me the following:
neo$ git pull
You asked me to pull without telling me which branch you want to merge with,
and 'branch.master.merge' in your configuration file does not tell me either.
Please name which branch you want to merge on the command line and try again
(e.g. 'git pull <repository> <refspec>'). See git-pull(1) for details on the refspec.
If you often merge with the same branch, you may want to configure the following
variables in your configuration file:
branch.master.remote = <nickname>
branch.master.merge = <remote-ref>
remote.<nickname>.url = <url>
remote.<nickname>.fetch = <refspec> Having a quick search around the net, told me that I needed to update only the branch.master.remote and the branch.master.merge. Since I am merging with the 'origin' branch, I run:
neo$ git config branch.master.remote origin Then to get the merge to work correctly I set the following:
neo$ git config branch.master.merge refs/heads/master This tells git to merge the remote changes into the master branch. Once done a simple
neo$ git pull should do the trick.
Becoming a better programmer
Ruby String.capitalize for all words
'the example string' -> 'The Example String'
So I had a look around and found this great little snippet of code:
'the example string'.gsub(/\b\w/){$&.upcase} Laters,
J.
Rake -- making sense of the garden tools
- Rails envy tell you all you need to get started using some very funny analogies.
- Martin Fowler takes a more in depth look at how rake stacks up against other build languages like make and ant.
require 'rake'
task :default => :hello
desc 'Say hello'
task :hello do
puts 'Hello World'
end anon@box:~/ruby$ rake (in /home/anon/ruby) Hello WorldI was in inspired by the ease with which I had written a simple rake task. So I decided to create a slightly more complex one using namespaces. Namespaces allow you to group related tasks with a common prefix, making it easier to figure out which tasks to call when you return to the script at a later date. It also allows you to have tasks with the same name perform different actions. A common example would be cleaning up files, directories and databases. It makes sense to name these actions clean or cleanup as that is the function they are performing. However, you can imagine the chaos if there were three tasks in a single Rakefile all with the same name. To get around this we use namespaces:
namespace :database do
task :clean do
#clean up the database
end
end
namespace :files do
task :clean do
#clean up the files
end
end
namespace :dir do
task :clean do
#clean up the directory structure
end
end
rake database:cleanThis was all working great, until I tried to make one of the tasks created in a name space the default task.
require 'rake'
task :default => :logs:clean
namespace :logs do
desc 'Clean up generated files'
task :clean do
#delete generated files
end
end
It turns out that ruby symbols don't like to have a ':' in them. After some digging around I discovered that tasks in namespaces have to be called slightly differently in order to get around this problem. The answer is to use a string:
require 'rake'
task :default => 'logs:clean'
namespace :logs do
desc 'Clean up generated files'
task :clean do
#delete generated files
end
end Disabling failed ssh logins from script kiddies
- Someone is trying to hack into my machines.
- The machine can slow to a crawl when trying to deal with the thousands of requests that I am getting. Causing my web services to slow down.
So, the question is:
How can I deal with this?
Some common suggestions:
- Change your port to a non-standard port such as 2222, or some other random number. I agree that this does work, but it can be annoying if my applications cant be configured to connect to a non-standard port.
- Use port knocking to keep the port closed, until the right sequence of ports have been probed. This sounds more elegant than changing the port number, but again if my programs cant do this, then I need some sort of hack to get the port open.
I have gone for a slightly different approach, which is to setup a cronjob to monitor my log files and add the offenders to a list of banned ip addresses. The code was written in ruby and is running on a debian server
Without further ado, here is the code:#!/usr/bin/ruby -T
# hash of found ips used to store the ips found in the
# auth.log
ipFound = Hash.new(0)
puts 'Checking for scanning attempts'
# 1. read in the auth.log
authLog = File.open('/var/log/auth.log','r')
authLog.each_line { |line|
# 2. check for the ip addresses that are scanning
if line =~ /Failed password for invalid user/
if line =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/
@ipaddress = $1
#add it to the list
ipFound[@ipaddress] += 1
end
end
}
authLog.close
# hash used to store the ips that are already banned
ipDenied = Hash.new(0)
# open the list of banned ip adresses
hostsDeny = File.open('/etc/hosts.deny','r')
hostsDeny.each_line { |line|
if line =~ /ALL:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/
ipDenied[$1] += 1
end
}
hostsDeny.close
# if ip not in denied list or from the local network,
# then add it
hostsDenied = File.open('/etc/hosts.deny','a')
ipFound.each_pair { |ip,count|
puts "IP:#{ip} -> #{count}"
if ipDenied[ip] != 0
puts "IP Already denied"
next
end
if ip =~ /^192.168.1/
puts "IP in local network"
next
end
if count < 10
puts "Didn't scan that much"
next
end
puts "Adding to the denied list"
hostsDenied << "ALL:#{ip}\n"
}
hostsDenied.close
puts "Done"
10 * * * * /path/to/banning_code 2>&1 | Mail -s 'offender ban run' you@your.domainNow it gets run every ten minutes and adds login offenders to the banned list. There is loads more that could be done with this code and it doesn't check for some edge cases, but it will catch most of the script kiddies and get rid of them, until their ip address changes :-(
Debian locales
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = "en_GB:en",
LC_ALL = (unset),
LANG = "en_GB.UTF-8"
are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
It turns out that this can be easily solved in a couple of steps (on debian).- install the locales package:
apt-get install locales
- reconfigure your locale:
dpkg-reconfigure locales
Installing Typo
- After creating a fresh debian install and using apt to get ruby and gems tried to run 'gem install typo'. Everything seemed to go well, until I actually tried to run the typo install command. It wasn't there. Apparently the debian version of gems is broken and wont install the required stubs into the /usr/bin directory. To get around this I had to manually install the gems package from rubygems.org.
- With gems manually installed I tried to get typo yet again. This time I received an error message stating that SSL was not installed on the system. Fixing this required an apt-get of openssl and libopenssl-ruby
- After finally getting rails and typo installed I tried running the typo command yet again. The failure this time complained about the migrate task not working. This turned out to be due to the rails version being too new. So, I downgraded the installed version of rails to 1.2.6 from 2.0.1 and everything worked fine.
