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....

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.