Linux7.3系统 升级python到3.6使用ping主机脚本
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()
Linux7.3系统 升级python到3.6使用ping主机脚本的更多相关文章
- 【Linux】CentOS下升级Python和Pip版本全自动化py脚本
[Linux]CentOS下升级Python和Pip版本全自动化py脚本 CentOS7.6自带py2.7和py3.6 想要安装其它版本的话就要自己重新下载和编译py其它版本并且配置环境,主要是软链接 ...
- 升级 python 2.6.6 系统到 2.7.10 版本
CentOS 6 系统默认 Python 版本是:2.6.6 平时在使用中遇到很多的库要求是 2.7.x 版本的库,比如使用 ConfigParser 库,在 2.6 版本库就不支持没有 value ...
- Linux系统下升级Python版本步骤(suse系统)
Linux系统下升级Python版本步骤(suse系统) http://blog.csdn.net/lifengling1234/article/details/53536493
- linux centos系统下升级python版本
本文参考资料:https://www.cnblogs.com/leon-zyl/p/8422699.html,https://blog.csdn.net/tpc1990519/article/deta ...
- CentOS6 系统下升级python后yum命令使用时报错
CentOS6 系统下升级python后yum命令使用时报错,如下: [root@xxxxxxx]#yumFile"/usr/bin/yum",line30exceptKeyboa ...
- Linux 下编译升级 Python
一.Centos下升级python3.4.3 1.下载安装 wget http://www.python.org/ftp/python/3.4.3/Python-3.4.3.tgz wget http ...
- CentOS 6.5升级Python和安装IPython
<转自:http://www.noanylove.com/2014/10/centos-6-5-sheng-ji-python-he-an-zhuang-ipython/>自己常用.以做备 ...
- Linux下升级python版本
转载自:http://lovebeyond.iteye.com/blog/1770476 CentOS下的Python版本一般都比较低,很多应用都需要升级python来完成.我装的centOS的默认的 ...
- CentOS 6.5升级Python后yum不可用的解决方案
因开发需要,今天把CentOS 6.5自带的Python2.6.6升级到了Python2.7.3.按照如下步骤进行升级 1.查看当前系统python的版本 python -V 2.下载2.7.3版本的 ...
随机推荐
- 【转】Python metaclass
转自: http://ju.outofmemory.cn/entry/32434 在回答了 yield关键字和 decorator的问题之后,我更明白了,我决定非常详细地回答这个问题. 读前警告:这个 ...
- ecstore开启发送邮件日志,并且排查邮件发布出去原因
config.php里 define("MAIL_LOG", true);//发送邮件日志开启2017-04-19 10:29:31 解决sendmail connection r ...
- 关于此实现不是 Windows 平台 FIPS 验证的加密算法的一部分。
注册表进入如下路径中 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\FipsAlgorithmPolicy 将 enable设置为0 ...
- C#中全局处理异常方式
using System; using System.Configuration; using System.Text; using System.Windows.Forms; using ZB.Qu ...
- 下载goland解压错误
把连接里面的 download.jetbrains.8686c.com 换成 download-cf.jetbrains.com
- 黄聪:PHP如何实现延迟一定时间后自动刷新当前页面、自动跳转header("refresh:1;url={$url}");
//1秒后自动跳转 header("refresh:1;url={$url}"); exit; //1秒后自动刷新当前页面header("refresh:1;" ...
- VBScript Scripting Techniques: File Open Dialog http://www.robvanderwoude.com/vbstech_ui_fileopen.php
I accept cookies This website uses cookies to ensure you get the best experience on our website More ...
- vue配合UI组件
bootstrap 创建项目 首先使用脚手架搭出项目基本框架,具体方法可以参考前面的文章. 1,执行命令创建项目:vue init webpack-simple vue-bootstrap 2,下载相 ...
- [转][PowerShell]ps执行重启IIS
来自:https://www.aliyun.com/jiaocheng/871477.html write-output 'Restarting IIS servers ............... ...
- java 泛型 checkcast
我们来看一段代码 public class Test3 { public static void main(String args[]) throws IllegalAccessException, ...