Hi friends

I am trying to connect to a Linux machine from a Windows PC. I used to do it thru TELNET, but now I want to use SSH for better security. I tried with PYSSH, but I am going nowhere with it... Parmaiko is not working in Windows... Is there any way this can be done? I just want python to login, issue a command, get the output & then log back out...

Thanks in advance....

Dani AI

Generated

This thread already demonstrates two useful patterns: calling a command‑line SSH client from Windows to run a single remote command (), and automating interactive sessions for multi‑step work (). Below are complementary, practical options and troubleshooting notes that make those patterns safer and easier to maintain.

For simple "run a command and capture output" tasks, invoke the platform SSH client from Python via the subprocess API and use key authentication rather than passwords on the command line. Example (Python 3):

import subprocess

cmd = [
  "ssh",
  "-i", r"C:\path\to\id_rsa",
  "-o", "BatchMode=yes",
  "user@host",
  "uname -a; whoami"
]

result = subprocess.run(cmd, capture_output=True, text=True, timeout=20)
print(result.stdout)

This avoids interactive prompts (BatchMode), captures stdout/stderr cleanly, and times out if the remote side hangs.

For richer programmatic control (SFTP, tunnels, multiplexed commands) use a maintained Python SSH library that creates connections in‑process rather than parsing spawned process output. That approach reduces brittle parsing logic and lets the script manage retries, timeouts, and errors more reliably.

Security and troubleshooting notes:

  • Never embed plaintext passwords on the command line (process lists expose them). Prefer key pairs and an SSH agent to handle passphrases.
  • Ensure the remote sshd is reachable (port, firewall, and NAT), and add the host key to known_hosts ahead of automation so scripts do not block on prompts.
  • Fix key file permissions (private key readable only by owner) and ensure keys created on Windows do not have CRLF line endings.
  • If the environment is an older Windows that lacks a native OpenSSH client, use a modern terminal environment (Git Bash, Cygwin, or the built‑in client on newer Windows) so the subprocess approach works predictably.

These patterns keep scripts simpler and more secure than embedding interactive password handling, while still aligning with the practical examples contributed here by , , and .

Recommended Answers

All 5 Replies

I just installed the pyssh module, it does not really have any good examples to follow. If I figure someting out I will let you know.

I have found a way to SSH to a *IX machine... I tried it with Linux, AIX & Solaris Machines... it works fine..

I downloaded a command line version of PUTTY, called as PLINK. Its a .EXE file.

when executed from the command line

c:\plink username@192.168.0.1 -pw <password> <command to be executed in server>

it just runs the required command & displays the output in our local machine..

So by using os.popen(), we can execute it & fetch the output for further processing....

Thanks for the update :-)

this could come in handy. I am always writing scripts that need to connect my windows and linux boxes. Using ssh I could use this to communicate with computers both on my intranet and also remote computers on the internet.

You can use the following code snippet to connect thru ssh and also do scp.

# Login to ABC Machine via SSH
child = pexpect.spawn('ssh -l %s %s'%(ABCusername, ABChostname))
i = child.expect([pexpect.TIMEOUT, SSH_NEWKEY, COMMAND_PROMPT, '(?i)password'])
if i == 0: # Timeout
print 'ERROR! could not login with SSH. Here is what SSH said:'
print child.before, child.after
print str(child)
sys.exit (1)
if i == 1: # In this case SSH does not have the public key cached.
child.sendline ('yes')
child.expect ('(?i)password')
if i == 2:
# This may happen if a public key was setup to automatically login.
pass
if i == 3:
child.sendline(ABCpassword)
# Now we are either at the command prompt or
# the login process is asking for our terminal type.
i = child.expect ([COMMAND_PROMPT, TERMINAL_PROMPT])
if i == 1:
child.sendline (TERMINAL_TYPE)
child.expect (COMMAND_PROMPT)

# Set command prompt to something more unique.

COMMAND_PROMPT = "\[PEXPECT\]\$ "
child.sendline ("PS1='[PEXPECT]\$ '") # In case of sh-style
i = child.expect ([pexpect.TIMEOUT, COMMAND_PROMPT], timeout=10)
if i == 0:
print "# Couldn't set sh-style prompt -- trying csh-style."
child.sendline ("set prompt='[PEXPECT]\$ '")
i = child.expect ([pexpect.TIMEOUT, COMMAND_PROMPT], timeout=10)
if i == 0:
print "Failed to set command prompt using sh or csh style."
print "Response was:"
print child.before
sys.exit (1)

# Now you can do scp as follows:

child.sendline('scp ~/sample.txt %s@%s:/sample.txt'%(XYZusername, XYZhostname))
i = child.expect([pexpect.TIMEOUT, SSH_NEWKEY, COMMAND_PROMPT, '(?i)password'])
if i == 0: # Timeout
print 'ERROR! could not copy with SCP. Here is what SCP said:'
print child.before, child.after
print str(child)
sys.exit (1)
if i == 1: # In this case SSH does not have the public key cached.
child.sendline ('yes')
child.expect ('(?i)password')
if i == 2:
# This may happen if a public key was setup to automatically login.
pass
if i == 3:
child.sendline(XYZpassword)
child.expect(COMMAND_PROMPT)
print child.before

### All u r doing is; connecting to a machine called ABC and from there, u r doing scp to any required machine XYZ.

### I hope it helps u.

Thanks ms_melagiri ...

I will use this code my future implementations....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.