473,324 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 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 12877
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 employer has provided me with the SQL Server 2000...
7
by: lvpaul | last post by:
Hallo ! I am using IIS-Windows-Authentication in my intranet (web.config <authentication mode="Windows" /> <identity impersonate="true" /> How can I get the users (client) IP-Address ? I...
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. What happens is I get the following exception:...
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 work, can anyone shed some light as to where I'm...
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 do the following: - Select remote server -...
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 and down to centralise it the form simply jumps...
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 file "instantsharedevices.msi", that it is on...
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 'Windows' C:\c#\CsharpPRO\Form1.Designer.cs 304 77 CsharpPRO I...
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 the autostart locations for windows? Ans: Here is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.