python第三方庫subprocess執(zhí)行cmd同時輸入密碼獲取參數(shù)
python subprocess執(zhí)行cmd同時輸入密碼獲取參數(shù)
一:手動輸入cmd命令
我們再執(zhí)行命令時需要同時傳入密碼或其他參數(shù)的時候,我們可以使用
echo {password} | adb shell ls /log'這個命令是一個組合的命令,涉及到 echo、管道 | 和 adb shell ls /log。下面我會逐一解釋每個部分:
1. echo {password}:
echo 是一個常用的命令行工具,用于輸出一個字符串或變量的內(nèi)容。
{password} 是一個占位符,通常代表要輸出的密碼。不過,請注意,直接在命令行中輸出密碼(尤其是使用 echo)是不安全的,因為這會將密碼暴露在命令歷史中,也可能被其他用戶在進程列表中看到。
2. |:
管道操作符。它的作用是將前一個命令的輸出作為下一個命令的輸入。
3. adb shell ls /log:
adb 是 Android Debug Bridge 的縮寫,它是一個命令行工具,允許你與 Android 設(shè)備進行通信。
shell 命令告訴 adb 在 Android 設(shè)備上執(zhí)行一個 shell 命令。
ls /log 是一個 shell 命令,用于列出 /log 目錄下的文件和目錄。在許多 Android 設(shè)備上,這是一個包含系統(tǒng)日志文件的目錄。
組合起來,這個命令的意圖是:輸出密碼,然后將這個輸出作為 adb shell ls /log 的輸入。但實際上,這個命令可能不會按照預期工作,因為 adb shell ls /log 不期望從管道接收密碼作為輸入。而且,如前所述,直接在命令行中輸出密碼是不安全的。
二. 萬能python三方庫subprocess
def subprocess_run( cmd, cmd_input=None):
"""
執(zhí)行 cmd 命令
"""
if cmd_input is not None:
# 創(chuàng)建子進程并執(zhí)行命令
p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
input_context = '{}\n'.format(cmd_input).encode('utf-8')
p.stdin.write(input_context)
# 獲取命令執(zhí)行結(jié)果
output, error = p.communicate()
# 使用sub函數(shù)去除命令行返回的命令符
clean_output = re.sub(r'\x1b\[.*?m', '', output.decode('utf-8'))
return clean_output
else:
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
return stdout.decode(), stderr.decode()到此這篇關(guān)于python subprocess執(zhí)行cmd同時輸入密碼獲取參數(shù)的文章就介紹到這了,更多相關(guān)python subprocess執(zhí)行cmd內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實現(xiàn)服務(wù)端渲染SSR的示例代碼
服務(wù)端渲染是一種常見的技術(shù)策略,特別是在需要改善網(wǎng)站的搜索引擎優(yōu)化(SEO)和首屏加載時間的場景下,本文將介紹如何利用?Python?實現(xiàn)?SSR,感興趣的可以了解下2024-02-02
python中數(shù)組array和列表list的基本用法及區(qū)別解析
大家都知道數(shù)組array是同類型數(shù)據(jù)的有限集合,列表list是一系列按特定順序排列的元素組成,可以將任何數(shù)據(jù)放入列表,且其中元素之間沒有任何關(guān)系,本文介紹python中數(shù)組array和列表list的基本用法及區(qū)別,感興趣的朋友一起看看吧2022-05-05
Python實現(xiàn)的求解最大公約數(shù)算法示例
這篇文章主要介紹了Python實現(xiàn)的求解最大公約數(shù)算法,涉及Python數(shù)學運算相關(guān)操作技巧,需要的朋友可以參考下2018-05-05
Python使用QQ郵箱發(fā)送郵件實例與QQ郵箱設(shè)置詳解
這篇文章主要介紹了Python發(fā)送QQ郵件實例與QQ郵箱設(shè)置詳解,需要的朋友可以參考下2020-02-02

