e2402a9536
# Conflicts: # .idea/workspace.xml # git.py
50 lines
1.9 KiB
Python
50 lines
1.9 KiB
Python
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
|
|
self.local_path = os.path.expanduser('~')
|
|
self.public_key_path = os.path.join(self.local_path, '.ssh', 'id_rsa.pub')
|
|
self.public_key = None
|
|
|
|
def add_key(self):
|
|
stdout, stderr = self.run("echo \"{0}\" >> ~/.ssh/authorized_keys".format(self.public_key))
|
|
return stdout
|
|
|
|
@staticmethod
|
|
def create_key():
|
|
if os.path.exists(os.path.join(os.path.expanduser('~'),".ssh","id_rsa.pub")):
|
|
os.remove(os.path.join(os.path.expanduser('~'),".ssh","id_rsa.pub"))
|
|
os.remove(os.path.join(os.path.expanduser('~'), ".ssh", "id_rsa"))
|
|
|
|
print("Key Path:","{0}".format(os.path.join(os.path.expanduser('~'),".ssh","id_rsa")))
|
|
ret_code = subprocess.Popen(["ssh-keygen", "-b 4096","-N ''", "-q",
|
|
"-f {0}".format(os.path.join(os.path.expanduser('~'),".ssh","id_rsa")),
|
|
], shell=False,
|
|
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
|
try:
|
|
stdout, stderr = ret_code.communicate(input=b"\ny\n", timeout=8)
|
|
except subprocess.TimeoutExpired:
|
|
ret_code.kill()
|
|
stdout, stderr = ret_code.communicate()
|
|
return stdout.decode("utf-8")
|
|
|
|
def get_key(self):
|
|
self.public_key = open(self.public_key_path, 'r').readline()
|
|
return self.public_key
|
|
|
|
|
|
if __name__ == "__main__":
|
|
new_key = Key("compute.bktus.com", "/home/git", "git", "123456", ssh_key=False)
|
|
# new_key.create_key()
|
|
new_key.get_key()
|
|
print(new_key.public_key)
|
|
new_key.add_key()
|