github.com/platonnetwork/platon-go@v0.7.6/cases/common/connect.py (about) 1 import paramiko 2 from client_sdk_python import HTTPProvider, Web3, WebsocketProvider 3 from client_sdk_python.middleware import geth_poa_middleware 4 from client_sdk_python.utils.threads import Timeout 5 from common.log import log 6 7 8 def connect_web3(url, chain_id=100): 9 """ 10 Connect to the web3 service, add block query middleware, 11 use to implement eth_getBlockByHash, eth_getBlockByNumber, etc. 12 :param url: 13 :param chain_id: 14 :return: 15 """ 16 if "ws" in url: 17 w3 = Web3(WebsocketProvider(url), chain_id=chain_id) 18 else: 19 w3 = Web3(HTTPProvider(url), chain_id=chain_id) 20 w3.middleware_stack.inject(geth_poa_middleware, layer=0) 21 22 return w3 23 24 25 def wait_connect_web3(url, chain_id=100, timeout=20, poll_latency=0.2): 26 with Timeout(timeout) as _timeout: 27 while True: 28 web3 = connect_web3(url, chain_id) 29 if web3.isConnected(): 30 break 31 _timeout.sleep(poll_latency) 32 return web3 33 34 35 def connect_linux(ip, username='root', password='Juzhen123!', port=22): 36 """ 37 Use the account password to connect to the linux server 38 :param ip: server ip 39 :param username: username 40 :param password: password 41 :param port: 42 :return: 43 ssh:Ssh instance for executing the command ssh.exec_command(cmd) 44 sftp:File transfer instance for uploading and downloading files sftp.get(a,b)Download a to b, sftp.put(a,b) upload a to b 45 t:Connection instance for closing the connection t.close() 46 """ 47 t = paramiko.Transport((ip, port)) 48 t.connect(username=username, password=password) 49 ssh = paramiko.SSHClient() 50 ssh._transport = t 51 sftp = paramiko.SFTPClient.from_transport(t) 52 return ssh, sftp, t 53 54 55 def connect_linux_pem(ip, username, pem_path): 56 """ 57 使用秘钥连接linux服务器 58 :param ip: 59 :param username: 60 :param pem_path: 61 :return: 62 """ 63 key = paramiko.RSAKey.from_private_key_file(pem_path) 64 ssh = paramiko.SSHClient() 65 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 66 ssh.connect(ip, username=username, pkey=key) 67 t = ssh.get_transport() 68 t.set_keepalive(30) 69 sftp = paramiko.SFTPClient.from_transport(t) 70 return ssh, sftp, t 71 72 73 def run_ssh(ssh, cmd, password=None): 74 try: 75 stdin, stdout, _ = ssh.exec_command("source /etc/profile;%s" % cmd) 76 if password: 77 stdin.write(password + "\n") 78 stdout_list = stdout.readlines() 79 # if len(stdout_list): 80 # log.debug('{}:{}'.format(cmd, stdout_list)) 81 except Exception as e: 82 raise e 83 return stdout_list 84 85 86 def run_ssh_cmd(ssh, cmd, password=None, password2=None, password3=None): 87 try: 88 log.info('execute shell cmd::: {} '.format(cmd)) 89 stdin, stdout, _ = ssh.exec_command("source /etc/profile;%s" % cmd) 90 if password: 91 stdin.write(password + "\n") 92 if password2: 93 stdin.write(password2 + "\n") 94 if password3: 95 stdin.write(password3 + "\n") 96 97 stdout_list = stdout.readlines() 98 # if len(stdout_list): 99 # log.info('{}:{}'.format(cmd, stdout_list)) 100 except Exception as e: 101 raise e 102 return stdout_list