I am able to give the following command in the command-line
C:\>cd "C:\Program Files\ExtraPuTTY\Bin"
C:\Program Files\ExtraPuTTY\Bin>putty.exe -ssh root@172.20.0.102 22
This helps me open the SSH session through PuTTY.
Whereas I am not able to reproduce them in the Python script.
cwd="C://Program Files//ExtraPuTTY//Bin"
COMMAND="ls"
ssh = Popen(['putty.exe -ssh','%s'%HOST, COMMAND,cwd],shell=True,stdout=f,stderr=f)
The error that I see is
"putty.exe -ssh"' is not recognized as an internal or external command,operable program or batch file
I know it' a bit beside the question, but that the most closed topic I found (I'd like to found that code a week ago on that post)
I was looking for a code to massively check if a password is active, and change it if possible
Putty have several cli tool like plink and pscp whch are useful for a lot of stuff.
Here is python 3 function to connect to a ssh server and accept ssh key. using pscp allow a auto accept key... can be useful for first time
Of cource, this juste Check connection (and accept ssh key)
So here is a code to run a script on the host (precisely a password change). To do so, you can't use one line syntax (even it must work, it won't, i tried) You have to pass through a script file and push it with plink.
So now you have two function to massively change password on your systems.
Bonus: a function to get /etc/passwd and /etc/shadow. Why? for educationnal use on your IT admin like 'hey you f*** up and use the same password everywhere, and now all this account can be Bruteforced. So clean up your mess
Some more notes:
if you don't use shell=True, you don't get the output, and it does not work, I don't know why.
I also tried a asynchronous communication to send comand line by line, it does not work.
I also tried the ssh command (yes it now exist on windows \o/) but it does not work for my purpose.
Hope this help someone one day, it would have helped me a lot a week ago :)
In the putty download page, download and install
plink
, and make sure its in the windows path ($PATH
variable)Then, this python snippet should work:
stdin
is the commands typed by the user in the terminal,stdout
andstderr
are the server output.Fill in your credentials in the
user="root"
,server="172.20.0.102 22"
and maybepassword
for the ssh connectionYou have to pass the
cwd
as thecwd
parameter of thePopen
:And you should use Plink, not PuTTY, for automating the remote command execution. The Plink accepts the command on its command-line (PuTTY does not):