472,351 Members | 1,577 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,351 software developers and data experts.

SSH and Windows

Hello. I'm writing a Python program that connects to servers through
telnetlib to execute a few commands. I've discovered that some of the
servers that I have to connect to with this program run only SSH, so I
need to add support for SSH to the program. I'm looking for a library
that behaves similarly to telnetlib for SSH connections. Does anyone
know of one? I was going to try using pexpect to control the Windows
telnet command, but pexpect only works on Unix.

Any help is appreciated.


Jul 18 '05 #1
5 12806
Isaac Raway wrote:

Hello. I'm writing a Python program that connects to servers through
telnetlib to execute a few commands. I've discovered that some of the
servers that I have to connect to with this program run only SSH, so I
need to add support for SSH to the program. I'm looking for a library
that behaves similarly to telnetlib for SSH connections. Does anyone
know of one? I was going to try using pexpect to control the Windows
telnet command, but pexpect only works on Unix.


If it's just to execute a few commands, consider downloading PLink
(from the PuTTY site) and use it via os.system(). It works well.

-Peter
Jul 18 '05 #2
Peter Hansen <pe***@engcorp.com> wrote in message news:<3F**************@engcorp.com>...
Isaac Raway wrote:

Hello. I'm writing a Python program that connects to servers through
telnetlib to execute a few commands. I've discovered that some of the
servers that I have to connect to with this program run only SSH, so I
need to add support for SSH to the program. I'm looking for a library
that behaves similarly to telnetlib for SSH connections. Does anyone
know of one? I was going to try using pexpect to control the Windows
telnet command, but pexpect only works on Unix.
If it's just to execute a few commands, consider downloading PLink
(from the PuTTY site) and use it via os.system(). It works well.


Thank you, it worked perfectly. You have ended a week of headaces.
-Peter

Jul 18 '05 #3
Peter Hansen <pe***@engcorp.com> wrote in message news:<3F**************@engcorp.com>...
Isaac Raway wrote:

Hello. I'm writing a Python program that connects to servers through
telnetlib to execute a few commands. I've discovered that some of the
servers that I have to connect to with this program run only SSH, so I
need to add support for SSH to the program. I'm looking for a library
that behaves similarly to telnetlib for SSH connections. Does anyone
know of one? I was going to try using pexpect to control the Windows
telnet command, but pexpect only works on Unix.


If it's just to execute a few commands, consider downloading PLink
(from the PuTTY site) and use it via os.system(). It works well.

-Peter


Hmm...is there a *nix version for PLink? I checked PuTTY's site and
it's all for Windows. Or maybe an alternative for PLink for *nix?
Jul 18 '05 #4
Fazer wrote:

Peter Hansen <pe***@engcorp.com> wrote in message news:<3F**************@engcorp.com>...
Isaac Raway wrote:

Hello. I'm writing a Python program that connects to servers through
telnetlib to execute a few commands. I've discovered that some of the
servers that I have to connect to with this program run only SSH, so I
need to add support for SSH to the program. I'm looking for a library
that behaves similarly to telnetlib for SSH connections. Does anyone
know of one? I was going to try using pexpect to control the Windows
telnet command, but pexpect only works on Unix.


If it's just to execute a few commands, consider downloading PLink
(from the PuTTY site) and use it via os.system(). It works well.

-Peter


Hmm...is there a *nix version for PLink? I checked PuTTY's site and
it's all for Windows. Or maybe an alternative for PLink for *nix?


Maybe try this. Tested (mostly) under Python 1.5.2 (as I recall, under
Linux) and Python 2.0 under Windows:

import sys
import os
import time

True, False = 1, 0

class SshException(Exception): pass

class LinuxSshSession:

PAT_PASSWORD = '[pP]assword:'

def __init__(self, host, user, password, timeout=30):
self.host = host
self.user = user
self.password = password
self.timeout = timeout
self.verbose = True

def scp(self, src, dest):
import pexpect
user = self.user
host = self.host

if self.verbose:
sys.stdout.write('scp %(src)s %(user)s@%(host)s:%(dest)s ...' % locals())
sys.stdout.flush()
began = time.time()

try:
# use compression (not that that would help with a .tgz file
# and make sure we don't get messed up by the known_hosts file
child = pexpect.spawn('scp -C'
' -o UserKnownHostsFile=/dev/null'
' -o StrictHostKeyChecking=no'
' %(src)s %(user)s@%(host)s:%(dest)s' % locals())
state = 'authorizing'
while 1:
#~ print '%s: %r///%r' % (state, child.before, child.after)
if state == 'authorizing':
match = child.expect([pexpect.EOF, pexpect.TIMEOUT, self.PAT_PASSWORD],
timeout=self.timeout)
if match == 0:
raise SshException('failed to authenticate')
elif match == 1:
raise SshException('timeout waiting to authenticate')
elif match == 2:
child.sendline(self.password)
state = 'copying'

