免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3942 | 回复: 5

如何利用python提取unix服务器里面的cat结果 [复制链接]

论坛徽章:
0
发表于 2010-08-02 15:32 |显示全部楼层
我想把unix服务器里面的cat命令结果赋给一个变量,不知道怎么写这个脚本好,请帮忙谢谢!cat /monitor/invinfo/lepus73/osdetail

论坛徽章:
0
发表于 2010-08-02 15:58 |显示全部楼层
  1. import commands
  2. a = commands.getoutput('cat /monitor/invinfo/lepus73/osdetail')
  3. print a
复制代码

论坛徽章:
0
发表于 2010-08-03 17:29 |显示全部楼层
本帖最后由 kevinwell 于 2010-08-03 17:33 编辑

如果要是命令前面要加一个判断,判断是aix或者是hpux或者linux,之后再使用不同的命令,比如:
if
   是aix
then
   cat /monitor/invinfo/lepus73/osdetail
if
是 hpux
then
cat/monitor/test.txt

if
是linux
then
cat /linux.txt

论坛徽章:
0
发表于 2010-08-03 18:04 |显示全部楼层

  1. import os
  2. import subprocess

  3. def runSysCmd(cmdStr):
  4.     if cmdStr == "":
  5.         return ("",0)
  6.     p = subprocess.Popen(cmdStr, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  7.     ret_str = p.stdout.read()
  8.     retval = p.wait()
  9.     return (ret_str,retval)
复制代码
然后调用:

  1. cmd = "/path to shell script or cmd"
  2. retstr,retCode = usys.runSysCmd(cmd)
复制代码
如果你要进行判断,那么将你的shell写成一个脚本。然后添加可执行权限。
然后cmd调用起shell脚本即可。

论坛徽章:
0
发表于 2010-08-04 11:38 |显示全部楼层
我想这个程序是单独靠python来做的,不调用shell。
首先利用osname来判断系统
然后再根据系统来执行不同的命令得到结果
最后把得到的结果set到应用系统的api中,
但是前两步,不知道如何写比较好

论坛徽章:
0
发表于 2010-08-04 16:49 |显示全部楼层
我想这个程序是单独靠python来做的,不调用shell。
首先利用osname来判断系统
然后再根据系统来执行不同的 ...
kevinwell 发表于 2010-08-04 11:38



不调用shell是什么意思?你是想自己写一个cat程序吗?

判断系统可以用platform模块。

>>> import platform
>>> platform.system()
'Linux'
>>>
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP