使用多线程执行这两个函数 def get_ip(): # 执行adb shell命令并输出结果 subprocess.check_output( ['adb', 'shell', 'udhcpc'] ) subprocess.check_output( ['adb', 'shell', 'udhcpc -i eth1'] ) conf = subprocess.check_output( ['adb', 'shell', 'ifconfig'] ).decode() # conf = str(ip).split(r'\r\r\n') # tmp = conf.replace( "\r\r\n", "\n" ) # print( tmp) ip = re.findall( r'addr:(.*?) Bcast', str( conf ) ) print(ip) for i in ip : speed = subprocess.check_output((['adb', 'shell', f'iperf3 -B {i} -c 192.168.102.105'])).decode() print(speed.replace("\r\r\n", "\n")) def get_ssh(): ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy) ssh.connect(hostname='192.168.102.105',port=22,username='root',password='xiayi123456',timeout=30) stdin,stdout,stderr = ssh.exec_command('iperf3 -s') print(stdout.read())
时间: 2023-12-02 22:05:19 浏览: 213
多线程扫描ip
可以使用 Python 的 `threading` 模块来实现多线程执行这两个函数。具体代码如下:
```
import threading
import subprocess
import re
import paramiko
def get_ip():
subprocess.check_output(['adb', 'shell', 'udhcpc'])
subprocess.check_output(['adb', 'shell', 'udhcpc -i eth1'])
conf = subprocess.check_output(['adb', 'shell', 'ifconfig']).decode()
ip = re.findall(r'addr:(.*?) Bcast', str(conf))
print(ip)
for i in ip:
speed = subprocess.check_output((['adb', 'shell', f'iperf3 -B {i} -c 192.168.102.105'])).decode()
print(speed.replace("\r\r\n", "\n"))
def get_ssh():
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)
ssh.connect(hostname='192.168.102.105', port=22, username='root', password='xiayi123456', timeout=30)
stdin, stdout, stderr = ssh.exec_command('iperf3 -s')
print(stdout.read())
if __name__ == '__main__':
# 创建两个线程
t1 = threading.Thread(target=get_ip)
t2 = threading.Thread(target=get_ssh)
# 启动线程
t1.start()
t2.start()
# 等待线程执行完毕
t1.join()
t2.join()
```
在主函数中,首先创建了两个线程 `t1` 和 `t2`,分别执行 `get_ip` 和 `get_ssh` 函数。然后通过调用 `start()` 方法启动线程,最后通过调用 `join()` 方法等待线程执行完毕。这样就可以实现多线程执行两个函数。
阅读全文