def change_link(*args, **kwargs): target_link = kwargs.get('link', 'T33L') version = kwargs.get('version') if kwargs.get('server').startswith('192'): gateway_session = SSHSession('173.36.203.62', 'wbxbuilds', password='******').open() print("Connected to sshlogin server.") remote_session = gateway_session.get_remote_session( kwargs.get('server'), kwargs.get('user', 'shuqli'), password=kwargs.get('pass', 'wbx@Aa$hfcm')) print(f"Connected to target server {kwargs.get('server')}.") else: remote_session = SSHSession(kwargs.get('server'), kwargs.get('user', 'wbxroot'), password=kwargs.get('pass', 'wbx@AaR00t')).open() print("Starting to change link ...") remote_session.get_cmd_output( "cd /www/htdocs/client; sudo rm -f {target_link}; sudo ln -sf " "`ls -lrt|grep WBXclient-{version}|grep ^d|tail -n1|awk '{{" "print $NF}}'` {target_link}; sudo chown -h nobody.nobody " "{target_link};".format(version=version, target_link=target_link)) ret = remote_session.get_exit_code( f"test -e /www/htdocs/client/{target_link}") print(ret) if ret == 0: return True else: return False
def search_atlas(ra, dec, Ryan = True): atlas_info = lc_cfg['atlas_info'] if Ryan: remote_session = SSHSession(atlas_info[3]['address'],atlas_info[3]['username']).open() else: gateway_session1 = SSHSession(atlas_info[2]['address'],atlas_info[2]['username'], password=atlas_info[2]['password']).open() if len(atlas_info)==3: gateway_session2 = gateway_session1.get_remote_session(atlas_info[1]['address'],atlas_info[1]['username'], password=atlas_info[2]['password']) remote_session = gateway_session2.get_remote_session(atlas_info[2]['address'], password=atlas_info[2]['password']) elif len(atlas_info)==2: remote_session = gateway_session1.get_remote_session(atlas_info[1]['address'], password=atlas_info[1]['password']) else: print('wrong format for atlas_params: too many jump connection') today = dt.today() con = sqlite3.connect(":memory:") tdate = ' '+str(list(con.execute("select julianday('"+today.strftime("%Y-%m-%d")+"')"))[0][0]-lc_cfg['lookback_days']-2400000) result=remote_session.get_cmd_output('./mod_force.sh '+str(ra)+' '+ str(dec)+tdate) atlas_lc = at.Table(names=('jd','mag', 'mag_err','flux', 'fluxerr', 'filter','maj', 'min', 'apfit', 'sky', 'zp', 'zpsys'), dtype=('f8', 'f8', 'f8', 'f8', 'f8', 'S1' , 'f8', 'f8', 'f8', 'f8','f8', 'U8')) split = result.split('\r\n') for i in split[1:]: k = i.split() # if int(k[3])>int(k[4]): atlas_lc.add_row([float(k[0]), float(k[1]), float(k[2]), float(k[3]), float(k[4]), k[5], float(k[12]), float(k[13]), float(k[15]), float(k[16]), float(k[17]), 'ab']) return atlas_lc
def test_get_cmd_output(docker_env): gateway_ip, gateway_port = docker_env.get_host_ip_port('gateway') gateway_session = SSHSession(host=gateway_ip, port=gateway_port, username='******', password='******').open() assert gateway_session.get_cmd_output('hostname') == 'gateway.example.com'
def get_cert_expiration_date(host_session: SSHSession, certificate_file: CertificateFile, ) -> datetime: try: date_from_host = host_session.get_cmd_output( f'openssl x509 -enddate -noout -in "{certificate_file.file_path}" |cut -d= -f 2)') return convert_pem_date_to_datetime(date_from_host) except Exception as e: print("Could not get certificate expiration date") raise e
def get_host_certs_list(host_session: SSHSession, certs_path: str, ) -> list: try: certs_list = [] host_certs_output_list = (host_session.get_cmd_output(f'find {certs_path} -name "*.cert.pem"')).split('\r\n') for cert_path in host_certs_output_list: certs_list.append(CertificateFile(cert_path,host_session.username, host_session.host)) return certs_list except Exception as e: print("Could not get hosts certificates list") raise e
def login(): for widget in frame.winfo_children(): widget.destroy() gatewaySession = SSHSession('users.isi.deterlab.net', entryUsername.get(), password=entryPassword.get()).open() ltmap = gatewaySession.get_cmd_output('cat /proj/' + entryProject.get() + '/exp/' + entryExperiment.get() + '/tbdata/ltmap') if not os.path.isfile('img/' + entryExperiment.get() + '.png'): gatewaySession.get(remote_path='/proj/' + entryProject.get() + '/exp/' + entryExperiment.get() + '/tbdata/' + entryExperiment.get() + '.png', local_path='img/') experimentLabel = tk.Label(frame, text="Experiment: " + entryExperiment.get() + " Project: " + entryProject.get(), bg="white", pady=30) experimentLabel.pack() for line in ltmap.split('\n'): #check if first character is h count = 1 if line[0] == 'h': nodeName=line[line.find(' ')+1:-1] label = tk.Label(frame, text="Node " + nodeName, bg="gray") label.pack() buttonNew = tk.Button( frame, text="Login to " + nodeName, width=15, padx=10, pady=5, #height=5 command=partial(sshToNode, nodeName) ) buttonNew.pack() topologyLabel = tk.Label(frame, text="Topology image:", pady=5) topologyLabel.pack() image = Image.open("img/" + entryExperiment.get() + ".png") img = ImageTk.PhotoImage(image) panel = tk.Label(frame, image=img) panel.pack() root.mainloop()
def test_input_data(docker_env): gateway_ip, gateway_port = docker_env.get_host_ip_port('gateway') gateway_session = SSHSession(host=gateway_ip, port=gateway_port, username='******', password='******').open() commands = [ 'read -p "Requesting user input value?" my_var', 'echo $my_var' ] # without input given, command will hang until timeout is reached with pytest.raises(exception.TimeoutError): gateway_session.run_cmd(commands, timeout=5) # with input given, command should run correctly and return the value entered assert gateway_session.get_cmd_output(commands, input_data={ 'Requesting user input value': 'dummy_value' }).split()[-1] == "dummy_value"
# from netmiko import ConnectHandler import getpass # import re device_ip = input("What is the IP of the device? > ") username = input("Username: "******"Password: "******"What do you want to name the file? > ") # remote_ip = "10.1.0.240" # remote_ip = input("What IP do you want to get to? > ") # device_command = "show ip int brief" device_command = input("What command do you want to run? > ") # establish ssh connection between your local machine and the jump server gateway_session = SSHSession(device_ip, username, password=password).open() # print("Connected to TACOBOX") # print(gateway_session.get_cmd_output(device_command)) # from jump server, establish connection with a remote server # remote_session = gateway_session.get_remote_session(remote_ip, password=password, retry=3, retry_interval=10) # command will be executed remotely and output will be returned locally and printed # print(remote_session.get_cmd_output(device_command)) file = open(file_name, 'w') file.write(gateway_session.get_cmd_output(device_command)) file.close() print("\nThis task has been completed...\n")