Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6291
  • 博文数量: 7
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 80
  • 用 户 组: 普通用户
  • 注册时间: 2021-10-19 14:55
文章分类
文章存档

2021年(7)

我的朋友

分类: Python/Ruby

2021-10-22 10:23:20

os.popen方法可以获取到返回内容

点击(此处)折叠或打开

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

os.system方法执行运行命令

点击(此处)折叠或打开

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

python调用ssh命令

点击(此处)折叠或打开

  1. #!/usr/bin/python3
  2. import os
  3. KeyFile = "/root/.ssh/id_rsa" # 客户端的私钥,不管哪一端生成私钥和公钥,客户端拥有私钥,服务器是公钥
  4. # 你想一下是不是git让你生成私钥和公钥,然后让你把公钥粘贴过去,所以通常情况下服务器都是公钥,私钥客户端保管

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

  11. print (check_server_df("192.168.10.215", "22", "root", KeyFile, "date"))
由好买网提供
阅读(432) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~