Python 执行 shell 命令添加 sudo 密码

Python 执行 shell 命令添加 sudo 密码Python 执行 shell 命令添加 sudo 密码 1 ping c4www baidu comstrong foreverstron ping c4www baidu comPINGwww a shifen com 61 135 169 121 56 84 bytesofdata 64bytesfrom6 135 169 121 icmp seq 1ttl 53time 24 8ms64bytesfr 135 169 121



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 = "" 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

https://yongqiang.blog.csdn.net/

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/212415.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午8:06
下一篇 2026年3月18日 下午8:06


相关推荐

  • genre-based_deepsort特征判断

    genre-based_deepsort特征判断  https://arxiv.org/abs/1804.01438https://blog.csdn.net/gavinmiaoc/article/details/80648754https://zhuanlan.zhihu.com/p/35296881 https://github.com/seathiefwang/MGN-pytorch

    2026年4月14日
    9
  • Vmware安装Ubuntu16.4、Ubuntu里安装python3.9、Ubuntu安装PyCharm的过程及出现的问题的解决[通俗易懂]

    Vmware安装Ubuntu16.4、Ubuntu里安装python3.9、Ubuntu安装PyCharm的过程及出现的问题的解决[通俗易懂]目录1、VMware安装Ubuntu16.4虚拟机1.1、下载Ubuntu镜像文件1.2、安装Ubuntu虚拟机1.2、装Ubuntu系统和虚拟机工具1.3、解决Ubuntu不能全屏显示1.4、设置共享文件夹1.4.1、主机上的文件夹设置1.4.2、虚拟机上的设置1.5、解决/mnt下没有hgfs文件夹1.6、解决找不到共享文件夹的问题1.7、解决重启后共享文件夹没有了的问题2、Ubuntu安装Python3.92.1、安装Python3.92.

    2022年8月29日
    7
  • eureka集群配置_hadoop高可用集群搭建

    eureka集群配置_hadoop高可用集群搭建Eruka高可用(集群)EurekaServer高可用配置(1)高可用是什么?“高可用性”(HighAvailability)通常来描述一个系统经过专门的设计,从而减少停工时间,而保持其服务的高度可用性如:给EurekaServer搞一个备份(2)服务同步原理多个EurekaServer之间也会互相注册为服务,当服务提供者注册到EurekaServer集群中的某个节点时,该节点会把服务的信息同步给集群中的每个节点,从而实现数据同步。因此,无论客户端访问到EurekaServer集

    2022年8月21日
    9
  • YUV格式到底是什么?

    YUV格式到底是什么?简介YUV是视频、图片、相机等应用中使用的一类图像格式,实际上是所有“YUV”像素格式共有的颜色空间的名称。与RGB格式(红-绿-蓝)不同,YUV是用一个称为Y(相当于灰度)的“亮度”分量和两个“色度”分量表示,分别称为U(蓝色投影)和V(红色投影),由此得名。YUV也可以称为YCbCr,虽然这些术语意味着略有不同,但它们往往会混淆并可互换使用。Y表示亮度分量:如果只显示Y的话,…

    2022年7月16日
    27
  • 数据库中的无损连接分解和是否保持函数依赖的判定

    数据库中的无损连接分解和是否保持函数依赖的判定首先了解一下几个概念 1 把一个关系模式分解成若干个关系模式的过程 称为关系模式的分解 2 把低一级的关系模式分解为若干个高一级的关系模式的方法不是唯一的 3 只有能够保证分解后的关系模式与原关系模式等价 分解方法才有意义 对于第一句话 为什么需要分解关系模式 因为原来的关系模式可能造成数据冗余或给数据库带来潜在的不一致性 对于第二句话 根据不同语义 分解的原则也不尽相

    2026年3月17日
    1
  • 笛卡尔积简单介绍「建议收藏」

    笛卡尔积简单介绍「建议收藏」title:简单介绍笛卡尔积date:2020-10-1320:26:18tags:“离散数学????”categories:“2020/10”简介这个博客参考左孝陵先生的《离散数学》,会尽量简单的讲讲笛卡尔积,能够给大家一个更加具体的认识。什么是序偶要知道什么是序偶,先得弄明白序偶的作用,我觉得序偶就是带顺序的集合,用来表示一些集合表示不了的东西。比如在小学学的直角坐标系上,有两个点,(2,3)和(3,2),点的坐标就是序偶,因为它自带顺序,为什么每次一个点都先读x坐标再读y坐标

    2022年7月11日
    24

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号