elif state == 'copying':
match = child.expect([pexpect.EOF, pexpect.TIMEOUT, 'stalled', 'ETA'],
timeout=self.timeout)
if match == 0:
break
elif match == 1:
raise SshException('timeout waiting for response')
elif match == 2:
state = 'stalled'

elif state == 'stalled':
match = child.expect([pexpect.EOF, pexpect.TIMEOUT, 'ETA'],
timeout=self.timeout)
if match == 0:
break
elif match == 1:
import pdb
pdb.set_trace()
raise SshException('stalled for too long, aborted copy')
elif match == 2:
state = 'copying'

finally:
if self.verbose:
elapsed = time.time() - began
try:
size = os.stat(src)[os.path.stat.ST_SIZE]
rate = size / elapsed
sys.stdout.write(' %.1fs (%d cps)\n' % (elapsed, rate))
except:
sys.stdout.write(' %.1fs\n' % (elapsed))
def ssh(self, cmd):
import pexpect
user = self.user
host = self.host

if self.verbose:
sys.stdout.write('ssh -l %(user)s %(host)s \"%(cmd)s\"\n' % locals())
sys.stdout.flush()

# use compression
# -o options make sure we don't get messed up by the known_hosts file
child = pexpect.spawn('ssh -C'
' -o UserKnownHostsFile=/dev/null'
' -o StrictHostKeyChecking=no'
' -l %(user)s %(host)s '
'\"%(cmd)s\"' % locals())
state = 'authorizing'
while 1:
if state == 'authorizing':
match = child.expect([pexpect.EOF, pexpect.TIMEOUT, self.PAT_PASSWORD],
timeout=self.timeout)
if match == 0:
raise SshException('failed to authenticate')
elif match == 1:
raise SshException('timeout waiting to authenticate')
elif match == 2:
child.sendline(self.password)
state = 'running'

elif state == 'running':
match = child.expect([pexpect.EOF, pexpect.TIMEOUT],
timeout=self.timeout)
if match == 0:
break
elif match == 1:
raise SshException('timeout waiting to finish')

return child.before
class WindowsSshSession:
def __init__(self, host, user, password, timeout=30):
self.host = host
self.user = user
self.password = password

def scp(self, src, dest):
user = self.user
host = self.host
password = self.password
return os.system('pscp -pw %(password)s %(src)s %(user)s@%(host)s:%(dest)s' % locals())

def ssh(self, cmd):
user = self.user
host = self.host
password = self.password
os.system('plink -pw %(password)s -ssh %(user)s@%(host)s "%(cmd)s"' % locals())
def SshSession(host, user, password, timeout=30):
if sys.platform == 'win32':
sessionClass = WindowsSshSession
else:
# assume we're on linux if platform is not windows
sessionClass = LinuxSshSession
return sessionClass(host, user, password, timeout)
-Peter
Jul 18 '05 #5
Peter Hansen wrote:

Maybe try this. Tested (mostly) under Python 1.5.2 (as I recall, under
Linux) and Python 2.0 under Windows:

elif state == 'stalled':
match = child.expect([pexpect.EOF, pexpect.TIMEOUT, 'ETA'],
timeout=self.timeout)
if match == 0:
break
elif match == 1:
import pdb
pdb.set_trace()
raise SshException('stalled for too long, aborted copy')


Ugh... remove the "import pdb" and subsequent pdb.set_trace() before attempting
to use the above... sorry about that. Must have checked a debugging version
in to CVS. :-(

-Peter
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ken Lindner | last post by:
I have a need to become familiar with SQL Server 2000 for work. Needless to say I am new to SQL Server any version, but not IT in general. My...
7
by: lvpaul | last post by:
Hallo ! I am using IIS-Windows-Authentication in my intranet (web.config <authentication mode="Windows" /> <identity impersonate="true" /> ...
7
by: Tyler Foreman | last post by:
Hello, I have a strange problem that occurs every so often in my application. It usually takes place when I hide one form and activate another....
1
by: Scott Davies | last post by:
Hi, I'm looking for some help on a small program that I'm trying to develop in VB.NET. I'm having trouble getting the code that I've written to...
0
by: Scott Davies | last post by:
Hi, I'm looking for some help on a small program that I'm trying to develop in VB.NET. The program I'm trying to develop needs to be able to...
4
by: Rod Gill | last post by:
Hi, I have a form that when opened in the designer appears of the screen. The form selector can't be dragged (or resized) and if I scroll right...
2
by: sambo251 | last post by:
After running a few updates I get this very annoying "Windows Installer" error #1706 that will ne go away! It keeps saying that it cannot find the...
1
by: mfunkmann | last post by:
Hi, I recently got an error and I don't know how to fix it: Error 1 'System.Data.DataColumn' does not contain a definition for...
0
AmberJain
by: AmberJain | last post by:
Windows Autorun FAQs: List of autostart locations Linked from the Original article- "Windows Autorun FAQs: Description". Que: Can you list all...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.