-
Notifications
You must be signed in to change notification settings - Fork 15
/
gen-server-keys.py
executable file
·73 lines (63 loc) · 2.72 KB
/
gen-server-keys.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/python -tt
# -*- coding: ascii -*-
# Copyright (c) 2007, 2008 Dwayne C. Litzenberger <dlitz@dlitz.net>
#
# This file is part of PySFTPd.
#
# PySFTPd is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# PySFTPd is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
import os
import paramiko
rsa_bits = 3072
dss_bits = 1024
rsa_key_filename = "server_rsa_key"
dss_key_filename = "server_dss_key"
def show_progress(s):
sys.stdout.write("... " + s)
sys.stdout.flush()
def main():
status = 0
if os.path.exists(rsa_key_filename):
print >>sys.stderr, "%s already exists. Not generating RSA host key." % (rsa_key_filename,)
status = 2
elif os.path.exists(rsa_key_filename + ".pub"):
print >>sys.stderr, "%s already exists. Not generating RSA host key." % (rsa_key_filename + ".pub",)
status = 2
else:
print "Generating %d-bit RSA host key..." % (rsa_bits,)
rsa_key = paramiko.RSAKey.generate(bits=rsa_bits, progress_func=show_progress)
print "... Writing %s" % (rsa_key_filename,)
rsa_key.write_private_key_file(rsa_key_filename)
print "... Writing %s" % (rsa_key_filename + ".pub",)
open(rsa_key_filename + ".pub", "w").write("%s %s\n" % (rsa_key.get_name(), rsa_key.get_base64()))
del rsa_key
print "... done!"
if os.path.exists(dss_key_filename):
print >>sys.stderr, "%s already exists. Not generating DSS host key." % (dss_key_filename,)
status = 2
elif os.path.exists(dss_key_filename + ".pub"):
print >>sys.stderr, "%s already exists. Not generating DSS host key." % (dss_key_filename + ".pub",)
status = 2
else:
print "Generating %d-bit RSA host key..." % (dss_bits,)
dss_key = paramiko.DSSKey.generate(bits=dss_bits, progress_func=show_progress)
print "... Writing %s" % (dss_key_filename,)
dss_key.write_private_key_file(dss_key_filename)
print "... Writing %s" % (dss_key_filename + ".pub",)
open(dss_key_filename + ".pub", "w").write("%s %s\n" % (dss_key.get_name(), dss_key.get_base64()))
del dss_key
print "... done!"
sys.exit(status)
if __name__ == '__main__':
main()
# vim:set ts=4 sw=4 sts=4 expandtab: