说明蓝色=命令名称

   浅绿=命令参数

   浅蓝=选项

   紫色=目录

   系统环境:CentOS  5.5 x86_64

   python版本:Python 2.7.3

参考paramiko和pycrypto官方文档写了一个通过rsa private key生成 public key的工具,如下

#!/usr/bin/env python#-*- coding:utf-8 -*-#Author:left_left import structimport base64import sysimport osfrom optparse import OptionParserfrom Crypto.PublicKey import RSAdef get_bin(n):    s = ''    n = long(n)    while (n != 0) and (n != -1):        s = struct.pack('>I', n & 0xffffffffL) + s        n = n >> 32             for i in enumerate(s):        if (n == 0) and (i[1] != '\000'):            break        if (n == -1) and (i[1] != '\xff'):            break             s = s[i[0]:]    if (n == 0) and (ord(s[0]) >= 0x80):        s = '\x00' + s    if (n == -1) and (ord(s[0]) <0x80):        s = '\xff' + s             s = struct.pack(">I", len(s)) + s    return sdef parse():    p = OptionParser()    p.set_usage("command [options] privatekey")    p.add_option('-p', '--publickey', dest = 'publickey', default = './p.pub')    #p.add_option('-v', '--private', dest = 'vf')    p.add_option('-t', '--type', dest = 'type', default = 'ssh-rsa')    return pdef main():    p = parse()    options, args = p.parse_args()            try:        vf = args[0]    except IndexError:        print "Please use '-h/--help' for help!"        sys.exit(1)    try:     f = open(vf)    except IOError, e:        print e        sys.exit(1)             uf = options.publickey    if os.path.exists(uf):        print "%s is exists!" % uf        sys.exit(1)    try:        k = RSA.importKey(f.read())    except ValueError, e:        print e        sys.exit(1)    s = ''    t = options.type    l = len(t)    s = struct.pack('>I%ss' % l, l, t)    e = get_bin(k.e)    n = get_bin(k.n)    b = base64.b64encode(''.join((s, e, n)))             try:        of = open(uf, 'w')        of.write("%s %s\n" % (t, b))        of.close()    except IOError, e:        print e        sys.exit(1)if __name__ == "__main__":    main()