forked from dhurley94/linux-sysadmin-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sshkey.py
24 lines (24 loc) · 1.43 KB
/
sshkey.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
#!/usr/bin/env python
import subprocess
from optparse import OptionParser
def main():
usage = "usage: python %prog [options] arg"
parser = OptionParser(usage)
parser.add_option("-s", "--source", dest="sourceip", type=str,
help="set the source ip address.")
parser.add_option("-p", "--port", dest="sourceport", default="22",
help="set port, defaults to 22 if not set")
(options, args) = parser.parse_args()
if (options.sourceip is None):
subprocess.call("python sshkey.py --help", shell=True)
else:
genkey="ssh-keygen -t rsa"
setperm=""ssh root@%s -p %s "mkdir ~/.ssh && chmod 700 ~/.ssh && chmod 600 .ssh/authorized_keys""" % (options.sourceip, options.sourceport)
sendkey=""cat ~/.ssh/id_rsa.pub | ssh root@%s -p %s "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys""" % (options.sourceip, options.sourceport)
#sendkey="ssh-copy-id -p %s %s" % (options.sourceport, options.sourceip)
subprocess.call(setperm, shell=True)
subprocess.call(genkey, shell=True)
subprocess.call(sendkey, shell=True)
access="ssh root@%s -p %s " % (options.sourceip, options.sourceport)
if __name__ == "__main__":
main()