Linux7.3默认的python系统是2.7.5,然后想着升级使用python3.6.6
1 下载
2 解压 tar fx Python-3.6.6.tgz
3 configure --prefix=/usr/local/python3.6
4 make && make install
5 ln -s /usr/local/python3.6/bin/python3.6 /usr/bin/python3.6
说明:这里是让两个版本的python共存
下面的脚本成功执行的
# /usr/bin/env python3.6
# -*- coding: utf-8 -*- import time
import subprocess
import re
from concurrent.futures import ThreadPoolExecutor def ping_call(ip):
command = 'ping -c 2 -W 1 %s' % ip
print(command)
file = open('/dev/null', 'w')
result = subprocess.call(command,
shell=True, stdout=file, stderr=file)
print(result)
if result:
print('ip-address {} ping fail'.format(ip))
else:
print('ip-address {} ping ok'.format(ip))
success.append(ip.split('.')[-1])
file.close() if __name__ == '__main__':
network = input('please input network>>>').strip()
host = input('please input your host range>>>').strip().split()
a, b = host[0], host[1]
print(network.split('.'))
if len(network.split('.')) == 3 and a.isdigit() and b.isdigit() and re.match('\d{1,3}\.\d{1,3}\.\d{1,3}', network):
a, b = int(a), int(b)
pool = ThreadPoolExecutor(50)
start_time = time.time()
success = []
for i in range(a, b + 1):
ret = pool.submit(ping_call, network + "." + str(i))
pool.shutdown()
print("multi_Thread spend-time {:.2f}".format(time.time() - start_time))
print("passed host is: ")
print(success)
说明:但是始终有个问题:不能使用中文,使用中文就报错,百度多次,暂时不明原因
使用中文报错:
UnicodeEncodeError 'ascii' codec can't encode characters in position
找到问题根源了:输入和输出编码的问题
[root@centos7 ~]# python3.6
Python 3.6.6 (default, Sep 1 2018, 17:07:25)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.stdout.encoding
'ANSI_X3.4-1968'
>>> sys.stdin.encoding
'ANSI_X3.4-1968'
>>>
所以使用前加入PYTHONIOENCODING=utf-8
中文版:
# /usr/bin/env python3.6
# -*- coding: utf-8 -*- import time
import subprocess
import re
from concurrent.futures import ThreadPoolExecutor def ping_call(ip):
command = 'ping -c 2 -W 1 %s' % ip
print(command)
file = open('/dev/null', 'w')
result = subprocess.call(command,
shell=True, stdout=file, stderr=file)
print(result)
if result:
print('ip地址: {} ping fail'.format(ip))
else:
print('ip地址: {} ping ok'.format(ip))
success.append(ip.split('.')[-1])
file.close() if __name__ == '__main__':
network = input('请输入 网段>>>').strip()
host = input('请输入主机范围,空格隔开>>>').strip().split()
a, b = host[0], host[1]
print(network.split('.'))
if len(network.split('.')) == 3 and a.isdigit() and b.isdigit() and re.match('\d{1,3}\.\d{1,3}\.\d{1,3}', network):
a, b = int(a), int(b)
pool = ThreadPoolExecutor(50)
start_time = time.time()
success = []
for i in range(a, b + 1):
ret = pool.submit(ping_call, network + "." + str(i))
pool.shutdown()
print("multi_Thread spend-time {:.2f}".format(time.time() - start_time))
print("passed host is: ")
print(success)
执行:与python2的方式不一样
[root@centos7 ~]# PYTHONIOENCODING=utf-8 python3.6 ping_host3.6.py
请输入 网段>>>192.168.0
请输入主机范围,空格隔开>>>1 10
['', '', '']
ping -c 2 -W 1 192.168.0.1
ping -c 2 -W 1 192.168.0.2
0
ip地址: 192.168.0.1 ping ok
ping -c 2 -W 1 192.168.0.3
ping -c 2 -W 1 192.168.0.4
ping -c 2 -W 1 192.168.0.5
ping -c 2 -W 1 192.168.0.6
ping -c 2 -W 1 192.168.0.7
ping -c 2 -W 1 192.168.0.8
ping -c 2 -W 1 192.168.0.9
ping -c 2 -W 1 192.168.0.10
0
ip地址: 192.168.0.2 ping ok
1
ip地址: 192.168.0.3 ping fail
1
ip地址: 192.168.0.4 ping fail
1
ip地址: 192.168.0.5 ping fail
1
ip地址: 192.168.0.6 ping fail
1
ip地址: 192.168.0.7 ping fail
1
ip地址: 192.168.0.8 ping fail
1
ip地址: 192.168.0.9 ping fail
1
ip地址: 192.168.0.10 ping fail
multi_Thread spend-time 2.09
passed host is:
['', '']
最后去掉提示改为一行命令版
# /usr/bin/env python3.6
# -*- coding: utf-8 -*- import time
import subprocess
import re
import sys
import os
from concurrent.futures import ThreadPoolExecutor def ping_call(ip):
command = 'ping -c 2 -W 1 %s' % ip
print(command)
file = open('/dev/null', 'w')
result = subprocess.call(command,
shell=True, stdout=file, stderr=file)
if result:
print('ip地址: {} ping fail'.format(ip))
else:
print('ip地址: {} ping ok'.format(ip))
success.append(ip.split('.')[-1])
file.close() if __name__ == '__main__':
# network = input('请输入 网段>>>').strip()
network = sys.argv[1]
# host = input('请输入主机范围,空格隔开>>>').strip().split()
a, b = sys.argv[2], sys.argv[3]
print(network.split('.'))
if len(network.split('.')) == 3 and a.isdigit() and b.isdigit() and re.match('\d{1,3}\.\d{1,3}\.\d{1,3}', network):
a, b = int(a), int(b)
pool = ThreadPoolExecutor(50)
start_time = time.time()
success = []
for i in range(a, b + 1):
ret = pool.submit(ping_call, network + "." + str(i))
pool.shutdown()
print("花费的时间: {:.2f}".format(time.time() - start_time))
print("能ping通的主机有: ")
for i in success:
print(i, end='\t')
print()