ITPub博客

首页 > 应用开发 > Python > Python-调用运行系统命令

Python-调用运行系统命令

原创 Python 作者:Caomeinico 时间:2021-10-22 10:43:50 0 删除 编辑

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

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70008684/viewspace-2838722/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2021-10-20

  • 博文量
    14
  • 访问量
    5254