Python & WinRM

Top Page
Attachments:
Message as email
+ (text/plain)
+ winexec (text/plain)
+ (text/plain)
Delete this message
Reply to this message
Author: Kevin Fries
Date:  
To: Main PLUG discussion list
Subject: Python & WinRM
I wrote this simple script to execute powershell scripts on our windows
servers from my Linux box. I am trying to learn Python better, but have
been unable to figure out how to re-write this script in Python.

- ostruct can easily be replaced with a dictionary... its not as
elegant, but functional enough

- optparse is also implemented in Python, so that will be no big deal.

The big stumbling block is WinRM

I do not, nor is it an option to install openwsman on the Linux boxes.
I am trying to bring our Windows boxes under the same level of
management as our Linux boxes, and the management servers are Linux. So
the solution needs to be Linux.

This is more of a learning exercise than anything else, so the most
elegant solution is not being asked for. I just don't know how to
implement the functionality I am getting out of the WinRM gem in Ruby.

Pointers????

Thanks
Kevin Fries



#!/usr/bin/ruby

require 'ostruct'
require 'optparse'
require 'winrm'

PROGRAM_NAME = $0
PROGRAM_VERSION = 1.0
REALM = 'MYREALM'

options = OpenStruct.new

optparse = OptionParser.new do |opts|
opts.banner = <<BANNER
This program will run a powerscript file on a Windows Server from a Linux host

Usage: #{PROGRAM_NAME} [options] <script filename> <list of machines to run on>
BANNER

   opts.on('-h', '--help', 'Show this message') do
      puts opts
      exit
   end


   opts.on_tail('--version', 'Show version') do
      puts "Version: #{PROGRAM_VERSION}"
      exit
   end
end


optparse.parse!

options.script = ARGV[0] unless ARGV[0].nil?
ARGV.shift

options.servers = Array.new
until ARGV[0].nil? do
options.servers << ARGV[0]
ARGV.shift
end

raise "Script file not specified" if options.script.nil?
raise "Servers to run script on, not specified" if options.servers.size == 0

begin
fileio = File.new(options.script, "r")
rescue Exception => e
puts e
exit 1
end

raise "Invalid Filename specified" unless fileio.kind_of?(IO)

options.servers.each do |server|
   begin
      fileio.rewind
      endpoint = "http://#{server}:5985/wsman"
      winrm = WinRM::WinRMWebService.new(endpoint, :kerberos, :realm => REALM)
      output = winrm.run_powershell_script(fileio)
   rescue GSSAPI::GssApiError => gae
      system 'kinit'
      retry
   rescue Exception => e
      puts "#{e.class}"
      puts "#{e.message}"
      exit
   end


   puts "Executed command on #{server}, output ="
   puts "-"*78
   output[:data].each do |line|
      STDOUT.print "#{line[:stdout]}" if line.key?(:stdout)
      STDERR.print "ERROR: #{line[:stderr]}" if line.key?(:stderr)
   end
   puts "-"*78
   puts
end

---------------------------------------------------
PLUG-discuss mailing list -
To subscribe, unsubscribe, or to change your mail settings:
http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-discuss