Python 执行 shell 命令添加 sudo 密码

1. ping -c 4 www.baidu.com

strong@foreverstrong:~$ ping -c 4 www.baidu.com
PING www.a.shifen.com (61.135.169.121) 56(84) bytes of data.
64 bytes from 61.135.169.121: icmp_seq=1 ttl=53 time=24.8 ms
64 bytes from 61.135.169.121: icmp_seq=2 ttl=53 time=25.7 ms
64 bytes from 61.135.169.121: icmp_seq=3 ttl=53 time=25.0 ms
64 bytes from 61.135.169.121: icmp_seq=4 ttl=53 time=24.7 ms

--- www.a.shifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 24.761/25.097/25.720/0.419 ms
strong@foreverstrong:~$

2. print(os.system("ping -c 4 www.taobao.com"))

os.system(command) 同主程序是同步执行的。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import os
import sys

sys.path.append(os.path.dirname(os.path.abspath(__file__)))
current_directory = os.path.dirname(os.path.abspath(__file__))

print(16 * "++--")
print("current_directory:", current_directory)
print(16 * "++--")


def forever():
    print("\ncheng.")
    print(os.system("ping -c 4 www.baidu.com"))
    print("\nyong.")
    print(os.system("ping -c 4 www.taobao.com"))
    print("\nqiang.")


if __name__ == '__main__':
    forever()
/usr/bin/python3.5 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--

cheng.
PING www.a.shifen.com (61.135.169.125) 56(84) bytes of data.
64 bytes from 61.135.169.125: icmp_seq=1 ttl=53 time=110 ms
64 bytes from 61.135.169.125: icmp_seq=2 ttl=53 time=111 ms
64 bytes from 61.135.169.125: icmp_seq=3 ttl=53 time=109 ms
64 bytes from 61.135.169.125: icmp_seq=4 ttl=53 time=60.2 ms

--- www.a.shifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3001ms
rtt min/avg/max/mdev = 60.256/98.022/111.091/21.809 ms
0

yong.
PING www.taobao.com.danuoyi.tbcache.com (123.138.67.77) 56(84) bytes of data.
64 bytes from 123.138.67.77: icmp_seq=1 ttl=55 time=38.0 ms
64 bytes from 123.138.67.77: icmp_seq=2 ttl=55 time=31.6 ms
64 bytes from 123.138.67.77: icmp_seq=3 ttl=55 time=48.6 ms
64 bytes from 123.138.67.77: icmp_seq=4 ttl=55 time=36.6 ms

--- www.taobao.com.danuoyi.tbcache.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 31.650/38.750/48.603/6.169 ms
0

qiang.

Process finished with exit code 0

3. os.system('echo %s | sudo -S %s' % (password, command))

Python 执行 shell 命令添加 sudo 密码。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import os
import sys

sys.path.append(os.path.dirname(os.path.abspath(__file__)))
current_directory = os.path.dirname(os.path.abspath(__file__))

print(16 * "++--")
print("current_directory:", current_directory)
print(16 * "++--")


def forever():
    password = "123456"
    command = "sudo cat /etc/hostname"

    print("\ncheng.")
    print(os.system("ping -c 4 www.baidu.com"))
    print("\nyong.")
    print(os.system("ping -c 4 www.taobao.com"))
    print("\nqiang.")

    os.system('echo %s | sudo -S %s' % (password, command))


if __name__ == '__main__':
    forever()
/usr/bin/python3.5 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--

cheng.
PING www.a.shifen.com (61.135.169.121) 56(84) bytes of data.
64 bytes from 61.135.169.121: icmp_seq=1 ttl=53 time=25.4 ms
64 bytes from 61.135.169.121: icmp_seq=2 ttl=53 time=25.1 ms
64 bytes from 61.135.169.121: icmp_seq=3 ttl=53 time=24.6 ms
64 bytes from 61.135.169.121: icmp_seq=4 ttl=53 time=24.9 ms

--- www.a.shifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 24.677/25.085/25.497/0.340 ms
0

yong.
PING www.taobao.com.danuoyi.tbcache.com (123.138.67.77) 56(84) bytes of data.
64 bytes from 123.138.67.77: icmp_seq=1 ttl=55 time=2.89 ms
64 bytes from 123.138.67.77: icmp_seq=2 ttl=55 time=3.36 ms
64 bytes from 123.138.67.77: icmp_seq=3 ttl=55 time=2.96 ms
64 bytes from 123.138.67.77: icmp_seq=4 ttl=55 time=2.43 ms

--- www.taobao.com.danuoyi.tbcache.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3003ms
rtt min/avg/max/mdev = 2.439/2.918/3.367/0.329 ms
0

qiang.
[sudo] password for strong: foreverstrong

Process finished with exit code 0

4. cat /etc/hostname

strong@foreverstrong:~$ cat /etc/hostname 
foreverstrong
strong@foreverstrong:~$

References

[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yongqiang Cheng

梦想不是浮躁,而是沉淀和积累。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值