def clone(c, test_results, experiment, login="", passs=""): if test_results["latest"] != "OK": test_results = record_no_run(test_results, "clone") return test_results repo = experiment["repo"] branch = experiment["branch"] result = c.run("mkdir test_today", warn=True, echo=True) if result.failed: c.run("rm -rf test_today; mkdir test_today", warn=True, echo=True) responder_user = Responder(pattern=r"Username for", response=f"{login}\n",) responder_pass = Responder(pattern=r"Password for", response=f"{passs}\n",) result = c.run( f"module load git; cd test_today;\ git clone -b {branch} {repo}", echo=True, pty=True, watchers=[responder_user, responder_pass], ) test_results = record_result(test_results, "clone", result) test_results["latest"] = "OK" return test_results
def create_user(c): c.user = '******' user = c.config.user password_responder = Responder( pattern=r'.*new UNIX password.*', response=f'{c.config.password}\n', ) press_ENTER_responder = Responder( pattern=r'Full Name|Room number|Work Phone|Home Phone|Other|correct?', response='\n\n', # for some reason one '\n' is not enough ) c.run(f'adduser {user}', watchers=[ password_responder, press_ENTER_responder, ]) c.run(f'adduser {user} sudo') # ssh stuff c.run(f'mkdir /home/{user}/.ssh') copy_authorized_keys(c) c.put(c.config.deploy.ssh_host_key, f'/home/{user}/.ssh/id_rsa') c.put(f'{c.config.deploy.ssh_host_key}.pub', # just in case f'/home/{user}/.ssh/id_rsa.pub') c.run(f'mkdir -p /home/{user}/sites/{c.host}') c.run(f'chown -R {user}:{user} /home/{user}/')
def patterns_span_multiple_lines(self): r = Responder(pattern=r'call.*problem', response='So sorry') output = """ You only call me when you have a problem You never call me Just to say hi """ eq_(list(r.submit(output)), ['So sorry'])
def patterns_span_multiple_lines(self): r = Responder(pattern=r"call.*problem", response="So sorry") output = """ You only call me when you have a problem You never call me Just to say hi """ assert list(r.submit(output)) == ["So sorry"]
def _get_github_auth_responders(): username_responder = Responder( pattern="Username for 'https://github.com':", response='{}\n'.format(github_username)) password_responder = Responder( pattern="Password for 'https://{}@github.com':".format( github_username), response='{}\n'.format(github_password)) return [username_responder, password_responder]
def auto_pick(c): boy = Responder( pattern=r"boy", response="Tom\n" ) girl = Responder( pattern=r"girl", response="Alice\n" ) c.run("echo boy; read answer; echo $answer; echo girl; read answer; echo $answer", watchers=[boy, girl]) c.run("echo girl; read answer; echo $answer; echo boy; read answer; echo $answer", watchers=[boy, girl])
def _get_github_auth_responders(): """返回Github用户密码填充器""" username_responder = Responder( pattern="Username for 'https://github.com':", response='{}\n'.format("test") ) password_responder = Responder( pattern="Password for 'https://github.com':", response='{}\n'.format("test") ) return [username_responder, password_responder]
def both_streams(self): watchers = [ Responder("standard out", "with it\n"), Responder("standard error", "between chair and keyboard\n"), ] run( "python -u respond_both.py", watchers=watchers, hide=True, timeout=5, )
def _get_github_auth_responders(c): """ 返回 GitHub 用户名密码自动填充器 """ username_responder = Responder( pattern="Username for 'https://github.com':", response='{}\n'.format(c.github_username)) password_responder = Responder( pattern="Password for 'https://{}@github.com':".format( c.github_username), response='{}\n'.format(c.github_password)) return [username_responder, password_responder]
def _get_github_auth_responders(): """ 返回 GitHub 用户名密码自动填充器 """ username_responder = Responder( pattern="Username for 'https://github.com':", response=f'{github_username}\n' ) password_responder = Responder( pattern=f"Password for 'https://{github_username}@github.com':", response=f'{github_password}\n' ) return [username_responder, password_responder]
def _create_responder(self, su=False): if su: # Responder with SU credentials self.responder = Responder( pattern=r'Password:'******'{}\n'.format(self.device.root_password), ) return self.responder else: # responder requires sudo password self.responder = Responder( pattern=r'\[sudo\] password for *', response='{}\n'.format(self.device.password), ) return self.responder
def passd(i): sudopass = Responder( pattern= r'\[sudo\] password for sasuke:', #terminal output pattern matching response='helloworld\n', ) #password i.run('sudo apt update', pty=True, watchers=[sudopass])
def execute_ssh_cmd(self, cmd): self.logger.debug("ssh cmd: %s ", cmd) try: with Connection( self.host, user=self.ssh_user, connect_kwargs={ #"key_filename": self.ssh_key, "password": self.ssh_pass, }) as c: sudopass = Responder(pattern=r'\[sudo\] password for ' + self.ssh_user + ':', response=self.sudo_pass + '\n') result = c.run(cmd, pty=True, watchers=[sudopass], warn=True, hide=True) if result and result.exited == 0 and result.ok: self.logger.debug('SSH executed fine. %s for node: %s', cmd, self.host) return True else: self.logger.debug('%s', result) return result.stdout.strip() except IOError as e: self.logger.error("Cannot connect to node: %s via network. %s", self.host, e) return None except ValueError as e: self.logger.error("Wrong parameter for fabric lib. %s", e) return None except Exception as e: self.logger.error('Command get exception: %s', e) return None
def clean_chain_data(self): try: with Connection( self.host, user=self.ssh_user, connect_kwargs={ #"key_filename": self.ssh_key, "password": self.ssh_pass }) as c: sudopass = Responder(pattern=r'\[sudo\] password for ' + self.ssh_user + ':', response=self.sudo_pass + '\n') result = c.run('sudo rm -rf {}'.format( DEPLOYMENT_DIR.format(self.ssh_user)), pty=True, watchers=[sudopass], warn=True, hide=True) if result and result.exited == 0 and result.ok: self.logger.info('chain data cleaned. %s', self.host) else: self.logger.error('chain data cleaning failed. %s', self.host) except Exception as e: self.logger.error("cannot clean chain data. %s, %s.", self.host, e) return False return True
def stop_client(self): try: with Connection( self.host, user=self.ssh_user, connect_kwargs={ #"key_filename": self.ssh_key, "password": self.ssh_pass }) as c: sudopass = Responder(pattern=r'\[sudo\] password for ' + self.ssh_user + ':', response=self.sudo_pass + '\n') #cmd = self.generate_stop_cmd() cmd = SYSTEMD_STOP_CLIENT result = c.run(cmd, pty=True, watchers=[sudopass], warn=True, hide=True) if result and result.exited == 0 and result.ok: self.logger.info('system service stopped. %s', self.host) self.client_stopped = True return True else: self.logger.error('system service stopping failed. %s', self.host) except Exception as e: self.logger.error("cannot stop client, %s, %s", self.host, e) return False
def load_systemd_file(self): try: with Connection( self.host, user=self.ssh_user, connect_kwargs={ #"key_filename": self.ssh_key, "password": self.ssh_pass }) as c: sudopass = Responder(pattern=r'\[sudo\] password for ' + self.ssh_user + ':', response=self.sudo_pass + '\n') src = '/home/{}/network-data/{}/autonity.service'.format( self.ssh_user, self.host) result = c.run('sudo cp {} {}'.format(src, SYSTEM_SERVICE_DIR), pty=True, watchers=[sudopass], warn=True, hide=True) if result and result.exited == 0 and result.ok: self.logger.info('system service loaded. %s', self.host) else: self.logger.error('systemd file loading failed. %s', self.host) except Exception as e: self.logger.error("cannot load systemd file. %s, %s", self.host, e)
def deploy(c): password = c.config['connect_kwargs']['password'] + '\n' sudopass = Responder( pattern=r'\[sudo\] password:'******'systemctl stop gunicorn_djremo ', pty=True, watchers=[sudopass]) with tarfile.open('djremo.tar.gz', 'w:gz') as tar: for root, dirs, files in os.walk('.'): for file in files: tar.add(os.path.join(root, file), filter=filter_function) c.run('mkdir -p temp_djremo') c.put('djremo.tar.gz', 'temp_djremo') c.local('del djremo.tar.gz') c.run('mkdir -p djremo') c.run('tar -xzvf temp_djremo/djremo.tar.gz -C djremo') c.run('rm -rf temp_djremo') c.run('''. ./env/bin/activate cd djremo pip install -r requirements.txt python manage.py migrate ''', pty=True) c.sudo('systemctl start gunicorn_djremo ', pty=True, watchers=[sudopass])
def test_response(c): responder = Responder( pattern=r"Enter your license:", response="/home/test/Downloads/Foglight.license\n", ) c.run("python3.7 target.py", watchers=[responder])
def import_snapshot(ctx, source_profile, snapshot_name=None): snapshot_file_path = download_snapshot(snapshot_name, ctx.config.aws.search_content_bucket, source_profile) database = ctx.config.postgres.database # Setup auto-responser. # Administrative postgres user on localhost will always be postgres (depends on POSTGRES_USER environment variable) postgres_password = ctx.config.secrets.postgres.password postgres_password_responder = Responder(pattern="Password", response=postgres_password + "\n") print("Importing snapshot") ctx.run(f"psql -h localhost -U postgres -d {database} -f {snapshot_file_path}", pty=True, watchers=[postgres_password_responder]) print("creating superuser") admin_password = ctx.config.secrets.django.admin_password harvester_key = ctx.config.secrets.harvester.api_key insert_user = insert_django_user_statement( "supersurf", admin_password, harvester_key, is_search_service=True ) ctx.run( f'psql -h localhost -U postgres -d {database} -W -c "{insert_user}"', echo=True, pty=True, warn=True, watchers=[postgres_password_responder], ) print("Cleanup") ctx.run(f"rm {snapshot_file_path}.bak", warn=True)
def createUser(ip, user, passwd, username, password, dirname=None): if not dirname: dirname = username responder = Responder( pattern=r'(Retype)?(N|n)?ew password:'******'{password}\n', ) command = "useradd -m -d /home/" + dirname + "/ -g users -s /bin/bash " + username passwdCmd = "passwd " + username try: with Connection(host=ip, user=user, connect_kwargs={"password": passwd}) as conn: conn.sudo(command, password=passwd, hide=True).stdout.strip() val2 = conn.sudo(passwdCmd, password=passwd, hide=True, watchers=[responder]).exited if val2 == 0: return ('User Created: ' + username, 0) else: return ('Cannot create user', 1) except Exception as e: return ('User already Exists', 1)
def unzip(self): """ 解压 :return: """ responder = Responder( pattern=r".*", response="A\n", ) cli.info("unzip {} -d {}".format(path.join(self.zip_process_path, self.zipfile_newname), self.zip_process_path)) result = self.local_connection.local("unzip {} -d {}".format(path.join(self.zip_process_path, self.zipfile_newname), self.zip_process_path), pty=True) # type:Result # if result.ok: # cli.info("解压完成") # elif result.failed: # cli.error("解压出错了") # cli.error(result.stderr + "\n") # exit(result.return_code) # 压缩包解压后的名字按命名规范重命名 cli.info("mv {} {}".format(path.join(self.zip_process_path, self.zipfile_root_foldername), path.join(self.zip_process_path, self.zipfile_newname[:-4]) )) self.local_connection.local("mv {} {}".format(path.join(self.zip_process_path, self.zipfile_root_foldername), path.join(self.zip_process_path, self.zipfile_newname)[:-4] ))
def __init__(self, sddc_server: str, sddc_user: str, sddc_pass: str, verbose: bool = False): # Variables self.sddc_server = sddc_server self.sddc_user = sddc_user self.sddc_pass = sddc_pass self.verbose = verbose # Parameters self.java_store = '/etc/alternatives/jre/lib/security/cacerts' self.java_store_password = '******' self.common_store = '/etc/vmware/vcf/commonsvcs/trusted_certificates.store' self.common_key = '/etc/vmware/vcf/commonsvcs/trusted_certificates.key' # Create SDDC manager SSH connection self.sddc_ssh = Connection(host=self.sddc_server, user=self.sddc_user, connect_kwargs={"password": self.sddc_pass}) self.sddc_su = Responder(pattern=r'Password:'******'{self.sddc_pass}\n') # Get certificate store password cmd = f'cat {self.common_key}' result = self.__su_cmd(cmd) self.common_store_password = result.stdout
def _command_helper(self, cmd, sudo, responder): try: if sudo: # CLASS_LOG.debug("sudo command") if responder: resp = Responder( pattern=r'\[sudo\] password for *', response='{}\n'.format(self.device.password), ) self.result = self.connection.sudo( self.device.cmd_format(cmd), pty=True, watchers=[resp]) else: self.result = self.connection.sudo( self.device.cmd_format(cmd)) else: if responder: resp = self._create_responder() self.result = self.connection.run( self.device.cmd_format(cmd), pty=True, watchers=[resp]) else: self.result = self.connection.run( self.device.cmd_format(cmd)) CLASS_LOG.debug( "Finished command helper. Obtained result: {}".format( self.result)) except UnexpectedExit: self.stderr = "Unexpected exit probably expecting a responder" CLASS_LOG.critical( "Unexpected exit probably expecting a responder") return self.result
def deliver_package(self): try: with Connection( self.host, user=self.ssh_user, connect_kwargs={ #"key_filename": self.ssh_key, "password": self.ssh_pass }) as c: sudopass = Responder(pattern=r'\[sudo\] password for ' + self.ssh_user + ':', response=self.sudo_pass + '\n') c.put(PACKAGE_NAME.format(self.host), REMOTE_NAME.format(self.ssh_user, self.host)) self.logger.info('Chain package was uploaded to %s.', self.host) result = c.run('sudo tar -C /home/{} -zxvf {}'.format( self.ssh_user, REMOTE_NAME.format(self.ssh_user, self.host)), pty=True, watchers=[sudopass], warn=True, hide=True) if result and result.exited == 0 and result.ok: self.logger.info('Chain package was unpacked to %s.', self.host) return True else: self.logger.error('chain package fail to unpacked to %s.', self.host) except Exception as e: self.logger.error("cannot deliver package to host. %s, %s", self.host, e) return False
def resize(host_start, host_end, site): """ Gathers sudo password, connects to host(s) and resizes the completely used 3TB of /dev/vg0/var_data. It then prints the final disk sizes and physical volume sizes to confirm the newly freed space. """ # TODO: This kills the partitions, fix it sudo_pass = getpass("Env Password: "******"Do you really want to reduce vg0/var_data? \[y\/n\]:", response="y\n") for i in range(int(host_start), (host_end + 1)): conn = Connection(f"vm{i}.product.{site}.domain.com", config=config) conn.sudo("systemctl stop node_exporter.service") conn.sudo("mv /var/data/node_exporter /var/tmp/") conn.sudo("umount /var/data") conn.sudo("e2fsck -f /dev/vg0/var_data") conn.sudo("resize2fs /dev/vg0/var_data 100G") conn.sudo("lvreduce -L 100G /dev/vg0/var_data", pty=True, watchers=[confirm]) conn.sudo("mount /var/data") conn.sudo("mv /var/tmp/node_exporter /var/data") conn.sudo("systemctl start node_exporter.service") print(run("df -h", shell=True), "\n", run("pvs", shell=True)) return None
def base_case(self): # Basic "doesn't explode" test: respond.py will exit nonzero unless # this works, causing a Failure. watcher = Responder(r"What's the password\?", "Rosebud\n") # Gotta give -u or Python will line-buffer its stdout, so we'll # never actually see the prompt. run("python -u respond_base.py", watchers=[watcher], hide=True)
def upload_archive(): print('uploading...') # establishing connection to the server c = Connection( host="178.128.193.27", user="******", connect_kwargs={ "key_filename": "D:\projects\doggo\.ssh\openSSH", }, ) # uploading the zip to myproject directory c.put(zipName, 'myproject/') print('exctracting...') # unziping and overwriting the files c.run('cd myproject && unzip -o %s' % zipName) # removing the archive c.run('cd myproject && rm %s' % zipName) # does server restart correctly? # sudo -S -p '[sudo] password: '******'\[sudo\] password:'******'1992\n', ) # does not work : c.config.sudo.password('1992') print('restarting...') c.sudo('systemctl restart myproject', pty=True, watchers=[sudopass])
def add_PPAs(c, ppas): c.sudo('rm -f /etc/apt/sources.list.d/*') press_ENTER_responder = Responder(pattern=r'ENTER', response='\n') for ppa in ppas: c.sudo(f'add-apt-repository ppa:{ppa}', watchers=[press_ENTER_responder]) c.sudo('apt-get update')
def __init__(self, name, pattern=None, response=None): """__init__ Under Construction. """ # Override these in the parent class... self.name = name self.responder = Responder(pattern=pattern, response=response)
def prompt(c): name = '/tmp/abc' responder = Responder( pattern=r"rm: remove directory ' + name + '? ", response="y\n", ) c.run("mkdir -p " + name) c.run("rm -r " + name, watchers=[responder])
def yields_response_when_regular_string_pattern_seen(self): r = Responder(pattern="empty", response="handed") assert list(r.submit("the house was empty")) == ["handed"]
def multiple_hits_within_stream_yield_multiple_responses(self): r = Responder(pattern="jump", response="how high?") assert list(r.submit("jump, wait, jump, wait")) == ["how high?"] * 2
def yields_response_when_regex_seen(self): r = Responder(pattern=r"tech.*debt", response="pay it down") response = r.submit("technically, it's still debt") assert list(response) == ["pay it down"]
def yields_response_when_regular_string_pattern_seen(self): r = Responder(pattern='empty', response='handed') eq_(list(r.submit('the house was empty')), ['handed'])
def multiple_hits_within_stream_yield_multiple_responses(self): r = Responder(pattern='jump', response='how high?') eq_(list(r.submit('jump, wait, jump, wait')), ['how high?'] * 2)
def yields_response_when_regex_seen(self): r = Responder(pattern=r'tech.*debt', response='pay it down') eq_(list(r.submit("technically, it's still debt")), ['pay it down'])