473,567 Members | 2,897 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 12887
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(Ex ception): 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.writ e('scp %(src)s %(user)s@%(host )s:%(dest)s ...' % locals())
sys.stdout.flus h()
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 UserKnownHostsF ile=/dev/null'
' -o StrictHostKeyCh ecking=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_PASSWO RD],
timeout=self.ti meout)
if match == 0:
raise SshException('f ailed to authenticate')
elif match == 1:
raise SshException('t imeout 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.ti meout)
if match == 0:
break
elif match == 1:
raise SshException('t imeout waiting for response')
elif match == 2:
state = 'stalled'

elif state == 'stalled':
match = child.expect([pexpect.EOF, pexpect.TIMEOUT , 'ETA'],
timeout=self.ti meout)
if match == 0:
break
elif match == 1:
import pdb
pdb.set_trace()
raise SshException('s talled 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.writ e(' %.1fs (%d cps)\n' % (elapsed, rate))
except:
sys.stdout.writ e(' %.1fs\n' % (elapsed))
def ssh(self, cmd):
import pexpect
user = self.user
host = self.host

if self.verbose:
sys.stdout.writ e('ssh -l %(user)s %(host)s \"%(cmd)s\"\ n' % locals())
sys.stdout.flus h()

# use compression
# -o options make sure we don't get messed up by the known_hosts file
child = pexpect.spawn(' ssh -C'
' -o UserKnownHostsF ile=/dev/null'
' -o StrictHostKeyCh ecking=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_PASSWO RD],
timeout=self.ti meout)
if match == 0:
raise SshException('f ailed to authenticate')
elif match == 1:
raise SshException('t imeout waiting to authenticate')
elif match == 2:
child.sendline( self.password)
state = 'running'

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

return child.before
class WindowsSshSessi on:
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('plin k -pw %(password)s -ssh %(user)s@%(host )s "%(cmd)s"' % locals())
def SshSession(host , user, password, timeout=30):
if sys.platform == 'win32':
sessionClass = WindowsSshSessi on
else:
# assume we're on linux if platform is not windows
sessionClass = LinuxSshSession
return sessionClass(ho st, 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.ti meout)
if match == 0:
break
elif match == 1:
import pdb
pdb.set_trace()
raise SshException('s talled 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
15212
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 Personal disk from the SQL Server 2000 Enterprise kit as this is reported here on the MSDN web site to be the version that is supported on Windows XP. ...
7
3198
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 think the username can be read with user.identity.name.
7
2622
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: System.ArgumentException occured in System.Windows.Forms.dll
1
3439
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 going wrong. The program has been written to do the following tasks: - Select remote server - Select from two specific services
0
3916
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 - Select from two specific services - Check the status of the server
4
3137
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 further away, completely leaving the selector box area. Any ideas? VS 2003 and VB.Net This is a simple application at the moment but the form is...
2
11704
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 another CD, and gets caught in a loop when I try to delete it! It mentions MS.net frame error # 1706. The "details" code follows...hope someone can help me...
1
7824
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 am note sure what to do because all propertiers work, except the System.Data.DataColumn.. I didn't write any code by hand and used the visual studio...
0
30187
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 a comprehensive list of all autostart locations for Windows OSes: NOTE : These are some abbreviations used in this list. Please note them...
0
7590
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8115
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7651
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5488
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3648
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3630
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1205
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
930
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.