简体   繁体   English

我如何使用Linux执行命令功能中python程序中存在的变量

[英]How i can use variable present in python program inside the execute command function of linux

import paramiko
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.WarningPolicy())
client.connect('example.com',22,'XXXXX','XXXX')
a=input ("enter the file path")
stdin,stdout,stderr=client.exec_command('cd a && pwd')

I want to pass the variable 'a' to the exec_command .. I am trying to get the file path from the user and i am storing it in a variable a, i want to know how i can pass the variable a to the Cd command under exec_command function 我想将变量“ a”传递给exec_command ..我正试图从用户那里获取文件路径,并将其存储在变量a中,我想知道如何将变量a传递给Cd命令。在exec_command函数下

Since there can be some security flaws by code injection, you must not format user input directly into shell commands. 由于代码注入可能会带来一些安全漏洞,因此您不能直接将用户输入格式化为shell命令。

Here you can use environment variables: 在这里您可以使用环境变量:

stdin,stdout,stderr=client.exec_command('cd "$a" && pwd', environment={'a': a})

您是否尝试根据变量构造命令?

stdin,stdout,stderr=client.exec_command('cd ' + str(a) + '&& pwd')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Python程序中调用linux mailx命令并使用存储在变量中的值? - How to call the linux mailx command from inside a Python program and also use a value stored in a variable? 我如何在 Python 中执行 comm Linux 命令 - How can i execute comm Linux command in Python 如何使用./ 通过 Python 脚本执行命令行程序? - How can I execute command line program with ./ via a Python script? 我如何使用 python 读取 linux 程序的输出(顶部) - How can i use python to read output of linux program (top) Python-如果在函数内部创建变量,那么如何在函数外部使用它? - Python - If I create a variable inside a function, how can I then use it outside the function? 如何在函数Login()中获取'username'的值,以在另一个python程序上使用它? - How can I get the value of 'username' inside the function Login() to use it on another python program? 来自python的Linux命令在子文件夹内执行 - Linux command from python to execute inside subfolders 如何使用 Python for 循环来执行里面的函数? - How to use Python for loop to execute the function inside? 如何在Python程序中使用Jinja2模板? - How can I use a Jinja2 template inside a Python program? 如何在 ffmpeg 命令中使用 python 变量 - How to use python variable inside ffmpeg command
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM