简体   繁体   中英

I have a couple of Linux commands, which i want to run on windows machine. How to Run Linux commands from Python on Windows

I have some Linux commands which will generate token. I've automated those commands using OS library form Python on Linux machine. It is working fine.

But, When I try the same code in the windows it is returning nothing.

The following is the code I've tried.

uniqueKey = os.popen('echo -n kittu | base64')
data = uniqueKey.read()
print data

in Linux I got the following output

a210dHU=

in windows it is empty.

Commands are specific to the OS. For example on Linux, ls lists files in a directory, while on Windows it's dir .

Windows has an echo like Linux but does not support the flag -n —the two commands are not related, only coinciding in name.

Windows also does not have a base64 command.

Nor does it use | to mean piping, though I believe PowerShell does.


Why use OS commands at all? Python supports base64 encoding natively:

uniqueKey = base64.b64encode('kittu')

如果您打算使用Linux命令但仍需要Windows机器,则可以安装Linux子系统: microsoft文档

I found the solution to execute Linux commands on Windows. I have enabled the 'bash' in windows machine. And I have used 'subprocess' library from Python to run 'Linux' commands on Windows. Following is the code.

bash = subprocess.Popen(['bash'],stdin=subprocess.PIPE,stdout=subprocess.PIPE)
result=bash.communicate(input="echo -n kittu|base64")
print result[0]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM