Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Paramiko help - processing multiple commands

Reply
Thread Tools

Re: Paramiko help - processing multiple commands

 
 
J. Clifford Dyer
Guest
Posts: n/a
 
      06-25-2009
On Wed, 2009-06-24 at 15:22 -0700, 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
>


It looks like you are trying to create a configuration class, but going
through extra gyrations to use global variables instead. Perl's class
system is rather convoluted, but it is straightforward in python, and
will make your code easier to maintain. I recommend you take a look at
the section on classes in the python tutorial, and play around with it.
Not everything needs to be a class in python, but in certain cases (and
this is one of them) it will make things much cleaner.

> 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()
>


You just did it. You processed two commands. The only problem is that
you wrote values to stdin, stdout, and stderr with the show command, and
then immediately overwrote them with the help command. Use different
variable names for each command (like stdout_show and stdout_help), and
you should be fine.

>
>
>
> #--Initialization-------------------------------------------------------------
> if __name__ == "__main__":
> options()
> storagessh()


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Paramiko help - processing multiple commands Frank Ruiz Python 3 06-25-2009 04:16 AM
Paramiko Help Frank Ruiz Python 1 06-24-2009 09:04 PM
using paramiko problem? please help me sa6113 Python 0 10-26-2008 12:29 PM
Need Help Differentiating Bad Commands From Incomplete Commands Tim Stanka Python 1 08-02-2004 02:08 AM
Re: man pages for C commands (GCC commands) Ben Pfaff C Programming 4 06-28-2003 06:21 PM



Advertisments