SSH and Windows

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Isaac Raway

    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.




  • Peter Hansen

    #2
    Re: SSH and Windows

    Isaac Raway wrote:[color=blue]
    >
    > 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.[/color]

    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

    Comment

    • Isaac Raway

      #3
      Re: SSH and Windows

      Peter Hansen <peter@engcorp. com> wrote in message news:<3F2069BD. 1FE2EC2@engcorp .com>...[color=blue]
      > Isaac Raway wrote:[color=green]
      > >
      > > 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.[/color]
      >
      > 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.[/color]

      Thank you, it worked perfectly. You have ended a week of headaces.
      [color=blue]
      > -Peter[/color]

      Comment

      • Fazer

        #4
        Re: SSH and Windows

        Peter Hansen <peter@engcorp. com> wrote in message news:<3F2069BD. 1FE2EC2@engcorp .com>...[color=blue]
        > Isaac Raway wrote:[color=green]
        > >
        > > 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.[/color]
        >
        > 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[/color]

        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?

        Comment

        • Peter Hansen

          #5
          Re: SSH and Windows

          Fazer wrote:[color=blue]
          >
          > Peter Hansen <peter@engcorp. com> wrote in message news:<3F2069BD. 1FE2EC2@engcorp .com>...[color=green]
          > > Isaac Raway wrote:[color=darkred]
          > > >
          > > > 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.[/color]
          > >
          > > 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[/color]
          >
          > 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?[/color]

          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

          Comment

          • Peter Hansen

            #6
            Re: SSH and Windows

            Peter Hansen wrote:[color=blue]
            >
            > 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')[/color]

            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

            Comment

            Working...