Python 执行 shell 命令添加 sudo 密码
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
