簡體   English   中英

如何將本地變量傳遞給遠程echo命令?

[英]How to pass local variable to remote echo command?

import paramiko, commands
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.load_system_host_keys()
ssh_client.connect('xx.xx.x', username='abc', 
key_filename='rsa')

line ="Hello"
stdin, stdout, stderr=ssh_client.exec_command('echo $line')
print stdout.readlines()

我想傳遞“行”內容以回顯。 但是我得到[u'\\ n']作為輸出。

我也試過echo \\ $ line,echo“ $ line”。 但是沒有打招呼作為輸出。

遠程外殼程序無法訪問您的程序變量,該命令必須在啟動之前完成。

stdin, stdout, stderr = ssh_client.exec_command('echo "{0}"'.format(line))

請注意安全問題( 感謝@Tripleee ),在Python 3中使用shlex.quote可以提高代碼的健壯性:

stdin, stdout, stderr = ssh_client.exec_command('echo {}'.format(quote(line)))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM