Пример #1
0
 def generateSSHKey(self):
     display('Generating ssh key...')
     c = Connection('localhost')
     c.local('rm -f "{keyFile}.*"'.format(**self.properties))
     c.local("echo 'yes' | ssh-keygen -t rsa -f {keyFile} -C {username}  -N '' ".format(**self.properties),hide='out')
     c.close()
     #p = Popen("echo 'yes' | ssh-keygen -t rsa -f {keyFile} -C {username} -N '' ".format(**self.properties),
     #              stdout=PIPE,
     #              shell=True,
     #              stderr=PIPE
     #               )
     #print(p.communicate())
     with open (self.properties['pubKeyFile'],'r') as f:
         display('Opening {}'.format(self.properties['pubKeyFile']))
         self.pub = f.read().strip()
Пример #2
0
def main():
    hosts = ['192.168.19.128', '192.168.19.129']
    Config.ssh_config_path = 'ssh_config'  # SSH Config File

    try:
        for host in hosts:
            con = Connection(host)
            # All Rask Run
            print()
            print('==============')
            print(ahc(con, 'date', hide=True, hostNameShow=False))
            print(hostname(con, hide=True))
            print(ip(con, hide=True))
            print(disk_free(con, hide=True))
            print(chpasswd(con, 'python', hide=True))
            con.close()

    except RuntimeError as r:
        sys.stderr.write('=== Runtime Error! ===')
        sys.stderr.write(str(r.args))
    print('ALL Done!')
Пример #3
0
class SessionProcessor:
    """
    Process sessions.
    """
    def __init__(self, host_id, session_id, session_config):
        host = Host.objects.select_related('group').get(pk=host_id)
        hostname = host.hostname or host.name
        config = MINKE_FABRIC_CONFIG.clone()
        self.host = host

        # At first try to load the hostgroup- and host-config...
        for obj in (host.group, host):
            if not obj or not obj.config: continue
            if obj.config in MINKE_HOST_CONFIG:
                config.load_snakeconfig(MINKE_HOST_CONFIG[obj.config])
            else:
                msg = 'Invalid MINKE_HOST_CONFIG for {}'.format(obj)
                logger.warning(msg)

        # At least load the session-config...
        config.load_snakeconfig(session_config or dict())

        # Initialize the connection...
        self.con = Connection(hostname,
                              host.username,
                              host.port,
                              config=config)

        # Initialize the session...
        self.minke_session = MinkeSession.objects.get(pk=session_id)
        REGISTRY.reload(self.minke_session.session_name)
        session_cls = REGISTRY[self.minke_session.session_name]
        self.session = session_cls(self.con, self.minke_session)

    def run(self):
        try:
            started = self.session.start()
            if not started: return

            try:
                self.session.process()

            # Since task-interruption could happen all along between
            # session.start() and session.end() we handle it in the outer
            # try-construct.
            except KeyboardInterrupt:
                raise

            # A SessionError might be raised by from the process method of a
            # session itself. It is a convenient way to end a session with an
            # error status.
            except SessionError as exc:
                self.session.set_status('error')
                for msg in exc.args:
                    self.session.add_msg(msg, 'error')
                self.session.end()

            # paramiko- and socket-related exceptions (ssh-layer)
            except (SSHException, GaiError, SocketError):
                self.session.end(failure=True)
                self.session.add_msg(ExceptionMessage())

            # invoke-related exceptions (shell-layer)
            except (Failure, ThreadException, UnexpectedExit):
                self.session.end(failure=True)
                self.session.add_msg(ExceptionMessage())

            # other exceptions raised by process (which is user-code)
            except Exception:
                self.session.end(failure=True)
                exc_msg = ExceptionMessage(print_tb=True)
                logger.error(exc_msg.text)
                if settings.MINKE_DEBUG:
                    self.session.add_msg(exc_msg)
                else:
                    # TODO: relegate to the log.
                    self.session.add_msg('An error occurred.', 'error')

            else:
                self.session.end()

        # task-interruption
        except KeyboardInterrupt:
            self.session.end()

        # at least close the ssh-connection
        finally:
            self.con.close()