Friday, February 03, 2012

Simple password generator

This generates shadow file entries with MD5 crypted passwords.

If you don't specify a password to use on the command line, it will generate a random 15 character password for you.

NB: You really shouldn't use it with a password on the command line, as this can be seen by other users with the ps command while it's running. I've just done this to simplify the example. This code is a snippet, intended to be used in a larger script where the password would be read from a file or provided on STDIN.


#!/usr/bin/perl
#
#
#
use warnings;
use strict;

my $len = 15;

my $plaintext = shift;
if ( ! $plaintext ) {

    my @chars;
    for my $char (33 .. 126) {
        push @chars, chr($char);
    }
    for (1..$len) {
        $plaintext .= $chars[int(rand($#chars))];
    }

}

my @schars = ('a'..'z','A'..'Z',0..9,'.','/');
my $salt = '';
for (1..8) {
    $salt .= $schars[int(rand($#schars))];
}
my $crypted_pass = crypt($plaintext,'$1$' . $salt )
    or die($!);

print "$plaintext\n$crypted_pass\n";


There are other tools to do this.

For example, on Debian Linux so far I know of:


$ dpkg -S /usr/bin/openssl
openssl: /usr/bin/openssl
$ dpkg -S /usr/bin/mkpasswd
whois: /usr/bin/mkpasswd
$ dpkg -S /usr/bin/makepasswd
makepasswd: /usr/bin/makepasswd


Usage:


$ openssl passwd -1 -stdin <<EOT
passwd
EOT
$1$nRGcgK4T$uI7mxwMxGUt6NQ.lyu42./
$ mkpasswd -5 -s <<EOT
> passwd
> EOT
$1$0rg1g/e9$rh1lfYHX9qkSVihZ9vBcd/
$ makepasswd --crypt-md5 --clearfrom=-
passwd
passwd $1$IEK./reC$UbqosXZvVn6Hv/2Zej.va/


I wanted to run it on Solaris, and I wanted to run it in a Perl program (because the rest of the program I was writing was in Perl)

The openssl and mkpasswd programs are compiled C binaries, makepasswd is written in Perl, but it uses Crypt::OpenSSL::Random to generate better randomness, and the box I needed to run it on didn't have that library.

I expect the rand() in Perl is quite random enough for my needs here!

Thursday, February 02, 2012

Yesterday...

You can get yesterday's date with


$(TZ=GMT+24 date +%y%m%d)


(or tomorrow's with -24)

Friday, January 27, 2012

Perl keyword 'our' doesn't work on old Perl

If you need to declare a variable in old (pre 5.6?) Perl, and can't use the 'my' keyword...


#!/usr/bin/perl -w
#
#
#
use strict;
use Getopt::Std;

our $opt_v;
getopts('v');
sub VERBOSE { $opt_v; }


This will fail:


$ ./try.pl
Use of reserved word "our" is deprecated at ./try.pl line 8.
Global symbol "$opt_v" requires explicit package name at ./try.pl line 8.
Execution of ./try.pl aborted due to compilation errors.


You can use this instead:


#!/usr/bin/perl -w
#
#
#
use strict;
use Getopt::Std;

use vars '$opt_v';
getopts('v');
sub VERBOSE { $opt_v; }


There is a discussion on what the error means at http://www.perlmonks.org/?node_id=23916

Friday, October 07, 2011

Registry entries for TheGun

TheGun is a great replacement for notepad on Windows. It's a bit funny with registering itself to open file types though, but if you add these registry entries (which expect it to be in C:\Program Files\TheGun\THEGUN.EXE) it should work the way you want it.


Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.pl\OpenWithList]
"g"="THEGUN.EXE"

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt\OpenWithList]
"g"="THEGUN.EXE"

[HKEY_CURRENT_USER\Software\Microsoft\Windows\ShellNoRoam\MUICache]
"C:\\Program Files\\TheGun\\THEGUN.EXE"="TheGun"
"C:\\Progra~1\\TheGun\\THEGUN.EXE"="TheGun"

[HKEY_CURRENT_USER\Software\Classes\Applications\THEGUN.EXE\shell\open\command]
@="C:\\Progra~1\\TheGun\\THEGUN.EXE %1"

[HKEY_CURRENT_USER_Classes\Applications\THEGUN.EXE\shell\open\command]
@="C:\\Progra~1\\TheGun\\THEGUN.EXE %1"

Wednesday, October 05, 2011

rpm queries

The rpm options you usually need:


rpm -qa (list all packages installed
rpm -qf /path/to/file (which package provides this file)
rpm -ql (list the files in a package)



http://www.rpm.org/max-rpm/s1-rpm-query-parts.html

Friday, August 12, 2011

NIS & Automount


# vi /var/yp/etc/auto_home
# cd /var/yp
# make auto.home


To flush the old mapping on the client server:


# automount -v

Monday, August 01, 2011

Spark Timezone

To correct the timezone in Spark, create a Spark.vmoptions file (not spark.vmoptions and not Spark.vmoptions.txt) beside the Spark.exe and add this to it:

-Duser.timezone=Europe/London

then restart Spark.

Thanks to:

Comand line wrapping

If you're having trouble on Solaris with your command line wrapping at 80 characters so it starts printing again at the start of the line...
Have you logged in via a jump server and have your terminal settings configured in the first machine, which is controlling what the second machine can send back?

Tuesday, June 14, 2011

Microsoft Natural Keyboard Function Keys...

Did you know the natural keyboard 4000 has a "F-Lock" key?

http://www.ehow.com/how_6777639_enable-function-keys-microsoft-keyboard.html

Open a program that uses the function keys such as Microsoft Word or Microsoft Excel. Check your keyboard for an "F-Lock" or "Function Lock" key. This key is used to toggle function-key support; when it is toggled on, the function keys will not work.

Monday, June 13, 2011

Windows hosts file ignored

If you have already tried all of these things:

http://mihaiu.name/2005/windows-hosts-file-ignored/

If you've copied the file from somewhere else rather than opening it and pasting in entries, check the permissions on the file.
The file permissions could be wrong meaning Windows will not read it.

More reading:

http://www.bleepingcomputer.com/tutorials/tutorial52.html