From Windows (Windows 10, Python3.6.3) I would like to send a few simple command to a Linux server and get the results back. Nothing more. I want to keep it simple and short. I didn't expect this to be so difficult but there seems to be a mess of helpful-meant but non-working, outdated or too complicated solutions. I know, one shouldn't pass the password in the command line but I'm in a closed environment so I don't need advanced encryption and public/private keys, etc. I also know that People strongly recommend to use Paramiko, but for different reasons I cannot install.
My learnings so far:
pxsshdoes not work for WindowsPuTTYdoesn't allow sending password in command line (whereasplinkdoes)- people strongly recommend to use
Paramiko. Although for different reasons I cannot install. Fabricbuilds onParamiko- I tried with
ssh.exewithout success
I guess, it doesn't make sense to list all the links of code snippets which did not work for me, there are too many... The following StackOverflow answers to these questions are either outdated or not helpful as well:
- Perform commands over ssh with Python
- Python script for SSH through PuTTY
- Using Plink (PuTTy) to SSH through Python
- How to execute a program or call a system command from Python?
Currently, I think I am the closest with plink because in Windows command line, the following
C:\Users\Downloads\PuTTY\plink.exe -ssh -batch user@111.22.33.44 -pw abc123
directly logs in without entering a password manually or confirmation via pressing Enter, and I can manually type commands like ls, uname -a, etc.
Now, however, I want to do this programmatically via a Python program. Starting from here: https://stackoverflow.com/a/32615977/7295599 (which seems to be outdated). So far, I ended up with the following:
import subprocess
ffname_exe = r'C:\Users\Downloads\PuTTY\plink.exe'
user='user'
server='111.22.33.44'
password = 'abc123'
plink = '{} -ssh -batch {}@{} -pw {}'.format(ffname_exe, user, server, password)
print(plink)
proc = subprocess.Popen(plink, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
command='uname -a'
stdout, stderr = proc.communicate(command.encode(),timeout=10)
print(stdout)
print(stderr)
But this times out after 10 seconds without any result printed.
I'm not sure whether this a problem with subprocess or with plink or the server? Actually, I'm not bound to plink, any other simple solution on Windows using PuTTY,ssh.exe, etc. without user interaction (to type a password or confirm anything) and installation of Paramiko are fine.