Paramiko help - processing multiple commands

Discussion in 'Python' started by Frank Ruiz, Jun 24, 2009.

  1. Frank Ruiz

    Frank Ruiz Guest

    Greetings,

    I am trying to process multiple commands using paramiko. I have
    searched other threads, and I think my use case is a little different.
    I am trying to login to a storage node that has a special shell, and
    as such I cant execute a script on the storage node side.

    I am also trying to avoid using pexpect because I hate making system
    calls.. hence my leaning towards paramiko.

    Was hoping someone could help me identify a way to process multiple
    commands using paramiko.

    I have two commands listed below, however only one is getting processed.

    Any help is much appreciated.

    Thanks!

    Here is my script:

    #!/usr/bin/env python


    #-Modules---------------------------------------------------------------------
    import optparse
    import sys
    import paramiko


    #-Variables-------------------------------------------------------------------
    plog = 'storagessh.log'
    suser = 'root'


    #-Config----------------------------------------------------------------------

    #-Subs-Defined----------------------------------------------------------------
    def options():
    global hostname
    global goldenimage
    global lunclone
    global sshport

    usage = "usage: %prog [options] -n <nodename> -g <goldenimage> -l <lun>"

    parser = optparse.OptionParser(usage)

    parser.add_option("-n", "--node",
    dest="hostname",
    help="Name of storage node you are connecting to.")
    parser.add_option("-g", "--gold",
    dest="goldenimage",
    help="Name of goldenimage to clone.")
    parser.add_option("-l", "--lun",
    dest="lunclone",
    help="Name of lun to create.")
    parser.add_option("-p", "--port",
    dest="sshport",
    default=22,
    help="SSH port number.")
    options, args = parser.parse_args()

    if not options.hostname:
    parser.error("Missing hostname argument.")
    exit
    elif not options.goldenimage:
    parser.error("Missing goldenimage argument.")
    exit
    elif not options.lunclone:
    parser.error("Missing lun argument.")
    exit

    hostname = options.hostname
    goldenimage = options.goldenimage
    lunclone = options.lunclone
    sshport = options.sshport

    def storagessh():
    paramiko.util.log_to_file(plog)
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.connect(hostname, sshport, suser)
    stdin, stdout, stderr = client.exec_command('show')
    stdin, stdout, stderr = client.exec_command('help')
    print stdout.read()
    client.close()




    #--Initialization-------------------------------------------------------------
    if __name__ == "__main__":
    options()
    storagessh()
    Frank Ruiz, Jun 24, 2009
    #1
    1. Advertising

  2. Frank Ruiz

    Jon Clements Guest

    On Jun 24, 11:22 pm, Frank Ruiz <> wrote:
    > Greetings,
    >
    > I am trying to process multiple commands using paramiko. I have
    > searched other threads, and I think my use case is a little different.
    > I am trying to login to a storage node that has a special shell, and
    > as such I cant execute a script on the storage node side.
    >
    > I am also trying to avoid using pexpect because I hate making system
    > calls.. hence my leaning towards paramiko.
    >
    > Was hoping someone could help me identify a way to process multiple
    > commands using paramiko.
    >
    > I have two commands listed below, however only one is getting processed.
    >
    > Any help is much appreciated.
    >
    > Thanks!
    >
    > Here is my script:
    >
    > #!/usr/bin/env python
    >
    > #-Modules---------------------------------------------------------------------
    > import optparse
    > import sys
    > import paramiko
    >
    > #-Variables-------------------------------------------------------------------
    > plog = 'storagessh.log'
    > suser = 'root'
    >
    > #-Config----------------------------------------------------------------------
    >
    > #-Subs-Defined----------------------------------------------------------------
    > def options():
    >     global hostname
    >     global goldenimage
    >     global lunclone
    >     global sshport
    >
    >     usage = "usage: %prog [options] -n <nodename> -g <goldenimage> -l <lun>"
    >
    >     parser = optparse.OptionParser(usage)
    >
    >     parser.add_option("-n", "--node",
    >                       dest="hostname",
    >                       help="Name of storage node you are connecting to.")
    >     parser.add_option("-g", "--gold",
    >                       dest="goldenimage",
    >                       help="Name of goldenimage to clone.")
    >     parser.add_option("-l", "--lun",
    >                       dest="lunclone",
    >                       help="Name of lun to create..")
    >     parser.add_option("-p", "--port",
    >                       dest="sshport",
    >                       default=22,
    >                       help="SSH port number.")
    >     options, args = parser.parse_args()
    >
    >     if not options.hostname:
    >         parser.error("Missing hostname argument.")
    >         exit
    >     elif not options.goldenimage:
    >         parser.error("Missing goldenimage argument.")
    >         exit
    >     elif not options.lunclone:
    >         parser.error("Missing lun argument.")
    >         exit
    >
    >     hostname = options.hostname
    >     goldenimage = options.goldenimage
    >     lunclone = options.lunclone
    >     sshport = options.sshport
    >
    > def storagessh():
    >     paramiko.util.log_to_file(plog)
    >     client = paramiko.SSHClient()
    >     client.load_system_host_keys()
    >     client.connect(hostname, sshport, suser)
    >     stdin, stdout, stderr = client.exec_command('show')
    >     stdin, stdout, stderr = client.exec_command('help')
    >     print stdout.read()
    >     client.close()
    >
    > #--Initialization-------------------------------------------------------------
    > if __name__ == "__main__":
    >     options()
    >     storagessh()


    Again, as you were asked on the original post -- full tracebacks and
    explain "what is not working".

    The use of global variables scares me -- why are those needed?
    Jon Clements, Jun 25, 2009
    #2
    1. Advertising

  3. Frank Ruiz

    Frank Ruiz Guest

    Hi Jon,

    Thanks for the reply. So there are no errors. Essentially everything
    runs as planned. Sorry for being ignorant, but I am not sure if there
    is another way for providing trace data. I will look into what other
    debugging I can provide.

    Essentially what happens is that only the second command gets
    processed and the first is ignored.

    As far as the global variables are concerned, I am not too sure what
    the best way is to allow my variables to be seen by another sub. In
    the script I am using optparse, however in order for the variables to
    make any sense to my storagessh sub, I have to declare them as global,
    since all the variables within my options sub have a local scope.

    I am not too sure of a better way to do this. I don't like it much
    either. Hopefully you can help me shed some light on this in terms of
    best practice.

    Normally I declare variable scope outside of my subroutines where required.

    However since variable scope is local within a subroutine, seems like
    I have to declare them global in order for the other subs to see it.

    Anyhow.. your feedback has been much appreciated.. The script is still
    a work in progress, so I plan to do so more cosmetic enhancements once
    I review it a few more times.

    Thanks!

    On Wed, Jun 24, 2009 at 4:34 PM, Jon Clements<> wrote:
    > On Jun 24, 11:22 pm, Frank Ruiz <> wrote:
    >> Greetings,
    >>
    >> I am trying to process multiple commands using paramiko. I have
    >> searched other threads, and I think my use case is a little different.
    >> I am trying to login to a storage node that has a special shell, and
    >> as such I cant execute a script on the storage node side.
    >>
    >> I am also trying to avoid using pexpect because I hate making system
    >> calls.. hence my leaning towards paramiko.
    >>
    >> Was hoping someone could help me identify a way to process multiple
    >> commands using paramiko.
    >>
    >> I have two commands listed below, however only one is getting processed.
    >>
    >> Any help is much appreciated.
    >>
    >> Thanks!
    >>
    >> Here is my script:
    >>
    >> #!/usr/bin/env python
    >>
    >> #-Modules---------------------------------------------------------------------
    >> import optparse
    >> import sys
    >> import paramiko
    >>
    >> #-Variables-------------------------------------------------------------------
    >> plog = 'storagessh.log'
    >> suser = 'root'
    >>
    >> #-Config----------------------------------------------------------------------
    >>
    >> #-Subs-Defined----------------------------------------------------------------
    >> def options():
    >>     global hostname
    >>     global goldenimage
    >>     global lunclone
    >>     global sshport
    >>
    >>     usage = "usage: %prog [options] -n <nodename> -g <goldenimage> -l <lun>"
    >>
    >>     parser = optparse.OptionParser(usage)
    >>
    >>     parser.add_option("-n", "--node",
    >>                       dest="hostname",
    >>                       help="Name of storage node you are connecting to.")
    >>     parser.add_option("-g", "--gold",
    >>                       dest="goldenimage",
    >>                       help="Name of goldenimage to clone.")
    >>     parser.add_option("-l", "--lun",
    >>                       dest="lunclone",
    >>                       help="Name of lun to create.")
    >>     parser.add_option("-p", "--port",
    >>                       dest="sshport",
    >>                       default=22,
    >>                       help="SSH port number.")
    >>     options, args = parser.parse_args()
    >>
    >>     if not options.hostname:
    >>         parser.error("Missing hostname argument.")
    >>         exit
    >>     elif not options.goldenimage:
    >>         parser.error("Missing goldenimage argument.")
    >>         exit
    >>     elif not options.lunclone:
    >>         parser.error("Missing lun argument.")
    >>         exit
    >>
    >>     hostname = options.hostname
    >>     goldenimage = options.goldenimage
    >>     lunclone = options.lunclone
    >>     sshport = options.sshport
    >>
    >> def storagessh():
    >>     paramiko.util.log_to_file(plog)
    >>     client = paramiko.SSHClient()
    >>     client.load_system_host_keys()
    >>     client.connect(hostname, sshport, suser)
    >>     stdin, stdout, stderr = client.exec_command('show')
    >>     stdin, stdout, stderr = client.exec_command('help')
    >>     print stdout.read()
    >>     client.close()
    >>
    >> #--Initialization-------------------------------------------------------------
    >> if __name__ == "__main__":
    >>     options()
    >>     storagessh()

    >
    > Again, as you were asked on the original post -- full tracebacks and
    > explain "what is not working".
    >
    > The use of global variables scares me -- why are those needed?
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    Frank Ruiz, Jun 25, 2009
    #3
  4. Frank Ruiz

    Noah Dain Guest

    On Wed, Jun 24, 2009 at 8:35 PM, Frank Ruiz<> wrote:
    > Hi Jon,
    >
    > Thanks for the reply. So there are no errors. Essentially everything
    > runs as planned. Sorry for being ignorant, but I am not sure if there
    > is another way for providing trace data. I will look into what other
    > debugging I can provide.
    >
    > Essentially what happens is that only the second command gets
    > processed and the first is ignored.
    >
    > As far as the global variables are concerned, I am not too sure what
    > the best way is to allow my variables to be seen by another sub. In
    > the script I am using optparse, however in order for the variables to
    > make any sense to my storagessh sub, I have to declare them as global,
    > since all the variables within my options sub have a local scope.
    >
    > I am not too sure of a better way to do this. I don't like it much
    > either. Hopefully you can help me shed some light on this in terms of
    > best practice.
    >
    > Normally I declare variable scope outside of my subroutines where required.
    >
    > However since variable scope is local within a subroutine, seems like
    > I have to declare them global in order for the other subs to see it.
    >
    > Anyhow.. your feedback has been much appreciated.. The script is still
    > a work in progress, so I plan to do so more cosmetic enhancements once
    > I review it a few more times.
    >
    > Thanks!
    >
    > On Wed, Jun 24, 2009 at 4:34 PM, Jon Clements<> wrote:
    >> On Jun 24, 11:22 pm, Frank Ruiz <> wrote:
    >>> Greetings,
    >>>
    >>> I am trying to process multiple commands using paramiko. I have
    >>> searched other threads, and I think my use case is a little different.
    >>> I am trying to login to a storage node that has a special shell, and
    >>> as such I cant execute a script on the storage node side.
    >>>
    >>> I am also trying to avoid using pexpect because I hate making system
    >>> calls.. hence my leaning towards paramiko.
    >>>
    >>> Was hoping someone could help me identify a way to process multiple
    >>> commands using paramiko.
    >>>
    >>> I have two commands listed below, however only one is getting processed..
    >>>
    >>> Any help is much appreciated.
    >>>
    >>> Thanks!
    >>>
    >>> Here is my script:
    >>>
    >>> #!/usr/bin/env python
    >>>
    >>> #-Modules---------------------------------------------------------------------
    >>> import optparse
    >>> import sys
    >>> import paramiko
    >>>
    >>> #-Variables-------------------------------------------------------------------
    >>> plog = 'storagessh.log'
    >>> suser = 'root'
    >>>
    >>> #-Config----------------------------------------------------------------------
    >>>
    >>> #-Subs-Defined----------------------------------------------------------------
    >>> def options():
    >>>     global hostname
    >>>     global goldenimage
    >>>     global lunclone
    >>>     global sshport
    >>>
    >>>     usage = "usage: %prog [options] -n <nodename> -g <goldenimage> -l <lun>"
    >>>
    >>>     parser = optparse.OptionParser(usage)
    >>>
    >>>     parser.add_option("-n", "--node",
    >>>                       dest="hostname",
    >>>                       help="Name of storage node you are connecting to.")
    >>>     parser.add_option("-g", "--gold",
    >>>                       dest="goldenimage",
    >>>                       help="Name of goldenimage to clone.")
    >>>     parser.add_option("-l", "--lun",
    >>>                       dest="lunclone",
    >>>                       help="Name of lun to create.")
    >>>     parser.add_option("-p", "--port",
    >>>                       dest="sshport",
    >>>                       default=22,
    >>>                       help="SSH port number.")
    >>>     options, args = parser.parse_args()
    >>>
    >>>     if not options.hostname:
    >>>         parser.error("Missing hostname argument.")
    >>>         exit
    >>>     elif not options.goldenimage:
    >>>         parser.error("Missing goldenimage argument.")
    >>>         exit
    >>>     elif not options.lunclone:
    >>>         parser.error("Missing lun argument.")
    >>>         exit
    >>>
    >>>     hostname = options.hostname
    >>>     goldenimage = options.goldenimage
    >>>     lunclone = options.lunclone
    >>>     sshport = options.sshport
    >>>
    >>> def storagessh():
    >>>     paramiko.util.log_to_file(plog)
    >>>     client = paramiko.SSHClient()
    >>>     client.load_system_host_keys()
    >>>     client.connect(hostname, sshport, suser)
    >>>     stdin, stdout, stderr = client.exec_command('show')
    >>>     stdin, stdout, stderr = client.exec_command('help')
    >>>     print stdout.read()
    >>>     client.close()
    >>>
    >>> #--Initialization-------------------------------------------------------------
    >>> if __name__ == "__main__":
    >>>     options()
    >>>     storagessh()

    >>
    >> Again, as you were asked on the original post -- full tracebacks and
    >> explain "what is not working".
    >>
    >> The use of global variables scares me -- why are those needed?
    >> --
    >> http://mail.python.org/mailman/listinfo/python-list
    >>

    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >


    this works for me:

    def storagessh():
    paramiko.util.log_to_file(plog)
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.connect(hostname, sshport, suser)
    stdin, stdout, stderr = client.exec_command('ps')
    print stdout.read()
    stdin, stdout, stderr = client.exec_command('help')
    print stdout.read()
    client.close()

    1) you reassign stdin, stdout, stderr so with your code you will never
    see the stdout of the first command ('show')
    2) the 'show' command did not exist on my system, so no output. I
    substituted 'ps' and added the print statement

    also, using user 'root' for dev code is a Bad Thing.

    --
    Noah Dain
    Noah Dain, Jun 25, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Pfaff

    Re: man pages for C commands (GCC commands)

    Ben Pfaff, Jun 24, 2003, in forum: C Programming
    Replies:
    4
    Views:
    3,950
    Thomas Stegen
    Jun 28, 2003
  2. Tim Stanka
    Replies:
    1
    Views:
    785
    Jeff Epler
    Aug 2, 2004
  3. sa6113
    Replies:
    0
    Views:
    265
    sa6113
    Oct 26, 2008
  4. Frank Ruiz

    Paramiko Help

    Frank Ruiz, Jun 24, 2009, in forum: Python
    Replies:
    1
    Views:
    451
    Jon Clements
    Jun 24, 2009
  5. J. Clifford Dyer
    Replies:
    0
    Views:
    628
    J. Clifford Dyer
    Jun 25, 2009
Loading...

Share This Page