Python執(zhí)行dos和Linux命令的方法詳解
在實(shí)際開發(fā)中,有時(shí)為了方便,可能需要執(zhí)行dos命令或者Linux命令。比如說執(zhí)行某些shell腳本,上傳下載一些文件,執(zhí)行adb命令等跨語言,加壓包,解壓包等跨操作系統(tǒng)的場景。這樣能大大加強(qiáng)多個(gè)平臺和操作系統(tǒng)之間的關(guān)聯(lián)性。
windows:
案例1:彈窗式執(zhí)行dos命令(與打開cmd執(zhí)行一模一樣)
# -*- coding: utf-8 -*- import os # 解決打印時(shí),部分中文亂碼問題 os.popen('chcp 65001') # 查看當(dāng)前路徑 command1 = "chdir" os.system(command1) # 查看 ip command2 = "ipconfig" os.system(command2)
案例2:后臺執(zhí)行
# -*- coding: utf-8 -*- import os # 后臺執(zhí)行 adb命令 command2 = "adb" os.popen(command2)
案例3:執(zhí)行多條dos命令
# -*- coding: utf-8 -*- import os # command1執(zhí)行成功,才往下執(zhí)行command2 command1 = "java -version" command2 = "adb version" # 使用 && 隔開。 command = "{} && {}".format(command1, command2) print(command) os.popen(command)
Linux
Linux操作系統(tǒng)下,Python需要安裝一個(gè)第三方模塊:
pip install paramiko
案例1:執(zhí)行單條linux命令 - 方法:execute_cmd()
案例2:執(zhí)行多條linux命令 - 方法:execute_cmd_list()
# coding=utf-8 import paramiko class ServerMessage: # 目標(biāo)服務(wù)器信息 host = "xxx.xxx.xxx.xxx" user = "root" password = "xxxxxxxx" class SshChannel: def __init__(self, cfg_obj, timeout_sec=15, port=22): self._cfg = cfg_obj self.ssh_connect_timeout = timeout_sec self.port = port self.ssh_cli = None def __enter__(self): try: self.connecting_server_with_SSH2() except paramiko.ssh_exception.SSHException: print("連接{}失敗, 請核實(shí)配置或重試".format(self._cfg.host)) self.ssh_cli.close() else: return self def __exit__(self, tp, value, trace): self.ssh_cli.close() def connecting_server_with_SSH2(self): self.ssh_cli = paramiko.SSHClient() self.ssh_cli.load_system_host_keys() key = paramiko.AutoAddPolicy() self.ssh_cli.set_missing_host_key_policy(key) self.ssh_cli.connect(self._cfg.host, port=self.port, username=self._cfg.user, password=self._cfg.password, timeout=self.ssh_connect_timeout) def execute_cmd(self, cmd, get_pty=False): """ :param cmd: 單個(gè)命令 :return: 服務(wù)器的輸出信息 """ stdin, stdout, stderr = self.ssh_cli.exec_command(cmd, get_pty) return stdout.read().decode('utf-8') def execute_cmd_list(self, cmd_list): """ :param cmd: 單個(gè)命令 :return: 服務(wù)器的輸出信息 """ # 英文模式下的分號隔開。" ; " cmd = "".join([i + " ; " for i in cmd_list]) stdin, stdout, stderr = self.ssh_cli.exec_command(cmd, get_pty=True) return stdout.read().decode('utf-8') if __name__ == '__main__': with SshChannel(ServerMessage) as my_server: # 調(diào)用單條命令演示 pwd1 = my_server.execute_cmd("pwd") print(pwd1) # 調(diào)用多條命令演示 pwd2 = my_server.execute_cmd_list(["pwd", "free -m"]) print(pwd2)
到此這篇關(guān)于Python執(zhí)行dos和Linux命令的方法詳解的文章就介紹到這了,更多相關(guān)Python執(zhí)行dos Linux命令內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python中的數(shù)據(jù)結(jié)構(gòu)比較
這篇文章主要介紹了python中的數(shù)據(jù)結(jié)構(gòu)比較,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05Python全面分析系統(tǒng)的時(shí)域特性和頻率域特性
今天小編就為大家分享一篇Python全面分析系統(tǒng)的時(shí)域特性和頻率域特性,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02python實(shí)現(xiàn)將一維列表轉(zhuǎn)換為多維列表(numpy+reshape)
今天小編就為大家分享一篇python實(shí)現(xiàn)將一維列表轉(zhuǎn)換為多維列表(numpy+reshape),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11python使用requests模塊實(shí)現(xiàn)爬取電影天堂最新電影信息
這篇文章主要介紹了python使用requests模塊實(shí)現(xiàn)爬取電影天堂最新電影信息,本文通過實(shí)例代碼給大家介紹了str/list/tuple三者之間怎么相互轉(zhuǎn)換,需要的朋友可以參考下2019-04-04python django使用haystack:全文檢索的框架(實(shí)例講解)
下面小編就為大家?guī)硪黄猵ython django使用haystack:全文檢索的框架(實(shí)例講解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09python對raw格式照片進(jìn)行降噪處理的方法詳解
要對RAW格式的照片進(jìn)行降噪,我們可以使用rawpy庫來讀取RAW圖像,并使用imageio庫將處理后的圖像保存為其他格式,如PNG或JPEG,本文將詳細(xì)給大家介紹python如何對raw格式照片進(jìn)行降噪處理,文中有詳細(xì)的代碼流程,需要的朋友可以參考下2023-05-05用Python程序抓取網(wǎng)頁的HTML信息的一個(gè)小實(shí)例
這篇文章主要介紹了用Python程序抓取網(wǎng)頁的HTML信息的一個(gè)小實(shí)例,用到的方法同時(shí)也是用Python編寫爬蟲的基礎(chǔ),需要的朋友可以參考下2015-05-05Python實(shí)現(xiàn)切割mp3片段并降低碼率
MoviePy是一個(gè)基于Python的視頻編輯庫,它提供了創(chuàng)建、編輯、合并、剪輯和轉(zhuǎn)換視頻的功能,所以本文主要介紹如何使用moviepy來分割音頻流并降低碼率,感興趣的可以了解下2023-08-08