常用各種小工具
今天來介紹一下我常使用的小工具吧。

pwgen

pwgen(Password Generator) 是由Mr. M基於Java寫的小程序,能夠生成特定長度的隨機字串(常用標準鍵盤有的字集)

載點:Not available yet


本來是揵好用的,以前在windows環境上面就經常用到。但在linux由於每次執行都得先啟動JVM,所以效能上略顯不足,於是我用perl寫了一個功能差不多的小工具:
#!/usr/bin/perl

use String::Urandom;

my $length = 16;

if($ARGV[0] =~ /^\d+$/) {
	$length = $ARGV[0];
}

my $rand = String::Urandom->new(
	LENGTH => $length
	, CHARS => [(0..9), ("a".."z"), ("A".."Z")]
);

print($rand->rand_string . "\n");

可惜perl使用起來有點麻煩,得建立CPAN的組態庫。安裝了的組態又無法刪除,感覺有點奇怪。


於是又用python寫了一個,這個是我覺得最舒服的:
#!/usr/bin/python

import string;
import random;

import os;
import sys;


if len(sys.argv) < 2:
    sys.exit('Usage: ' + os.path.basename(__file__) + ' length');

l = sys.argv[1];

if not l.isdigit():
    sys.exit('"' + l + '" is not a valid length.');

# cast l into intergers
l = int(l);

# define the character set
charSet = string.ascii_letters + string.digits;

# generate stack
out_stack = ''.join(random.choice(charSet) for x in range(l));

# Am I being piped?
if sys.stdout.isatty():
    print(out_stack);
else:
    sys.stdout.write(out_stack);

Flex assembler

這是寫flex時必備的小程序,他會建立一個Build的Class,能追蹤當前的編釋版本。
#!/bin/bash

# this command will execute on script exit
trap "{ [ -e Build.as ] && rm Build.as; }" EXIT


# Compiler checking
if [ -x mxmlc ]; then
	COMPILER=mxmlc
else
	COMPILER={編釋器位置}
fi

# Root checking
if [ $(id -u) -eq 0 ]; then
	echo This function cannot be run as root.
	exit 1
fi

# Get basename
FILE=$(basename $1 .as)

# This control if use build counter
isBUILD=0

# Proscenium implementation options
opts=''

# Check if include libraries
if [ "$2" = "--uselibs" ]; then
	opts="-include-libraries <組態庫路徑>/*.swc"
fi

# If build counter is available
if [ -e builds/$FILE ]; then
	
	# Enable build function
	isBUILD=1
	BuildInfo="builds/$FILE"
	
	# Generate Build class
	cat << 'EOF' > Build.as
package {

	public class Build {

		private var echo:Function = function (...msg:*):void { };
		protected var buildNumber:int = 
EOF

	# Increment build count
	BUILD=$(cat $BuildInfo|head -n 1)
	let "BUILD++"
	echo $BUILD>>Build.as
	cat << 'EOF' >> Build.as
		, __mode:String
		;

		public function Build(echo:Function = null, mode:Boolean = true):void {
			this.__mode = mode ? "Production":"Debug";
			if(echo as Function) {
				this.echo = echo;
				echo(
EOF

	# append rest of the codes
	echo \"$FILE \">> Build.as
	cat << 'EOF' >> Build.as
				+ __mode + " build " + buildNumber);
			}
		}

		public function set build(a:int):void {
			echo("Error: buildNumber is a read-only variable.");
		}
		public function get build():int {
			return buildNumber;
		}

		public function set mode(a:String):void {
			echo("Error: buildNumber is a read-only variable.");
		}
		public function get mode():String {
			return __mode;
		}

	}
}
EOF
else
	# Setup build counter for file
	echo 0>builds/$FILE
fi

# Compile
$COMPILER "$FILE.as" -static-rsls --show-actionscript-warnings=true --strict=true --debug=true -use-network=true -compatibility-version=3 $opts

# Move compiled file to swf root
[ -s "$FILE.swf" ] && mv "$FILE.swf" "<目標路徑>/$FILE.swf"

# If build success
if [ $? -eq 0 ] && [ $isBUILD -eq 1  ] ; then
	echo $BUILD > $BuildInfo
	echo PASSED >> $BuildInfo
	echo -e Build "\e[01;32m$BUILD\e[00m" $('date')
else
	sed '2s/.*/FAILED/' -i $BuildInfo
	echo $('date')
	echo -e ---"\e[01;31mError occured\e[00m"---
fi

還有一些簡單的排定工作如服務器IP更換提示,自動備份與簡單的管理等。

嘛,其實這的確是自找麻煩啦,這些東西寫的時候很耗時的。通常比起建立這些小工具,直接手動做這些動作省的時間反而要少得多了。

那為什麼還要做呢?

其實並不是討厭重複的工作,而是覺得與其將時間花在這些工作上,不如考慮怎麼將這些工作「自動化」比較有意義。加上做好之後就不用常常考慮這些事,所以我可以更力集中地做事。(不過我不知道到底什麼要我專注地去做,大概就是寫另一個小工具吧XD)

亦可能會讓我之後的生活過得輕鬆一點?
Tag(s): diary bash python perl
Profile picture
斟酌 鵬兄
Tue Oct 01 2013 12:44:51 GMT+0000 (Coordinated Universal Time)
Last modified: Sun Apr 10 2022 11:28:23 GMT+0000 (Coordinated Universal Time)
Comments
No comments here.
Do you even comment?
website: 
Not a valid website
Invalid email format
Please enter your email
*Name: 
Please enter a name
Submit
抱歉,Google Recaptcha 服務被牆掉了,所以不能回覆了