1

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.

K.Krishnakanth
  • 127
  • 1
  • 15
  • Possible duplicate of [Base64 Encode "string" - command-line Windows?](https://stackoverflow.com/questions/37046771/base64-encode-string-command-line-windows) – Compo May 31 '19 at 09:45

3 Answers3

3

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')
Andrew Cheong
  • 29,362
  • 15
  • 90
  • 145
  • But, is there any other way to automate those Linux commands on Windows using Python. In the first command I have only 'base64'. But the other command has combination of 'openssl','base64'. – K.Krishnakanth May 31 '19 at 05:56
  • Oh, duh. You're just trying to encode a string to base64. Why use OS commands at all? Python supports it natively. `base64.b64encode(s[, altchars])` - See my edit. – Andrew Cheong May 31 '19 at 06:08
  • I tried that. But not working as expected. Manually I'm able to do that from Cygwin. But, I don't find a way to automate it. I've used the following code to automate it. But, it is just launching the cygwin, not performing any thing. p = subprocess.Popen("C:/cygwin64/bin/mintty.exe -i /Cygwin-Terminal.ico -", stdin=subprocess.PIPE, stdout=subprocess.PIPE) p.stdin.write("ls") p.stdin.close() out = p.stdout.read() print (out) – K.Krishnakanth May 31 '19 at 06:12
  • Ah, but `openssl`. No, there's no easy way to just make things work in Windows, aside from installing Cygwin (a UNIX layer, pretty much like Linux) on Windows and run your Python script from within Cygwin. – Andrew Cheong May 31 '19 at 06:14
  • That's probably some other error on your part. I would create a new question with exactly what you tried with Cygwin and ask why you are not seeing an output. Someone will be able to help you. – Andrew Cheong May 31 '19 at 06:17
0

If you use intend to use Linux commands a lot but still want a Windows machine, you can install a Linux subsystem: microsoft documentation

0

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]
K.Krishnakanth
  • 127
  • 1
  • 15