class Rdpcmd::Rdpcmd

Public Class Methods

new(params={}) click to toggle source
# File lib/rdpcmd/rdpcmd.rb, line 6
def initialize(params={})
        @log=params.fetch('log','')
end

Public Instance Methods

cleanupremmina() click to toggle source
# File lib/rdpcmd/rdpcmd.rb, line 100
def cleanupremmina()
        systeml("xdotool windowactivate --sync #{@wid} key --clearmodifiers 'Control_R'")
        @log.debug("Sleeping for final things to settle")
        sleep 1
end
closecmd(sleepfor) click to toggle source
# File lib/rdpcmd/rdpcmd.rb, line 93
def closecmd(sleepfor)
        @log.debug("Sleeping for final things to settle")
        sleep sleepfor
        systeml("xdotool type 'exit'")
        systeml('xdotool key --clearmodifiers "Return"')
end
connect(opts={}) click to toggle source
# File lib/rdpcmd/rdpcmd.rb, line 41
def connect (opts={})
        rem=Remmina.new
        rem.readconfig

        @log.debug("Connecting to IP: #{opts['server']} with user #{opts['username']} and domain #{opts['domain']}")
        tempconfig=rem.genconfig(opts)

        @t = Tempfile.new(["rdpcmd",".remmina"])
        @t.write(tempconfig)
        @t.close

        cmdspawn="remmina -c #{@t.path}"
        @log.debug("Spawning Remmina with cmdline: #{cmdspawn}")

        @pid=Process.spawn(cmdspawn)

        @log.debug("Spawned Remmina with PID: #{@pid}")

        sleepfor=3
        @log.debug("Sleeping for #{sleepfor}")
        sleep(sleepfor)

        wait4window(@pid,opts['server'])
        if @wid.nil? then
                @log.error("Cannot find Remmina window, is Remmina and xdotool installed? Connection problem.")
                return false
        end

        @log.debug("Remmina contacted, window ID: #{@wid}")

end
copyfile(src,dest,sleepfor=0.5) click to toggle source
# File lib/rdpcmd/rdpcmd.rb, line 128
def copyfile(src,dest,sleepfor=0.5) 
        sendline("copy con #{dest}")
        sleep(sleepfor)
        sendfile(src, sleepfor)
        sleep(sleepfor)
        systeml('xdotool key --clearmodifiers "Ctrl+Z" "Return"')
end
prepareremmina() click to toggle source
# File lib/rdpcmd/rdpcmd.rb, line 79
def prepareremmina()
        systeml("xdotool windowactivate --sync #{@wid} key --clearmodifiers 'Control_R'")
end
sendfile(file, sleepfor=0.5) click to toggle source
# File lib/rdpcmd/rdpcmd.rb, line 119
def sendfile(file, sleepfor=0.5) 
        input= File.new(file, "r")
        input.each do |line|
                sendline(line)
                sleep(sleepfor)
        end
        input.close
end
sendkeys(keys,clearmod=false) click to toggle source
# File lib/rdpcmd/rdpcmd.rb, line 111
def sendkeys(keys,clearmod=false)
        opts=''
        if clearmod then
                opts << " --clearmodifiers "
        end
        systeml("xdotool key #{opts} #{keys}")
end
sendline(cmd) click to toggle source
# File lib/rdpcmd/rdpcmd.rb, line 106
def sendline(cmd)
        systeml("xdotool type '#{cmd}'")
        systeml('xdotool key --clearmodifiers "Return"')
end
singleElevated(cmd,toexit=0) click to toggle source
# File lib/rdpcmd/rdpcmd.rb, line 136
def singleElevated(cmd,toexit=0) 
        prepareremmina()
        startrunele()
        sendline(cmd)
        if toexit > 0 then
                closecmd(toexit)
        end
        cleanupremmina()
end
singleNormal(cmd,toexit=0) click to toggle source
# File lib/rdpcmd/rdpcmd.rb, line 146
def singleNormal(cmd,toexit=0) 
        prepareremmina()
        startrun('cmd.exe')
        sendline(cmd)
        if toexit > 0 then
                closecmd(toexit)
        end
        cleanupremmina()
end
startrun(cmd) click to toggle source
# File lib/rdpcmd/rdpcmd.rb, line 83
def startrun(cmd)
        systeml("xdotool windowactivate --sync #{@wid} key 'Super+r' sleep 2 type '#{cmd}'")
        systeml('xdotool key --clearmodifiers "Return"')
end
startrunele(sleepfor=4) click to toggle source
# File lib/rdpcmd/rdpcmd.rb, line 88
def startrunele(sleepfor=4)
        startrun("powershell Start-Process cmd -Verb runAs")
        systeml("xdotool sleep 3 key 'Alt+y' sleep #{sleepfor}")
end
systeml(str) click to toggle source
# File lib/rdpcmd/rdpcmd.rb, line 36
def systeml(str)
        @log.debug(str)
        system(str)
end
terminate() click to toggle source
# File lib/rdpcmd/rdpcmd.rb, line 73
def terminate()
        @t.unlink
        @log.debug("Killing Remmina with PID: #{@pid}")
        Process.kill("KILL", @pid)
end
wait4window(pid,ip) click to toggle source
# File lib/rdpcmd/rdpcmd.rb, line 10
def wait4window(pid,ip)
        waitfor=5
        wid=nil

        waitfor.times do |i|
                @log.debug("Waiting for Remmina to start: #{i}")
                f = IO.popen("xdotool search --all --pid #{pid} --name #{ip}")
                # puts f.readlines
                idline=''
                f.each do |line|
                        @log.debug("Parsed xdotool line: #{line}")
                        idline=line
                end
                unless idline.to_s.strip.empty? then
                        wid=idline.chomp
                        @log.debug("Taking wid: #{wid}")
                        f.close
                        break
                end
                f.close
                sleep(1)
        end
        @wid=wid
        return wid
end