def exec_ssh(host, user, key, ssh_commands): shell = spur.SshShell( hostname=host, username=user, missing_host_key=spur.ssh.MissingHostKey.accept) with shell: for ssh_command in ssh_commands: logging.debug('Host - %s: Command - %s', host, ssh_command) try: shell.run(["bash", "-c", ssh_command]) except spur.results.RunProcessError as exception: logging.error( ssh_command + " - error: " + traceback.format_exc(exception))
def connect_shell(): try: shell = spur.SshShell(hostname="localhost", \ username="******", password="******", \ missing_host_key=spur.ssh.MissingHostKey.accept) with shell: #result = shell.run("sh", "-c", "python ./tf-hpo/src/cnn_layer2_multi.py -h") process = shell.spawn("sh", "-c", "echo hello") result = process.wait_for_result() print result.output except spur.ssh.ConnectionError as error: print error.original_traceback raise
def install_certificates(shell=None, localshell=None, target_host=None, domain_name=None): shell = spur.SshShell(hostname=target_host, username="******") cmd = "wget https://dl.eff.org/certbot-auto".format() result = shell.run(cmd.split(), cwd=".", allow_error=False) cmd = "chmod a+x ./certbot-auto".format() result = shell.run(cmd.split(), cwd=".", allow_error=False) cmd = "yes | ./certbot-auto certonly --standalone -d {} -m [email protected] --agree-tos".format( domain_name) result = shell.run(cmd.split(), cwd=".", allow_error=False)
def test_1_reset_reinitialize(self): try: self.login_admin() self.element_clickable("click maintain button", "//div[2]/div/div/div/div/div/div/div/div/div/div/div[8]/div/div/img") self.element_clickable("choose system recovery", "//div[2]/div/a[2]/span/span/span[2]") self.element_clickable("choose radio reset_to_default", "//div[6]/div/div/div/div/div/span") self.element_clickable("click reset device button", "//div[7]/div/div/a/span/span/span[2]") sleep(3) self.element_clickable("click confirm button", "//div[3]/div/div/a[5]/span/span/span[2]") sleep(40) # check nas reboot status,give nas 3min reboot for i in range(1, 10): ping_status = ping_host(nasip) if ping_status == False: print "NAS reinitialize success!!" break else: print "NAS is alive,ping time =" + str(i) sleep(10) continue print "-----------------------------------------------------------------------------------------" print "after reset checking nas ip........" for i in range(1, 10): reset_after_ip = get_ip_address() print "reset_after_ip =" + str(reset_after_ip) reboot_ping = ping_host(reset_after_ip) if reboot_ping == True: print "NAS reset success!!" break else: print "NAS is restarting...." sleep(10) continue shell = spur.SshShell(hostname=reset_after_ip, username="******", password="******", port=2222, missing_host_key=spur.ssh.MissingHostKey.accept) result = shell.run(["/usr/local/sbin/zpool", "list"]) result_string = result.output if len(result_string) == 19: reinit_status = True print "reset success!!" else: reinit_status = False print "reset fail" except Exception as error: print error self.assertTrue(reinit_status==True,msg="Test success!!")
def rerun_filebeats_container(shell, localshell, target_host): shell = spur.SshShell(hostname=target_host, username="******") #, password="******") # cmd = ( # "docker run -v /root/filebeat.yml:/usr/share/filebeat/filebeat.yml " # "-v /var/lib/docker/containers:/usr/share/filebeat/containers_from_host:Z " # "-v /var/log:/usr/share/filebeat/logs_from_host:Z " # "docker.elastic.co/beats/filebeat:6.2.1" # ) cmd = ( "docker run -v /root/filebeat.yml:/usr/share/filebeat/filebeat.yml " # "-v /var/lib/docker/containers:/usr/share/filebeat/containers_from_host:ro " # "-v /var/log:/usr/share/filebeat/logs_from_host:ro " "-v /root/fakefile.log:/usr/share/filebeat/log_test/fakefile.log " "docker.elastic.co/beats/filebeat:6.2.1") result = shell.run(cmd.split(), cwd=".", allow_error=False)
def source_set_zero(): # load credentials with open("../config.json") as f: config = json.load(f) ipa = config["cage_rpi_ipa"] usr = config["cage_rpi_usr"] pwd = config["cage_rpi_pwd"] shell = spur.SshShell(hostname=ipa, username=usr, password=pwd) with shell: result = shell.run(["python3", "encoders/set_zero_source.py"]) ans = float(result.output.decode("utf-8")) print("Encoder set to zero, returned: ", ans) return ans
def udm_ssh(): try: with spur.SshShell(hostname=udm_creds['hostname'], username=udm_creds['username'], password=udm_creds['password'], connect_timeout=6) as shell: if "wpa_supplicant" in str(shell.run(["podman", "ps"]).output): print("wpa_supplicant is already running") return shell.run( ["podman", "container", "start", "wpa_supplicant-udmpro"]) print("Launched wpa_supplicant") return except Exception as e: print(e) return
def button_solr_post_callback(): """ what to do when the "Post //fileserver/files/? Files to Solr" button is pressed """ target = entry.get() statusText.set("/opt/solr/bin/post for {} is running on fileserver...".format(target)) message.configure(fg="red") message.update() fileserver = "192.168.1.24" shell = spur.SshShell(hostname="fileserver", username="******", missing_host_key=spur.ssh.MissingHostKey.warn) target_path = "/files/{}".format(target) result = shell.run(["/opt/solr/bin/post", "-c", "fs-core", target_path]) # result = shell.run(["ls", "-al", "/."]) statusText.set("/opt/solr/bin/post on fileserver is complete with a return code of {}".format(result.return_code)) message.configure(fg="dark green")
def run(self, **kwargs): shell = spur.SshShell( hostname=kwargs['hostname'], port=int(kwargs['port']), username=kwargs['username'], password=kwargs['password'], connect_timeout=kwargs['timeout'], missing_host_key=spur.ssh.MissingHostKey.accept ) result = {} with shell: try: result = shell.run(kwargs['command'].split()) except: raise else: return result.output
def parallelize(nodes_list, all_runs_args, run_script, on_gpu=False, dry_run=False): """ Running on a list of given servers, a bunch of experiments. Assumes that can connect automatically to the servers :param nodes_list: :param all_runs_args: :param run_script: :param on_gpu: :param dry_run: allows to simply print the intended experiments, and not actually run them :return: """ # assumes automatic connection w/o password connections = [ spur.SshShell(hostname=node, username="******") for node in nodes_list ] # ┌──────────────┐ # │ execute tasks│ # └──────────────┘ for sub_exp_idx, combination in enumerate(all_runs_args): args_str = f"{ts} sh {run_script}" for item in combination: args_str += f" {item}" if on_gpu: gpu_id = sub_exp_idx % 4 args_str += f" cuda:0" node_id = sub_exp_idx // 4 % len(nodes_list) env['CUDA_VISIBLE_DEVICES'] = f"{gpu_id}" print(args_str.split(" "), node_id, gpu_id) else: node_id = sub_exp_idx % len(nodes_list) print(args_str.split(" "), node_id) if not dry_run: connections[node_id].run(args_str.split(" "), update_env=env) print(f"==> running {len(all_runs_args)} experiments")
def exec_ssh(host, user, key, ssh_commands): command_exe_status = True result = None shell = spur.SshShell(hostname=host, username=user, private_key_file=key, missing_host_key=spur.ssh.MissingHostKey.accept) with shell: for ssh_command in ssh_commands: LOGGER.debug('Host - %s: Command - %s', host, ssh_command) try: result = shell.run(["bash", "-c", ssh_command]).output except spur.results.RunProcessError as exception: LOGGER.error(ssh_command + " - error: " + traceback.format_exc(exception)) command_exe_status = False return (command_exe_status, result)
def connect_to_cluster(self, create=False): try: self.cluster_shell = spur.SshShell( hostname=self.cluster_ip, username=cluster_username, password=cluster_password, missing_host_key=spur.ssh.MissingHostKey.accept) self.print_to_console("Connecting to MPI Cluster") if create: self.update_p2c() self.activate_tool(self.supported_tools) while True: try: self.print_to_console("Updating apt-get") command = "sudo apt-get update" zip_shell = self.cluster_shell.spawn( ["sh", "-c", command], use_pty=True) zip_shell.stdin_write(cluster_password + "\n") self.print_to_console( zip_shell.wait_for_result().output) self.print_to_console("Installing zip") command = "sudo apt-get install zip -y" zip_shell = self.cluster_shell.spawn( ["sh", "-c", command], use_pty=True) zip_shell.stdin_write(cluster_password + "\n") # zip_shell.stdin_write("Y\n") self.print_to_console( zip_shell.wait_for_result().output) break except spur.RunProcessError as err: if err.return_code == -1: # no return code received self.logger.error( self.log_prefix + 'No response from server. Retrying command ({0})' .format(command)) else: self.logger.error(self.log_prefix + 'RuntimeError: ' + err.message) except spur.ssh.ConnectionError as err: self.print_to_console("Error: Failed to connect to MPI cluster.") # self.print_to_console(sys.exc_info()) self.print_to_console(err.args) raise
def make_socket(remote): shell = spur.SshShell( hostname=remote, username=user, missing_host_key=spur.ssh.MissingHostKey.accept) try: shell.run(shlex.split("""which lttng"""), allow_error=False) except spur.results.RunProcessError as rpe: print("lttng not found on machine {m}".format(m=remote)) raise rpe #try: # shell.run(shlex.split("""id | grep -q tracing"""), allow_error=False) #except spur.results.RunProcessError as rpe: # print("Couldn't check for tracing group membership for '{usr}'. Got {exp}".format(usr=user, exp=rpe)) # raise rpe return shell
def remote_command(self, hostname, command_list): #TODO(Yasumoto): Since there are many calls to the same host, consider memoizing the connection and # re-using on subsequent executions shell = spur.SshShell(hostname=hostname, username=self.username, password=self.password, missing_host_key=spur.ssh.MissingHostKey.warn) result = None with shell: result = shell.run(command_list, allow_error=True) if result is not None: if result.return_code is not 0: raise self.RemoteExecutionError( "Error code %d when connecting to %s: %s" % (result.return_code, hostname, result.stderr_output)) return result.output raise self.RemoteExecutionError('Did not successfully run on %s!' % hostname)
def get_ssh(): try: ssh = spur.SshShell(hostname=get_arg('server[ip]'), port=get_arg('server[port]'), username=get_arg('server[username]'), password=get_arg('server[password]'), missing_host_key=spur.ssh.MissingHostKey.accept, connect_timeout=60) except: return None try: ssh.run(['echo', '-n', 'hello']) except: return None return ssh
def setup_remotehost(config): shell = spur.SshShell(hostname=config['host'], username=config['ssh_username'], private_key_file=config['ssh_pem_file'], missing_host_key=spur.ssh.MissingHostKey.accept) with shell: for ssh_command in config['ssh_commands']: logging.info('Host - %s: Command - %s', config['host'], ssh_command) try: result = shell.run(ssh_command) logging.debug( string.join(ssh_command, " ") + " - output: " + result.output) except spur.results.RunProcessError as exception: logging.error( string.join(ssh_command, " ") + " - error: " + exception.stderr_output)
def send_key(username, host): #send key local_shell = spur.LocalShell() local_shell.run([ "scp", "-i", config.ADMIN_KEY_PATH, config.KEY_STORAGE_PATH % username, "ubuntu@%s:%s/%s.pub" % (host, config.ADMIN_HOME, username) ]) shell = spur.SshShell(hostname=host, username=config.ADMIN_USERNAME, private_key_file=config.ADMIN_KEY_PATH, missing_host_key=spur.ssh.MissingHostKey.warn) # create user shell.run(["sudo", "useradd", "-m", username]) shell.run(["sudo", "usermod", "-aG", "sudo", username]) shell.run(["sudo", "mkdir", "/home/%s/.ssh" % username]) # save key shell.run([ "sudo", "sh", "-c", "cat %s/%s.pub >> /home/%s/.ssh/authorized_keys" % (config.ADMIN_HOME, username, username) ]) shell.run([ "sudo", "chown", "-R", "%s:%s" % (username, username), "/home/%s/.ssh" % username ]) shell.run( ["sudo", "chmod", "600", "/home/%s/.ssh/authorized_keys" % username]) shell.run(["sudo", "chmod", "700", "/home/%s/.ssh" % username]) # configure server shell.run(["sudo", "chsh", "-s", "/bin/bash", username]) local_shell.run([ "scp", "-i", config.KEY_STORAGE_PATH % username, config.VIMRC_PATH, "%s@%s:~/.vimrc" % (username, host) ]) local_shell.run([ "scp", "-i", config.KEY_STORAGE_PATH % username, config.BASHRC_PATH, "%s@%s:~/.bashrc" % (username, host) ])
def check_servers_running(env): ''' Identify all servers running ''' shell = spur.SshShell(hostname=env.ssh_host, username=env.ssh_usr, password=env.ssh_pwd) result = shell.run(["ps", "auxww"]) serversRunning = [] for line in str(result.output).split(r'\n'): match = re.findall(r'(oracle\s+\d+\s).+(-Dweblogic.Name=\w+)', line) if match: serverPid = re.sub(r'[^0-9]', '', str(match[0][0])) serverName = re.sub(r'-Dweblogic.Name=', '', str(match[0][1])) serversRunning.append((serverName, serverPid)) serversRunning.sort() return serversRunning
def test_0_fw_upgrade_status(self): ip = get_ip_address() shell = spur.SshShell(hostname=ip, username="******", password="******", port=2222, missing_host_key=spur.ssh.MissingHostKey.accept) result = shell.run(["/bin/uname", "-a"]) result_string = result.output date = result_string[40:48] print "FW date =" + date systime = time.strftime("%Y%m%d") print systime compare_result = string_compare(date, systime) if compare_result == True: print "FW upgrade success" else: print "FW upgrade fail" pytest.exit("close the program") self.assertTrue(compare_result==True)
def startDirectBF(self): '''test direct BF''' shell = spur.SshShell(hostname="192.168.110.239", username="******", password="******") #TODO: loop test? with shell: result = shell.run( ["cat", "/proc/net/rtl88x2bu/wlan3/rate_search"]) print(result) # 1. trigger rate search result = shell.run( ["echo", "1", ">", "/proc/net/rtl88x2bu/wlan3/rate_search"]) #start ping process = shell.spawn( ["sudo", "alpha_ping", "-n 1300", "192.168.0.20"], store_pid=True) #wait finish search (2min) ''' waittime=0 while proc.is_running(): print(".") time.sleep(1) waittime=waittime+1 ''' result = process.wait_for_result() print(result.output) print("wait search finish") result = shell.run( ["cat", "/proc/net/rtl88x2bu/wlan3/rate_search"]) while "=1" in result.decode(): print(".") time.sleep(5) result = shell.run( ["cat", "/proc/net/rtl88x2bu/wlan3/rate_search"]) # 2. record rate_search_result, pathb_phase result = shell.run( ["cat", "/proc/net/rtl88x2bu/wlan3/rate_search_result"]) print("search_result: %s" % result) result = shell.run( ["cat", "/proc/net/rtl88x2bu/wlan3/pathb_phase"]) print("pathb_phase: %s" % result)
def display(self, stimuli_file_name: str) -> None: """Displays the specified stimuli on the screen.""" shell = spur.SshShell( hostname=self.address, username="******", missing_host_key=spur.ssh.MissingHostKey.accept, load_system_host_keys=False, ) with shell: shell.spawn( [ "feh", "-F", f"{self.remote_image_directory}/{stimuli_file_name}", "&", ], update_env={"DISPLAY": ":0"}, store_pid=True, )
def clean_server(env): print('Process start') #Stop servers pythonScript = f'{os.path.dirname(os.path.realpath(__file__))}/actions/stop_servers.py' subprocess.run([ './wlst.sh', f'{env.oracle_home}/wlserver/server/bin', pythonScript, f'"{env.__dict__}"' ]) # MOVE date_now = datetime.now() pattern_folder_old = "{:%Y%m}".format(date_now) pattern_folder_new = "{:%Y%m%d%H%M}".format(date_now) shell = spur.SshShell(hostname=env.ssh_host, username=env.ssh_usr, password=env.ssh_pwd) ssh_session = shell._connect_ssh() for node in env.weblogic_nodes: node_address = f'{env.weblogic_domain_dir}/servers/{node}' node_address_backup = f'{node_address}_{pattern_folder_new}' node_address_remove = f'{node_address}_{pattern_folder_old}*' cmd_on_server = f'rm -rf {node_address_remove} && mv {node_address} {node_address_backup}' print() print(f'CMD ON SERVER: {cmd_on_server}') ssh_session.exec_command(cmd_on_server) ssh_session.close() print() print() #Start servers pythonScript = f'{os.path.dirname(os.path.realpath(__file__))}/actions/start_servers.py' subprocess.run([ './wlst.sh', f'{env.oracle_home}/wlserver/server/bin', pythonScript, f'"{env.__dict__}"' ]) print('Process end')
def _start_proxy(self): proxy = None ts = time.time() cmd = config.proxy_cmd.format(port=str(config.proxy_com_port)) self.log.write("Proxy CMD: " + cmd + "\n") shell = spur.SshShell(hostname=mv.vm2ip(self.tc[0]), username=config.vm_user, missing_host_key=spur.ssh.MissingHostKey.accept, private_key_file=config.vm_ssh_key) #Setup NetEM try: ret = shell.run(["/bin/bash", "-i", "-c", config.limit_cmd]) self.log.write("Setting up netem:\n" + ret.output) except Exception as e: print "Setting up netem failed: " + str(e) self.log.write("Setting up netem failed: " + str(e) + "\n") return None #Start Proxy proxy = shell.spawn(["/bin/bash", "-i", "-c", cmd], store_pid=True, allow_error=True) if not proxy.is_running(): res = proxy.wait_for_result() self.log.write("Proxy Failed to Start: " + res.output + res.stderr_output) return None else: self.log.write("Started proxy on " + str(mv.vm2ip(self.tc[0])) + "...\n") #Wait for proxy to come up if (self._waitListening(mv.vm2ip(self.tc[0]), config.proxy_com_port, 240, False) == False): self.log.write("Proxy Failed to start after 240 seconds!\n") print "Proxy Failed to Start after 240 seconds!" return None self.log.write('[timer] Start proxy: %f sec.\n' % (time.time() - ts)) self.proxy_running = True return proxy
def run_cmd(self, cmd, interactive=True): try: logging.info(cmd) logging.info(config.CONFIG['host']) logging.info(config.CONFIG['host_user']) logging.info(config.CONFIG['host_password']) shell = spur.SshShell( hostname=config.CONFIG['host'], username=config.CONFIG['host_user'], password=config.CONFIG['host_password'], missing_host_key=spur.ssh.MissingHostKey.accept) with shell: if interactive: result = shell.run(cmd.split(' ')) else: result = shell.spawn(cmd.split(' ')) logging.info('Run: ' + cmd) except Exception as e: logging.warning('Run failed: ' + cmd) logging.warning(e.__doc__)
def get_connection(host, port, username, password): """ Attempts to connect to the given server and returns a connection. """ try: shell = spur.SshShell(hostname=host, username=username, password=password, port=port, missing_host_key=spur.ssh.MissingHostKey.accept, connect_timeout=10) shell.run(["echo", "connected"]) except spur.ssh.ConnectionError as e: raise WebException( "Cannot connect to {}@{}:{} with the specified password".format( username, host, port)) return shell
def stage_artifact(self, hostname, local_path, artifact_version, staging_dir): connection = spur.SshShell( hostname=hostname, username=self.username, password=self.password, missing_host_key=spur.ssh.MissingHostKey.warn) remote_artifact = os.path.join(staging_dir, local_path) remote_resting_place = os.path.join(staging_dir, str(artifact_version)) print('Copying %s to %s' % (local_path, hostname)) with connection.open(remote_artifact, 'wb') as remote_file: with open(local_path, 'rb') as local_file: shutil.copyfileobj(local_file, remote_file) print('Untar-ing %s on %s' % (remote_artifact, hostname)) self.create_directory(hostname, remote_resting_place) self.remote_command(hostname, [ '/bin/tar', '-xzf', remote_artifact, '-C', remote_resting_place, '--strip-components=1' ]) self.remote_command(hostname, ['rm', remote_artifact])
def run(self, cmd): """ :param cmd: Shell Command. list """ shell = spur.SshShell(hostname=self._hostname, username=self._username, password=self._password, port=self._port, private_key_file=self._private_key_file, connect_timeout=self._connect_timeout, missing_host_key=self._missing_host_key, shell_type=self._shell_type, load_system_host_keys=self._load_system_host_keys, sock=self._sock ) with shell: result = shell.run(cmd) return result.output
def query_encoder(rpi_pin, t_sleep=0.01, com_spd=10000, verbose=True, max_reads=3, zero=False): """ Access the RPi-side routine, "read_encoders.py" via SSH. To read an encoder position: $ python3 motor_movement.py -p [rpi_pin] [options: -s, -c, -v] To zero an encoder: $ python3 motor_movement.py -z [rpi_pin] [options: -m, -s, -c, -v] """ shell = spur.SshShell(hostname=ipconf["cage_rpi_ipa"], username=ipconf["cage_rpi_usr"], password=ipconf["cage_rpi_pwd"]) with shell: cmd = "python3 cage/motors/read_encoders.py" if not zero: cmd += f" -p {rpi_pin} -s {t_sleep} -c {com_spd}" else: cmd += f" -z {rpi_pin} -m {max_reads} -s {t_sleep} -c {com_spd}" if verbose: cmd += " -v" # send the command and decode the output tmp = shell.run(shlex.split(cmd)) ans = tmp.output.decode("utf-8") if verbose: enc_name = rpins[rpi_pin] if zero: print( f"\nZeroing {enc_name} encoder (pin {rpi_pin}).\nRPi output:") else: print( f"\nReading {enc_name} encoder position (pin {rpi_pin}).\nRPi output:" ) print(ans) return ans
def SSHscript(): shell = spur.SshShell(hostname="127.0.0.1", port=2222, username="******", password="******", missing_host_key=spur.ssh.MissingHostKey.accept) with shell: #Storing u.data into HDFS cluster shell.run([ "curl", "-O", "https://raw.githubusercontent.com/essakh/spark-application/master/Spark%20files/ml-100k/u.data" ]) #shell.run(["hadoop", "fs", "-rm", "-r", "ml-100k"]) shell.run(["hadoop", "fs", "-mkdir", "ml-100k"]) shell.run( ["hadoop", "fs", "-copyFromLocal", "u.data", "ml-100k/u.data"]) shell.run(["rm", "u.data"]) #Storing u.item onto local shell.run([ "curl", "-O", "https://raw.githubusercontent.com/essakh/spark-application/master/Spark%20files/ml-100k/u.item" ]) #shell.run(["rm", "-rf", "ml-100k"]) shell.run(["mkdir", "ml-100k"]) shell.run(["mv", "u.item", "ml-100k"]) #Storing Spark Scripts onto local shell.run([ "curl", "-O", "https://raw.githubusercontent.com/essakh/spark-application/master/Spark%20files/LowestRatedMovie.py" ]) shell.run([ "curl", "-O", "https://raw.githubusercontent.com/essakh/spark-application/master/Spark%20files/LowestRatedPopularMovie.py" ]) shell.run([ "curl", "-O", "https://raw.githubusercontent.com/essakh/spark-application/master/Spark%20files/PopularMovie.py" ])
def display_on_screen(self, stimuli_name, correct_answer): """Displays the specified stimuli on the screen. Should only be called if the SensorGrouping config is not None Parameters: stimuli_name (str): The name of the file corresponding to the desired stimuli to be displayed. correct_answer (boolean): Denotes whether this is the desired selection. """ self.correct_stimulus = correct_answer shell = spur.SshShell( hostname=self.address, username='******', missing_host_key=spur.ssh.MissingHostKey.accept, load_system_host_keys=False ) with shell: result = shell.spawn(['feh', '-F', f'''{self.config['REMOTE_IMAGE_DIRECTORY']}/{stimuli_name}''', '&'], update_env={'DISPLAY': ':0'}, store_pid=True).pid self.pid_of_previous_display_command = int(result)