def run():
str_shell='ipconfig'
sub=subprocess.Popen(args=str_shell,shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE,
stderr=subprocess.PIPE,universal_newlines=True)
try:
out,err=sub.communicate(timeout=15)
except TimeoutExpired:
sub.kill()
out, err = sub.communicate()
#res=sub.stdout.readlines() if sub.returncode == 0:
if out:
#log.info("执行输出正常")
log.info(out)
return out
if err:
#log.error("出现异常")
log.error(err,exc_info=True)
else:
if sub.returncode == 1:
log.error("执行shell对象结果有空")
else:
raise subprocess.CalledProcessError(sub.returncode, str_shell)

  

[root@hostuser src]# python3 subprocess_popen.py
[INFO]2019-05-18 23:39:07 Sat --app-- subprocess_popen.py:
<class 'str'>
[INFO]2019-05-18 23:39:07 Sat --app-- subprocess_popen.py:
Filesystem 1M-blocks Used Available Use% Mounted on
/dev/mapper/centos-root 27627 8627 19000 32% /
devtmpfs 894 0 894 0% /dev
tmpfs 910 1 910 1% /dev/shm
tmpfs 910 11 900 2% /run
tmpfs 910 0 910 0% /sys/fs/cgroup
/dev/sda1 1014 232 783 23% /boot
tmpfs 182 1 182 1% /run/user/42
tmpfs 182 0 182 0% /run/user/0
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:9100 0.0.0.0:* LISTEN 6481/node_exporter
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 127.0.0.1:8080 0.0.0.0:* LISTEN 7057/unicorn master
tcp 0 0 127.0.0.1:9168 0.0.0.0:* LISTEN 6505/ruby
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 9232/dnsmasq
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 7211/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 7212/cupsd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 7459/master
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 52237/sshd: root@pt
tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN 57561/sshd: root@pt
tcp 0 0 0.0.0.0:8060 0.0.0.0:* LISTEN 6479/nginx: master
tcp 0 0 0.0.0.0:8989 0.0.0.0:* LISTEN 6479/nginx: master
tcp 0 0 127.0.0.1:9121 0.0.0.0:* LISTEN 6485/redis_exporter
tcp 0 0 127.0.0.1:9090 0.0.0.0:* LISTEN 6472/prometheus
tcp 0 0 127.0.0.1:9187 0.0.0.0:* LISTEN 6494/postgres_expor
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 ::1:9168 :::* LISTEN 6505/ruby
tcp6 0 0 :::22 :::* LISTEN 7211/sshd
tcp6 0 0 :::23 :::* LISTEN 35527/xinetd
tcp6 0 0 ::1:631 :::* LISTEN 7212/cupsd
tcp6 0 0 ::1:25 :::* LISTEN 7459/master
tcp6 0 0 ::1:6010 :::* LISTEN 52237/sshd: root@pt
tcp6 0 0 ::1:6011 :::* LISTEN 57561/sshd: root@pt
NoneType: None
[root@hostuser src]#

  

最新文章

  1. Linux中进行单文件内容的复制
  2. PHP浅复制与深复制
  3. 查找数据库中重复的值的数据,having的使用,count(1),sum等聚会函数
  4. Android Handler的使用示例:结合源码理解Android Handler机制(一)
  5. OGG-03517 Conversion from character set failed解决方法
  6. BZOJ-1036 树的统计Count 链剖线段树(模板)=(树链剖分+线段树)
  7. 将Eclipse项目导入Android Studio出现中文乱码的问题
  8. android中ViewHolder通用简洁写法
  9. ASP.net中的Cache使用介绍
  10. 织梦CMS后台卡死的解决办法
  11. 一些User-Agent
  12. error: a label can only be part of a statement and a declaration is not a statement
  13. Windows as a Service(2)—— 使用WSUS管理Windows10更新
  14. Ubuntu 下安装 matlab2018a
  15. android4.2添加重启菜单项
  16. Android Gradle 依赖配置:implementation &amp; api
  17. MySQL:缓存算什么东西?!
  18. Android 读取后台数据并显示。模拟小区车辆管理系统
  19. SpringMVC(3):DispatcherServlet详解
  20. Gnucash数据库结构

热门文章

  1. docker容器 - 宿主机和容器之间复制文件(cp)
  2. docker容器 - 导入容器、导出容器、查看容器
  3. JDBC简单代码
  4. Java-POJ1001-求高精度幂
  5. 线性筛-euler,强大O(n)
  6. Axure licensee key 8~9-转
  7. Atcoder Beginner Contest153E(完全背包)
  8. nginx配置指令auth_basic、auth_basic_user_file及相关知识
  9. Building Ethereum private chain on CentOS
  10. jvm系列(一)之内存模型