关于python:Python调用运行系统命令

4次阅读

共计 601 个字符,预计需要花费 2 分钟才能阅读完成。

os.popen 办法能够获取到返回内容

HeadText = os.popen('sed -n 1p \"{}\"'.format(DirFile)).read()

os.system 办法执行运行命令

Command="sh /home/TradeInfo/new/trade_plan/py/CopyTradeplan.sh" 
os.system(Command)

python 调用 ssh 命令

#!/usr/bin/python3
import os
KeyFile = "/root/.ssh/id_rsa" # 客户端的私钥,不论哪一端生成私钥和公钥,客户端领有私钥,服务器是公钥
# 你想一下是不是 git 让你生成私钥和公钥,而后让你把公钥粘贴过来,所以通常状况下服务器都是公钥,私钥客户端保存

def check_server_df(host, port, username, pkey, command):
    try:
        data = os.popen('ssh -p {} -i {} {}@{} {}'.format(port, pkey, username, host, command)).read()
    except Exception as e:
        print(e)
    return data

print (check_server_df("192.168.10.215", "22", "root", KeyFile, "date"))

由好买网提供 更多建站及源码交易信息请见 GoodMai 好买网

正文完
 0