Skip to content

Commit

Permalink
Add missing ssh module
Browse files Browse the repository at this point in the history
  • Loading branch information
alxchk committed May 23, 2017
1 parent e48afa5 commit 7a3f183
Showing 1 changed file with 105 additions and 0 deletions.
105 changes: 105 additions & 0 deletions Linux/lazagne/softwares/sysadmin/ssh.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
from lazagne.config.constant import *
from lazagne.config.write_output import print_debug
from lazagne.config.moduleInfo import ModuleInfo
from lazagne.config import homes

import os
import pwd

class Ssh(ModuleInfo):
def __init__(self):
options = {'command': '-S', 'action': 'store_true', 'dest': 'ssh', 'help': 'ssh'}
suboptions = []
ModuleInfo.__init__(self, 'ssh', 'sysadmin', options, suboptions)

def get_ids(self):
known = set()
for user, identity in homes.users(file=[
os.path.join('.ssh', item) for item in (
'id_rsa', 'id_rsa', 'id_ecdsa', 'id_ed25519'
)
]):
if os.path.isfile(identity):
try:
with open(identity) as fidentity:
yield {
'KEY': fidentity.read(),
'User': user,
}
known.add(identity)
except:
pass

for user, config in self.get_configs():
for pw in self.get_ids_from_config(user, config):
if pw['KEY'] in known:
continue

try:
with open(pw['KEY']) as fidentity:
pw['KEY'] = fidentity.read()
yield pw
known.add(identity)
except:
pass


def get_configs(self):
return homes.users(file=os.path.join('.ssh', 'config'))

def create_pw_object(self, identity, host, port, user):
pw = {'KEY': identity}
if host:
pw['Host'] = host
if port:
pw['Port'] = port
if user:
pw['Login'] = user
return pw

def get_ids_from_config(self, default_user, config):
try:
hostname = None
port = 22
user = default_user
identity = None

with open(config) as fconfig:
for line in fconfig.readlines():
line = line.strip()
if line.startswith('#'):
continue
line = line.split()
if len(line) < 2:
continue
cmd, args = line[0].lower(), line[1:]
args = ' '.join([ x for x in args if x ])
if cmd == 'host':
if identity:
yield self.create_pw_object(
identity, hostname, port, user
)

hostname = None
port = 22
user = default_user
identity = None
elif cmd == 'hostname':
hostname = args
elif cmd == 'user':
user = args
elif cmd == 'identityfile':
if args.startswith('~/'):
args = config[:config.find('.ssh')] + args[2:]
identity = args

if identity:
yield self.create_pw_object(
identity, hostname, port, user
)

except Exception, e:
pass

def run(self, software_name=None):
return list(self.get_ids())

0 comments on commit 7a3f183

Please sign in to comment.