giteasy/keygen.py

36 lines
1.1 KiB
Python
Raw Normal View History

2019-03-11 03:18:02 +00:00
import git
import subprocess
import os
import ssh
class Key(ssh.SSH):
def __init__(self,hostname, path, user, password=None, ssh_key=False):
super().__init__(hostname=hostname, path=path, user=user, passwd=password, ssh_key=ssh_key)
self.connect(5)
self.pub_key = None
self.keygen = None
2019-03-11 18:28:36 +00:00
self.local_path = "/Users/huyibing/"
2019-03-11 10:58:59 +00:00
self.public_key_path = self.local_path+".ssh/id_rsa.pub"
self.public_key = None
2019-03-11 03:18:02 +00:00
def add_key(self):
2019-03-11 10:58:59 +00:00
self.run("echo \"{0}\" >> ~/.ssh/authorized_keys".format(self.public_key))
2019-03-11 03:18:02 +00:00
2019-03-11 10:58:59 +00:00
@staticmethod
def create_key():
ret_code = subprocess.Popen(["ssh-keygen", "-b 4096"], shell=True,
2019-03-11 03:18:02 +00:00
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
2019-03-11 10:58:59 +00:00
stdout, stderr = ret_code.communicate(input=b"\n \n \n \n")
2019-03-11 03:18:02 +00:00
def get_key(self):
2019-03-11 10:58:59 +00:00
self.public_key = open(self.public_key_path, 'r').readline()
2019-03-11 03:18:02 +00:00
if __name__ == "__main__":
new_key = Key("compute.bktus.com", "/home/git", "git", "123456", ssh_key=False)
# new_key.create_key()
2019-03-11 10:58:59 +00:00
new_key.get_key()
print(new_key.public_key)
new_key.add_key()