Python-调用运行系统命令

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 好买网

你可能感兴趣的:(python)