Using python with SSH?

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

    Using python with SSH?

    Hi,

    I need to run a remote program from a server that I have access rights
    to. I want to be able to do this from within in my python script and I
    want to use ssh (or anything else that will work). However, doing
    something like:

    ssh -l user host "/path/to/program"

    prompts for a password and I don't know how to supply one via a python
    script. Is there an easy way out? Can I log onto to the other machine
    via ssh somehow? Does a python script support this? Thanks!

    Steve

  • Paul Rubin

    #2
    Re: Using python with SSH?

    Steve <nospam@nopes > writes:[color=blue]
    > ssh -l user host "/path/to/program"
    >
    > prompts for a password and I don't know how to supply one via a python
    > script. Is there an easy way out? Can I log onto to the other machine
    > via ssh somehow? Does a python script support this? Thanks![/color]

    The simplest way is to set up ssh at both ends with RSA authentication
    and have a private key on the local machine. That gets rid of the
    need for a password, while still keeping the connection secure, as
    long as the local machine is secure.

    Comment

    • Lawrence Oluyede

      #3
      Re: Using python with SSH?

      Steve <nospam@nopes > writes:
      [color=blue]
      > ssh -l user host "/path/to/program"
      >
      > prompts for a password and I don't know how to supply one via a python
      > script. Is there an easy way out? Can I log onto to the other machine via
      > ssh somehow? Does a python script support this? Thanks![/color]

      Try something like this:

      import os

      ssh_pipe = os.popen("ssh -l user path")
      ssh_pipe.write( password)
      ssh_pipe.write( "\n")
      ssh_pipe.flush( )
      ssh_pipe.close( )

      hope this helps

      --
      Lawrence "Rhymes" Oluyede
      Blogger ist ein Veröffentlichungs-Tool von Google, mit dem du ganz einfach deine Gedanken der Welt mitteilen kannst. Mit Blogger kannst du problemlos Texte, Fotos und Videos in deinem persönlichen Blog oder deinem Team-Blog veröffentlichen.

      Comment

      • Jp Calderone

        #4
        Re: Using python with SSH?

        On Thu, Dec 18, 2003 at 12:08:43AM +0100, Lawrence Oluyede wrote:[color=blue]
        > Steve <nospam@nopes > writes:
        >[color=green]
        > > ssh -l user host "/path/to/program"
        > >
        > > prompts for a password and I don't know how to supply one via a python
        > > script. Is there an easy way out? Can I log onto to the other machine via
        > > ssh somehow? Does a python script support this? Thanks![/color]
        >[/color]

        You could open ssh in a pty, or you could use a Python module that speaks
        the SSH protocol, thus obviating the requirement to call out to the ssh
        command line program.

        A couple choices for such modules:

        http:/www.twistedmatr ix.com/


        [color=blue]
        > Try something like this:
        >
        > import os
        >
        > ssh_pipe = os.popen("ssh -l user path")
        > ssh_pipe.write( password)
        > ssh_pipe.write( "\n")
        > ssh_pipe.flush( )
        > ssh_pipe.close( )
        >
        > hope this helps[/color]

        It does not. ssh won't accept a password from standard input, it requires
        it to be entered into the terminal.

        Jp

        Comment

        • Lawrence Oluyede

          #5
          Re: Using python with SSH?

          Jp Calderone <exarkun@intarw eb.us> writes:
          [color=blue]
          > It does not. ssh won't accept a password from standard input, it requires
          > it to be entered into the terminal.[/color]

          Ah ok, i thought it did.

          --
          Lawrence "Rhymes" Oluyede
          Blogger ist ein Veröffentlichungs-Tool von Google, mit dem du ganz einfach deine Gedanken der Welt mitteilen kannst. Mit Blogger kannst du problemlos Texte, Fotos und Videos in deinem persönlichen Blog oder deinem Team-Blog veröffentlichen.

          Comment

          • Martin Franklin

            #6
            Re: Using python with SSH?

            On Wed, 2003-12-17 at 23:00, Steve wrote:[color=blue]
            > Hi,
            >
            > I need to run a remote program from a server that I have access rights
            > to. I want to be able to do this from within in my python script and I
            > want to use ssh (or anything else that will work). However, doing
            > something like:
            >
            > ssh -l user host "/path/to/program"
            >
            > prompts for a password and I don't know how to supply one via a python
            > script. Is there an easy way out? Can I log onto to the other machine
            > via ssh somehow? Does a python script support this? Thanks!
            >
            > Steve[/color]

            Steve,


            You don't say if you are running on Windows or not but this is a ssh
            wrapper I use that uses the pexpect library...:


            import pexpect
            import sys
            import re
            import os


            PROMPT = "\$|\%|\>"


            class SSH:
            def __init__(self, user, password, host):
            self.child = pexpect.spawn(" ssh %s@%s"%(user, host))
            i = self.child.expe ct(['assword:', r"yes/no"], timeout=120)
            if i==0:
            self.child.send line(password)
            elif i==1:
            self.child.send line("yes")
            self.child.expe ct("assword:", timeout=120)
            self.child.send line(password)
            self.child.expe ct(PROMPT)


            def command(self, command):
            """send a command and return the response"""
            self.child.send line(command)
            self.child.expe ct(PROMPT)
            response = self.child.befo re
            return response

            def close(self):
            """close the connection"""
            self.child.clos e()


            if __name__=="__ma in__":
            import getpass
            password = getpass.getpass ("Password: ")
            ssh = SSH("RemoteUser name", password, "RemoteHost ")
            print ssh.command("pw d")
            ssh.close()






            --
            Martin Franklin <mfranklin1@gat wick.westerngec o.slb.com>


            Comment

            • Albert Hofkamp

              #7
              Re: Using python with SSH?

              On Thu, 18 Dec 2003 10:06:02 +0000, Martin Franklin <mfranklin1@gat wick.westerngec o.slb.com> wrote:[color=blue]
              > On Wed, 2003-12-17 at 23:00, Steve wrote:[color=green]
              >> Hi,
              >>
              >> I need to run a remote program from a server that I have access rights
              >> to. I want to be able to do this from within in my python script and I
              >> want to use ssh (or anything else that will work). However, doing
              >> something like:
              >>
              >> ssh -l user host "/path/to/program"
              >>
              >> prompts for a password and I don't know how to supply one via a python
              >> script. Is there an easy way out? Can I log onto to the other machine[/color][/color]

              If your sysadmin allows it, you can configure ssh to connect to another
              machine without prompting for a password, at least at Linux systems.


              Albert
              --
              Unlike popular belief, the .doc format is not an open publically available format.

              Comment

              Working...