Windows - Get current logged user

Discussion in 'Ruby' started by Rodrigo Bermejo, Sep 28, 2007.

  1. Hello /.


    I need to know how is the user who is currently logged to an specific
    machine.
    I'm able to log into the box using 'net/telnet' and then do whatever I
    need to
    get the user logged. My 1st try was to take a look at the %USERNAME%
    ...jeje

    I've found that doing this works but is not a cleaver solution:

    def get_user

    require 'win32/eventlog'
    include Win32

    # Open the security log
    log = EventLog.open('Security')

    #Clear it
    log.clear

    #Close it
    log.close

    #Open the new log
    log = EventLog.open('Security')

    user=""

    #There is just one record
    log.read {|l|
    user=l.description.scan(/Client User Name:\t(.+)\r/).flatten[0]
    }

    user

    end


    Any idea of how accomplish this with a more robust method /?

    Thanks
    --
    Posted via http://www.ruby-forum.com/.
     
    Rodrigo Bermejo, Sep 28, 2007
    #1
    1. Advertising

  2. Rodrigo Bermejo

    James Tucker Guest

    Rodrigo Bermejo wrote:
    > Hello /.
    >
    >
    > I need to know how is the user who is currently logged to an specific
    > machine.
    > I'm able to log into the box using 'net/telnet' and then do whatever I
    > need to
    > get the user logged. My 1st try was to take a look at the %USERNAME%
    > ...jeje
    >
    > I've found that doing this works but is not a cleaver solution:
    >
    > def get_user
    >
    > require 'win32/eventlog'
    > include Win32
    >
    > # Open the security log
    > log = EventLog.open('Security')
    >
    > #Clear it
    > log.clear
    >
    > #Close it
    > log.close
    >
    > #Open the new log
    > log = EventLog.open('Security')
    >
    > user=""
    >
    > #There is just one record
    > log.read {|l|
    > user=l.description.scan(/Client User Name:\t(.+)\r/).flatten[0]
    > }
    >
    > user
    >
    > end
    >
    >
    > Any idea of how accomplish this with a more robust method /?
    >
    > Thanks


    def get_user; ENV['USERNAME']; end
     
    James Tucker, Sep 29, 2007
    #2
    1. Advertising

  3. James Tucker wrote:
    > def get_user; ENV['USERNAME']; end


    The issue here is that it will return the username Im using to log into
    the box
    using net/telnet.

    Iam looking for the username of the guy seated in front of the computer.
    Something similar to the UNIX 'finger,who,w' commands

    I've already tried looking in the registry with out success.


    /. thNKs

    --
    Posted via http://www.ruby-forum.com/.
     
    Rodrigo Bermejo, Sep 29, 2007
    #3
  4. Rodrigo Bermejo wrote:
    > James Tucker wrote:
    >> def get_user; ENV['USERNAME']; end

    >
    > The issue here is that it will return the username Im using to log into
    > the box
    > using net/telnet.
    >
    > Iam looking for the username of the guy seated in front of the computer.
    > Something similar to the UNIX 'finger,who,w' commands
    >
    > I've already tried looking in the registry with out success.


    require 'windows/system_info'
    include Windows::SystemInfo

    buffer = 0.chr * 256
    nsize = [buffer.size].pack("L")

    unless GetUserName.call(buffer, nsize)
    raise 'Something went wrong'
    end

    length = nsize.unpack("L")[0]
    username = buffer[0 ... length].chop
    username

    Regards,

    Dan
     
    Daniel Berger, Oct 21, 2007
    #4
  5. Re: Windows - Get current logged user

    On Oct 21, 5:38 pm, Daniel Berger <> wrote:
    > Rodrigo Bermejo wrote:
    > > James Tucker wrote:
    > >> def get_user; ENV['USERNAME']; end

    >
    > > The issue here is that it will return the username Im using to log into
    > > the box
    > > using net/telnet.

    >
    > > Iam looking for the username of the guy seated in front of the computer.
    > > Something similar to the UNIX 'finger,who,w' commands

    >
    > > I've already tried looking in the registry with out success.

    >
    > require 'windows/system_info'
    > include Windows::SystemInfo
    >
    > buffer = 0.chr * 256
    > nsize = [buffer.size].pack("L")
    >
    > unless GetUserName.call(buffer, nsize)
    > raise 'Something went wrong'
    > end
    >
    > length = nsize.unpack("L")[0]
    > username = buffer[0 ... length].chop
    > username
    >
    > Regards,
    >
    > Dan


    Hi Daniel,

    Your suggestion looks neat! However, I ran into trouble running it.

    I got the error message:
    "K:/_Utilities/Ruby_1.8.2-15/ruby/lib/ruby/site_ruby/1.8/rubygems/
    custom_require.rb:27:in `gem_original_require': No such file to load
    -- windows/system_info (LoadError)
    from K:/_Utilities/Ruby_1.8.2-15/ruby/lib/ruby/site_ruby/1.8/rubygems/
    custom_require.rb:27:in `require'
    from GetUsername.rb:4
    >Exit code: 1"


    My line #4 was the require statement.

    I tried "gem install windows", which yielded:
    Could not find windows (> 0) in the repository

    I tried "gem install windows/system_info", and got:
    Could not find windows/system_info (> 0) in the repository

    Would you please point out where I went wrong?

    Thanks in Advance,
    Richard
     
    RichardOnRails, Oct 22, 2007
    #5
  6. Re: Windows - Get current logged user

    RichardOnRails wrote:
    > On Oct 21, 5:38 pm, Daniel Berger <> wrote:
    >> Rodrigo Bermejo wrote:
    >>> James Tucker wrote:
    >>>> def get_user; ENV['USERNAME']; end
    >>> The issue here is that it will return the username Im using to log into
    >>> the box
    >>> using net/telnet.
    >>> Iam looking for the username of the guy seated in front of the computer.
    >>> Something similar to the UNIX 'finger,who,w' commands
    >>> I've already tried looking in the registry with out success.

    >> require 'windows/system_info'
    >> include Windows::SystemInfo
    >>
    >> buffer = 0.chr * 256
    >> nsize = [buffer.size].pack("L")
    >>
    >> unless GetUserName.call(buffer, nsize)
    >> raise 'Something went wrong'
    >> end
    >>
    >> length = nsize.unpack("L")[0]
    >> username = buffer[0 ... length].chop
    >> username
    >>
    >> Regards,
    >>
    >> Dan

    >
    > Hi Daniel,
    >
    > Your suggestion looks neat! However, I ran into trouble running it.
    >
    > I got the error message:
    > "K:/_Utilities/Ruby_1.8.2-15/ruby/lib/ruby/site_ruby/1.8/rubygems/
    > custom_require.rb:27:in `gem_original_require': No such file to load
    > -- windows/system_info (LoadError)
    > from K:/_Utilities/Ruby_1.8.2-15/ruby/lib/ruby/site_ruby/1.8/rubygems/
    > custom_require.rb:27:in `require'
    > from GetUsername.rb:4
    >> Exit code: 1"

    >
    > My line #4 was the require statement.
    >
    > I tried "gem install windows", which yielded:
    > Could not find windows (> 0) in the repository
    >
    > I tried "gem install windows/system_info", and got:
    > Could not find windows/system_info (> 0) in the repository
    >
    > Would you please point out where I went wrong?


    Whoops, forgot to mention the gem. You want: gem install windows-pr

    Regards,

    Dan
     
    Daniel Berger, Oct 22, 2007
    #6
  7. Re: Windows - Get current logged user

    RichardOnRails wrote:
    > On Oct 21, 5:38 pm, Daniel Berger <> wrote:
    >> Rodrigo Bermejo wrote:
    >>> James Tucker wrote:
    >>>> def get_user; ENV['USERNAME']; end
    >>> The issue here is that it will return the username Im using to log into
    >>> the box
    >>> using net/telnet.
    >>> Iam looking for the username of the guy seated in front of the computer.
    >>> Something similar to the UNIX 'finger,who,w' commands
    >>> I've already tried looking in the registry with out success.

    >> require 'windows/system_info'
    >> include Windows::SystemInfo
    >>
    >> buffer = 0.chr * 256
    >> nsize = [buffer.size].pack("L")
    >>
    >> unless GetUserName.call(buffer, nsize)
    >> raise 'Something went wrong'
    >> end
    >>
    >> length = nsize.unpack("L")[0]
    >> username = buffer[0 ... length].chop
    >> username
    >>
    >> Regards,
    >>
    >> Dan

    >
    > Hi Daniel,
    >
    > Your suggestion looks neat! However, I ran into trouble running it.
    >
    > I got the error message:
    > "K:/_Utilities/Ruby_1.8.2-15/ruby/lib/ruby/site_ruby/1.8/rubygems/
    > custom_require.rb:27:in `gem_original_require': No such file to load
    > -- windows/system_info (LoadError)
    > from K:/_Utilities/Ruby_1.8.2-15/ruby/lib/ruby/site_ruby/1.8/rubygems/
    > custom_require.rb:27:in `require'
    > from GetUsername.rb:4
    >> Exit code: 1"

    >
    > My line #4 was the require statement.
    >
    > I tried "gem install windows", which yielded:
    > Could not find windows (> 0) in the repository
    >
    > I tried "gem install windows/system_info", and got:
    > Could not find windows/system_info (> 0) in the repository
    >
    > Would you please point out where I went wrong?


    Actually, you can use the sys-admin gem, which abstracts that for you:

    require 'sys/admin'
    include Sys

    p Admin.get_login

    Regards,

    Dan
     
    Daniel Berger, Oct 23, 2007
    #7
  8. Re: Windows - Get current logged user

    On Oct 22, 7:05 pm, Daniel Berger <> wrote:
    > RichardOnRails wrote:
    > > On Oct 21, 5:38 pm, Daniel Berger <> wrote:
    > >> Rodrigo Bermejo wrote:
    > >>> James Tucker wrote:
    > >>>> def get_user; ENV['USERNAME']; end
    > >>> The issue here is that it will return the username Im using to log into
    > >>> the box
    > >>> using net/telnet.
    > >>> Iam looking for the username of the guy seated in front of the computer.
    > >>> Something similar to the UNIX 'finger,who,w' commands
    > >>> I've already tried looking in the registry with out success.
    > >> require 'windows/system_info'
    > >> include Windows::SystemInfo

    >
    > >> buffer = 0.chr * 256
    > >> nsize = [buffer.size].pack("L")

    >
    > >> unless GetUserName.call(buffer, nsize)
    > >> raise 'Something went wrong'
    > >> end

    >
    > >> length = nsize.unpack("L")[0]
    > >> username = buffer[0 ... length].chop
    > >> username

    >
    > >> Regards,

    >
    > >> Dan

    >
    > > Hi Daniel,

    >
    > > Your suggestion looks neat! However, I ran into trouble running it.

    >
    > > I got the error message:
    > > "K:/_Utilities/Ruby_1.8.2-15/ruby/lib/ruby/site_ruby/1.8/rubygems/
    > > custom_require.rb:27:in `gem_original_require': No such file to load
    > > -- windows/system_info (LoadError)
    > > from K:/_Utilities/Ruby_1.8.2-15/ruby/lib/ruby/site_ruby/1.8/rubygems/
    > > custom_require.rb:27:in `require'
    > > from GetUsername.rb:4
    > >> Exit code: 1"

    >
    > > My line #4 was the require statement.

    >
    > > I tried "gem install windows", which yielded:
    > > Could not find windows (> 0) in the repository

    >
    > > I tried "gem install windows/system_info", and got:
    > > Could not find windows/system_info (> 0) in the repository

    >
    > > Would you please point out where I went wrong?

    >
    > Actually, you can use the sys-admin gem, which abstracts that for you:
    >
    > require 'sys/admin'
    > include Sys
    >
    > p Admin.get_login
    >
    > Regards,
    >
    > Dan


    On Oct 22, 7:05 pm, Daniel Berger <> wrote:
    > RichardOnRails wrote:
    > > On Oct 21, 5:38 pm, Daniel Berger <> wrote:
    > >> Rodrigo Bermejo wrote:
    > >>> James Tucker wrote:
    > >>>> def get_user; ENV['USERNAME']; end
    > >>> The issue here is that it will return the username Im using to log into
    > >>> the box
    > >>> using net/telnet.
    > >>> Iam looking for the username of the guy seated in front of the computer.
    > >>> Something similar to the UNIX 'finger,who,w' commands
    > >>> I've already tried looking in the registry with out success.
    > >> require 'windows/system_info'
    > >> include Windows::SystemInfo

    >
    > >> buffer = 0.chr * 256
    > >> nsize = [buffer.size].pack("L")

    >
    > >> unless GetUserName.call(buffer, nsize)
    > >> raise 'Something went wrong'
    > >> end

    >
    > >> length = nsize.unpack("L")[0]
    > >> username = buffer[0 ... length].chop
    > >> username

    >
    > >> Regards,

    >
    > >> Dan

    >
    > > Hi Daniel,

    >
    > > Your suggestion looks neat! However, I ran into trouble running it.

    >
    > > I got the error message:
    > > "K:/_Utilities/Ruby_1.8.2-15/ruby/lib/ruby/site_ruby/1.8/rubygems/
    > > custom_require.rb:27:in `gem_original_require': No such file to load
    > > -- windows/system_info (LoadError)
    > > from K:/_Utilities/Ruby_1.8.2-15/ruby/lib/ruby/site_ruby/1.8/rubygems/
    > > custom_require.rb:27:in `require'
    > > from GetUsername.rb:4
    > >> Exit code: 1"

    >
    > > My line #4 was the require statement.

    >
    > > I tried "gem install windows", which yielded:
    > > Could not find windows (> 0) in the repository

    >
    > > I tried "gem install windows/system_info", and got:
    > > Could not find windows/system_info (> 0) in the repository

    >
    > > Would you please point out where I went wrong?

    >
    > Actually, you can use the sys-admin gem, which abstracts that for you:
    >
    > require 'sys/admin'
    > include Sys
    >
    > p Admin.get_login
    >
    > Regards,
    >
    > Dan


    Thanks, Dan. That's a nice project you composed. Works great. Many
    thanks for sharing it with the Ruby community ... and me in
    particular :)

    If you don't mind another question, can you speculate on why the
    command (on my WinXP-Pro/SP2 system with Ruby 1.8.2-15):
    K:\_Utilities\Ruby_1.8.2-15>ri sys-admin
    yields:
    Nothing known about sys-admin

    I tried a couple of variants to no avail:
    K:\_Utilities\Ruby_1.8.2-15>ri sys-admin-1.4.2-mswin32
    Bad argument: sys-admin-1.4.2-mswin32

    K:\_Utilities\Ruby_1.8.2-15\ruby\lib\ruby\gems\1.8\gems>ri sys-
    admin-1.4.2-mswin32
    Bad argument: sys-admin-1.4.2-mswin32

    K:\_Utilities\Ruby_1.8.2-15\ruby\lib\ruby\gems\1.8\gems>ri sys-admin
    Nothing known about sys-admin


    I presume the ri should return the contents of:
    K:\_Utilities\Ruby_1.8.2-15\ruby\lib\ruby\gems\1.8\gems\sys-
    admin-1.4.2-mswin32\README
    which is loaded with your documentation.

    I've never tried using ri heretofore. I tried it on some other
    packages just now and got the same "Nothing known" msg.

    Any ideas for this Ruby newbie (sort of)?

    Best wishes,
    Richard
     
    RichardOnRails, Oct 23, 2007
    #8
  9. Re: Windows - Get current logged user

    RichardOnRails wrote:
    > On Oct 22, 7:05 pm, Daniel Berger <> wrote:
    >> RichardOnRails wrote:
    >>> On Oct 21, 5:38 pm, Daniel Berger <> wrote:
    >>>> Rodrigo Bermejo wrote:
    >>>>> James Tucker wrote:
    >>>>>> def get_user; ENV['USERNAME']; end
    >>>>> The issue here is that it will return the username Im using to log into
    >>>>> the box
    >>>>> using net/telnet.
    >>>>> Iam looking for the username of the guy seated in front of the computer.
    >>>>> Something similar to the UNIX 'finger,who,w' commands
    >>>>> I've already tried looking in the registry with out success.
    >>>> require 'windows/system_info'
    >>>> include Windows::SystemInfo
    >>>> buffer = 0.chr * 256
    >>>> nsize = [buffer.size].pack("L")
    >>>> unless GetUserName.call(buffer, nsize)
    >>>> raise 'Something went wrong'
    >>>> end
    >>>> length = nsize.unpack("L")[0]
    >>>> username = buffer[0 ... length].chop
    >>>> username
    >>>> Regards,
    >>>> Dan
    >>> Hi Daniel,
    >>> Your suggestion looks neat! However, I ran into trouble running it.
    >>> I got the error message:
    >>> "K:/_Utilities/Ruby_1.8.2-15/ruby/lib/ruby/site_ruby/1.8/rubygems/
    >>> custom_require.rb:27:in `gem_original_require': No such file to load
    >>> -- windows/system_info (LoadError)
    >>> from K:/_Utilities/Ruby_1.8.2-15/ruby/lib/ruby/site_ruby/1.8/rubygems/
    >>> custom_require.rb:27:in `require'
    >>> from GetUsername.rb:4
    >>>> Exit code: 1"
    >>> My line #4 was the require statement.
    >>> I tried "gem install windows", which yielded:
    >>> Could not find windows (> 0) in the repository
    >>> I tried "gem install windows/system_info", and got:
    >>> Could not find windows/system_info (> 0) in the repository
    >>> Would you please point out where I went wrong?

    >> Actually, you can use the sys-admin gem, which abstracts that for you:
    >>
    >> require 'sys/admin'
    >> include Sys
    >>
    >> p Admin.get_login
    >>
    >> Regards,
    >>
    >> Dan

    >
    > On Oct 22, 7:05 pm, Daniel Berger <> wrote:
    >> RichardOnRails wrote:
    >>> On Oct 21, 5:38 pm, Daniel Berger <> wrote:
    >>>> Rodrigo Bermejo wrote:
    >>>>> James Tucker wrote:
    >>>>>> def get_user; ENV['USERNAME']; end
    >>>>> The issue here is that it will return the username Im using to log into
    >>>>> the box
    >>>>> using net/telnet.
    >>>>> Iam looking for the username of the guy seated in front of the computer.
    >>>>> Something similar to the UNIX 'finger,who,w' commands
    >>>>> I've already tried looking in the registry with out success.
    >>>> require 'windows/system_info'
    >>>> include Windows::SystemInfo
    >>>> buffer = 0.chr * 256
    >>>> nsize = [buffer.size].pack("L")
    >>>> unless GetUserName.call(buffer, nsize)
    >>>> raise 'Something went wrong'
    >>>> end
    >>>> length = nsize.unpack("L")[0]
    >>>> username = buffer[0 ... length].chop
    >>>> username
    >>>> Regards,
    >>>> Dan
    >>> Hi Daniel,
    >>> Your suggestion looks neat! However, I ran into trouble running it.
    >>> I got the error message:
    >>> "K:/_Utilities/Ruby_1.8.2-15/ruby/lib/ruby/site_ruby/1.8/rubygems/
    >>> custom_require.rb:27:in `gem_original_require': No such file to load
    >>> -- windows/system_info (LoadError)
    >>> from K:/_Utilities/Ruby_1.8.2-15/ruby/lib/ruby/site_ruby/1.8/rubygems/
    >>> custom_require.rb:27:in `require'
    >>> from GetUsername.rb:4
    >>>> Exit code: 1"
    >>> My line #4 was the require statement.
    >>> I tried "gem install windows", which yielded:
    >>> Could not find windows (> 0) in the repository
    >>> I tried "gem install windows/system_info", and got:
    >>> Could not find windows/system_info (> 0) in the repository
    >>> Would you please point out where I went wrong?

    >> Actually, you can use the sys-admin gem, which abstracts that for you:
    >>
    >> require 'sys/admin'
    >> include Sys
    >>
    >> p Admin.get_login
    >>
    >> Regards,
    >>
    >> Dan

    >
    > Thanks, Dan. That's a nice project you composed. Works great. Many
    > thanks for sharing it with the Ruby community ... and me in
    > particular :)
    >
    > If you don't mind another question, can you speculate on why the
    > command (on my WinXP-Pro/SP2 system with Ruby 1.8.2-15):
    > K:\_Utilities\Ruby_1.8.2-15>ri sys-admin
    > yields:
    > Nothing known about sys-admin
    >
    > I tried a couple of variants to no avail:
    > K:\_Utilities\Ruby_1.8.2-15>ri sys-admin-1.4.2-mswin32
    > Bad argument: sys-admin-1.4.2-mswin32
    >
    > K:\_Utilities\Ruby_1.8.2-15\ruby\lib\ruby\gems\1.8\gems>ri sys-
    > admin-1.4.2-mswin32
    > Bad argument: sys-admin-1.4.2-mswin32
    >
    > K:\_Utilities\Ruby_1.8.2-15\ruby\lib\ruby\gems\1.8\gems>ri sys-admin
    > Nothing known about sys-admin
    >
    >
    > I presume the ri should return the contents of:
    > K:\_Utilities\Ruby_1.8.2-15\ruby\lib\ruby\gems\1.8\gems\sys-
    > admin-1.4.2-mswin32\README
    > which is loaded with your documentation.
    >
    > I've never tried using ri heretofore. I tried it on some other
    > packages just now and got the same "Nothing known" msg.


    I'm actually not that familiar with ri myself. But, you should be able
    to call "ri Sys::Admin.get_login" and get a result.

    I wonder if I can set things up so that anyone who does 'ri sys-admin'
    will get the README. Anyone know?

    Regards,

    Dan
     
    Daniel Berger, Oct 23, 2007
    #9
  10. Re: Windows - Get current logged user

    On Oct 22, 11:36 pm, Daniel Berger <> wrote:
    > RichardOnRails wrote:
    > > On Oct 22, 7:05 pm, Daniel Berger <> wrote:
    > >> RichardOnRails wrote:
    > >>> On Oct 21, 5:38 pm, Daniel Berger <> wrote:
    > >>>> Rodrigo Bermejo wrote:
    > >>>>> James Tucker wrote:
    > >>>>>> def get_user; ENV['USERNAME']; end
    > >>>>> The issue here is that it will return the username Im using to log into
    > >>>>> the box
    > >>>>> using net/telnet.
    > >>>>> Iam looking for the username of the guy seated in front of the computer.
    > >>>>> Something similar to the UNIX 'finger,who,w' commands
    > >>>>> I've already tried looking in the registry with out success.
    > >>>> require 'windows/system_info'
    > >>>> include Windows::SystemInfo
    > >>>> buffer = 0.chr * 256
    > >>>> nsize = [buffer.size].pack("L")
    > >>>> unless GetUserName.call(buffer, nsize)
    > >>>> raise 'Something went wrong'
    > >>>> end
    > >>>> length = nsize.unpack("L")[0]
    > >>>> username = buffer[0 ... length].chop
    > >>>> username
    > >>>> Regards,
    > >>>> Dan
    > >>> Hi Daniel,
    > >>> Your suggestion looks neat! However, I ran into trouble running it.
    > >>> I got the error message:
    > >>> "K:/_Utilities/Ruby_1.8.2-15/ruby/lib/ruby/site_ruby/1.8/rubygems/
    > >>> custom_require.rb:27:in `gem_original_require': No such file to load
    > >>> -- windows/system_info (LoadError)
    > >>> from K:/_Utilities/Ruby_1.8.2-15/ruby/lib/ruby/site_ruby/1.8/rubygems/
    > >>> custom_require.rb:27:in `require'
    > >>> from GetUsername.rb:4
    > >>>> Exit code: 1"
    > >>> My line #4 was the require statement.
    > >>> I tried "gem install windows", which yielded:
    > >>> Could not find windows (> 0) in the repository
    > >>> I tried "gem install windows/system_info", and got:
    > >>> Could not find windows/system_info (> 0) in the repository
    > >>> Would you please point out where I went wrong?
    > >> Actually, you can use the sys-admin gem, which abstracts that for you:

    >
    > >> require 'sys/admin'
    > >> include Sys

    >
    > >> p Admin.get_login

    >
    > >> Regards,

    >
    > >> Dan

    >
    > > On Oct 22, 7:05 pm, Daniel Berger <> wrote:
    > >> RichardOnRails wrote:
    > >>> On Oct 21, 5:38 pm, Daniel Berger <> wrote:
    > >>>> Rodrigo Bermejo wrote:
    > >>>>> James Tucker wrote:
    > >>>>>> def get_user; ENV['USERNAME']; end
    > >>>>> The issue here is that it will return the username Im using to log into
    > >>>>> the box
    > >>>>> using net/telnet.
    > >>>>> Iam looking for the username of the guy seated in front of the computer.
    > >>>>> Something similar to the UNIX 'finger,who,w' commands
    > >>>>> I've already tried looking in the registry with out success.
    > >>>> require 'windows/system_info'
    > >>>> include Windows::SystemInfo
    > >>>> buffer = 0.chr * 256
    > >>>> nsize = [buffer.size].pack("L")
    > >>>> unless GetUserName.call(buffer, nsize)
    > >>>> raise 'Something went wrong'
    > >>>> end
    > >>>> length = nsize.unpack("L")[0]
    > >>>> username = buffer[0 ... length].chop
    > >>>> username
    > >>>> Regards,
    > >>>> Dan
    > >>> Hi Daniel,
    > >>> Your suggestion looks neat! However, I ran into trouble running it.
    > >>> I got the error message:
    > >>> "K:/_Utilities/Ruby_1.8.2-15/ruby/lib/ruby/site_ruby/1.8/rubygems/
    > >>> custom_require.rb:27:in `gem_original_require': No such file to load
    > >>> -- windows/system_info (LoadError)
    > >>> from K:/_Utilities/Ruby_1.8.2-15/ruby/lib/ruby/site_ruby/1.8/rubygems/
    > >>> custom_require.rb:27:in `require'
    > >>> from GetUsername.rb:4
    > >>>> Exit code: 1"
    > >>> My line #4 was the require statement.
    > >>> I tried "gem install windows", which yielded:
    > >>> Could not find windows (> 0) in the repository
    > >>> I tried "gem install windows/system_info", and got:
    > >>> Could not find windows/system_info (> 0) in the repository
    > >>> Would you please point out where I went wrong?
    > >> Actually, you can use the sys-admin gem, which abstracts that for you:

    >
    > >> require 'sys/admin'
    > >> include Sys

    >
    > >> p Admin.get_login

    >
    > >> Regards,

    >
    > >> Dan

    >
    > > Thanks, Dan. That's a nice project you composed. Works great. Many
    > > thanks for sharing it with the Ruby community ... and me in
    > > particular :)

    >
    > > If you don't mind another question, can you speculate on why the
    > > command (on my WinXP-Pro/SP2 system with Ruby 1.8.2-15):
    > > K:\_Utilities\Ruby_1.8.2-15>ri sys-admin
    > > yields:
    > > Nothing known about sys-admin

    >
    > > I tried a couple of variants to no avail:
    > > K:\_Utilities\Ruby_1.8.2-15>ri sys-admin-1.4.2-mswin32
    > > Bad argument: sys-admin-1.4.2-mswin32

    >
    > > K:\_Utilities\Ruby_1.8.2-15\ruby\lib\ruby\gems\1.8\gems>ri sys-
    > > admin-1.4.2-mswin32
    > > Bad argument: sys-admin-1.4.2-mswin32

    >
    > > K:\_Utilities\Ruby_1.8.2-15\ruby\lib\ruby\gems\1.8\gems>ri sys-admin
    > > Nothing known about sys-admin

    >
    > > I presume the ri should return the contents of:
    > > K:\_Utilities\Ruby_1.8.2-15\ruby\lib\ruby\gems\1.8\gems\sys-
    > > admin-1.4.2-mswin32\README
    > > which is loaded with your documentation.

    >
    > > I've never tried using ri heretofore. I tried it on some other
    > > packages just now and got the same "Nothing known" msg.

    >
    > I'm actually not that familiar with ri myself. But, you should be able
    > to call "ri Sys::Admin.get_login" and get a result.
    >
    > I wonder if I can set things up so that anyone who does 'ri sys-admin'
    > will get the README. Anyone know?
    >
    > Regards,
    >
    > Dan


    Hi Dan,

    Thanks for your additional reply. When the gods are against you, you
    can't catch a break, e.g.

    K:\_Downloads\Ruby\amrita-1.0.2>ri Sys::Admin.get_login
    Nothing known about Sys::Admin.get_login

    I'll post a generic question about later after I exhaust all my
    research sources, i.e. Google, PickAxe, The Ruby Way, The Ruby
    Cookbook.

    Again, thanks for educating me.

    Best wishes,
    Richard
     
    RichardOnRails, Oct 23, 2007
    #10
  11. Rodrigo Bermejo

    Thufir Guest

    Re: Windows - Get current logged user

    I, too, would like to know how to get the current user. For all
    platforms, I guess.



    -Thufir
     
    Thufir, Oct 23, 2007
    #11
  12. Re: Windows - Get current logged user

    Thufir wrote:
    > I, too, would like to know how to get the current user. For all
    > platforms, I guess.


    require 'sys/admin'
    include Sys

    p Admin.get_login :)

    Works everywhere. :)

    Regards,

    Dan
     
    Daniel Berger, Oct 23, 2007
    #12
  13. Rodrigo Bermejo

    bbiker Guest

    Re: Windows - Get current logged user

    On Oct 22, 10:55 pm, RichardOnRails
    <> wrote:
    > On Oct 22, 7:05 pm, Daniel Berger <> wrote:
    >
    >
    >
    >
    >
    > > RichardOnRails wrote:
    > > > On Oct 21, 5:38 pm, Daniel Berger <> wrote:
    > > >> Rodrigo Bermejo wrote:
    > > >>> James Tucker wrote:
    > > >>>> def get_user; ENV['USERNAME']; end
    > > >>> The issue here is that it will return the username Im using to log into
    > > >>> the box
    > > >>> using net/telnet.
    > > >>> Iam looking for the username of the guy seated in front of the computer.
    > > >>> Something similar to the UNIX 'finger,who,w' commands
    > > >>> I've already tried looking in the registry with out success.
    > > >> require 'windows/system_info'
    > > >> include Windows::SystemInfo

    >
    > > >> buffer = 0.chr * 256
    > > >> nsize = [buffer.size].pack("L")

    >
    > > >> unless GetUserName.call(buffer, nsize)
    > > >> raise 'Something went wrong'
    > > >> end

    >
    > > >> length = nsize.unpack("L")[0]
    > > >> username = buffer[0 ... length].chop
    > > >> username

    >
    > > >> Regards,

    >
    > > >> Dan

    >
    > > > Hi Daniel,

    >
    > > > Your suggestion looks neat! However, I ran into trouble running it.

    >
    > > > I got the error message:
    > > > "K:/_Utilities/Ruby_1.8.2-15/ruby/lib/ruby/site_ruby/1.8/rubygems/
    > > > custom_require.rb:27:in `gem_original_require': No such file to load
    > > > -- windows/system_info (LoadError)
    > > > from K:/_Utilities/Ruby_1.8.2-15/ruby/lib/ruby/site_ruby/1.8/rubygems/
    > > > custom_require.rb:27:in `require'
    > > > from GetUsername.rb:4
    > > >> Exit code: 1"

    >
    > > > My line #4 was the require statement.

    >
    > > > I tried "gem install windows", which yielded:
    > > > Could not find windows (> 0) in the repository

    >
    > > > I tried "gem install windows/system_info", and got:
    > > > Could not find windows/system_info (> 0) in the repository

    >
    > > > Would you please point out where I went wrong?

    >
    > > Actually, you can use the sys-admin gem, which abstracts that for you:

    >
    > > require 'sys/admin'
    > > include Sys

    >
    > > p Admin.get_login

    >
    > > Regards,

    >
    > > Dan

    >
    > On Oct 22, 7:05 pm, Daniel Berger <> wrote:
    >
    >
    >
    >
    >
    > > RichardOnRails wrote:
    > > > On Oct 21, 5:38 pm, Daniel Berger <> wrote:
    > > >> Rodrigo Bermejo wrote:
    > > >>> James Tucker wrote:
    > > >>>> def get_user; ENV['USERNAME']; end
    > > >>> The issue here is that it will return the username Im using to log into
    > > >>> the box
    > > >>> using net/telnet.
    > > >>> Iam looking for the username of the guy seated in front of the computer.
    > > >>> Something similar to the UNIX 'finger,who,w' commands
    > > >>> I've already tried looking in the registry with out success.
    > > >> require 'windows/system_info'
    > > >> include Windows::SystemInfo

    >
    > > >> buffer = 0.chr * 256
    > > >> nsize = [buffer.size].pack("L")

    >
    > > >> unless GetUserName.call(buffer, nsize)
    > > >> raise 'Something went wrong'
    > > >> end

    >
    > > >> length = nsize.unpack("L")[0]
    > > >> username = buffer[0 ... length].chop
    > > >> username

    >
    > > >> Regards,

    >
    > > >> Dan

    >
    > > > Hi Daniel,

    >
    > > > Your suggestion looks neat! However, I ran into trouble running it.

    >
    > > > I got the error message:
    > > > "K:/_Utilities/Ruby_1.8.2-15/ruby/lib/ruby/site_ruby/1.8/rubygems/
    > > > custom_require.rb:27:in `gem_original_require': No such file to load
    > > > -- windows/system_info (LoadError)
    > > > from K:/_Utilities/Ruby_1.8.2-15/ruby/lib/ruby/site_ruby/1.8/rubygems/
    > > > custom_require.rb:27:in `require'
    > > > from GetUsername.rb:4
    > > >> Exit code: 1"

    >
    > > > My line #4 was the require statement.

    >
    > > > I tried "gem install windows", which yielded:
    > > > Could not find windows (> 0) in the repository

    >
    > > > I tried "gem install windows/system_info", and got:
    > > > Could not find windows/system_info (> 0) in the repository

    >
    > > > Would you please point out where I went wrong?

    >
    > > Actually, you can use the sys-admin gem, which abstracts that for you:

    >
    > > require 'sys/admin'
    > > include Sys

    >
    > > p Admin.get_login

    >
    > > Regards,

    >
    > > Dan

    >
    > Thanks, Dan. That's a nice project you composed. Works great. Many
    > thanks for sharing it with the Ruby community ... and me in
    > particular :)
    >
    > If you don't mind another question, can you speculate on why the
    > command (on my WinXP-Pro/SP2 system with Ruby 1.8.2-15):
    > K:\_Utilities\Ruby_1.8.2-15>ri sys-admin
    > yields:
    > Nothing known about sys-admin
    >
    > I tried a couple of variants to no avail:
    > K:\_Utilities\Ruby_1.8.2-15>ri sys-admin-1.4.2-mswin32
    > Bad argument: sys-admin-1.4.2-mswin32
    >
    > K:\_Utilities\Ruby_1.8.2-15\ruby\lib\ruby\gems\1.8\gems>ri sys-
    > admin-1.4.2-mswin32
    > Bad argument: sys-admin-1.4.2-mswin32
    >
    > K:\_Utilities\Ruby_1.8.2-15\ruby\lib\ruby\gems\1.8\gems>ri sys-admin
    > Nothing known about sys-admin
    >
    > I presume the ri should return the contents of:
    > K:\_Utilities\Ruby_1.8.2-15\ruby\lib\ruby\gems\1.8\gems\sys-
    > admin-1.4.2-mswin32\README
    > which is loaded with your documentation.
    >
    > I've never tried using ri heretofore. I tried it on some other
    > packages just now and got the same "Nothing known" msg.
    >
    > Any ideas for this Ruby newbie (sort of)?
    >
    > Best wishes,
    > Richard- Hide quoted text -
    >
    > - Show quoted text -- Hide quoted text -
    >
    > - Show quoted text -


    I am using fastri -- a souped up version of ri

    qri Sys::Admin =>

    C:\Documents and Settings\Owner>qri Sys::Admin
    ------------------------------------------------------ Class:
    Sys::Admin
    (no description...)
    ------------------------------------------------------------------------
    Constants:
    ----------
    VERSION: '1.4.2'
    SidTypeUser: 1
    SidTypeGroup: 2
    SidTypeDomain: 3
    SidTypeAlias: 4
    SidTypeWellKnownGroup: 5
    SidTypeDeletedAccount: 6
    SidTypeInvalid: 7
    SidTypeUnknown: 8
    SidTypeComputer: 9
    Class methods:
    --------------
    add_global_group, add_global_user, add_local_group,
    add_local_user,
    config_global_group, config_global_user, config_local_group,
    config_local_user, delete_global_group, delete_global_user,
    delete_local_group, delete_local_user, get_group, get_login,
    get_user, groups, users
     
    bbiker, Oct 23, 2007
    #13
  14. Devi Web Development, Oct 24, 2007
    #14
    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. John Dalberg
    Replies:
    6
    Views:
    21,136
    John Dalberg
    Apr 18, 2005
  2. keithb
    Replies:
    0
    Views:
    650
    keithb
    Feb 16, 2006
  3. Imran Aziz

    Get the current logged in user name

    Imran Aziz, Aug 2, 2005, in forum: ASP .Net Security
    Replies:
    8
    Views:
    227
  4. Shaminda Illangantilaka

    How can I get the current logged user information

    Shaminda Illangantilaka, Jan 4, 2006, in forum: ASP .Net Security
    Replies:
    2
    Views:
    160
    Patrick.O.Ige
    Jan 25, 2006
  5. mvr
    Replies:
    5
    Views:
    574
    Aaron Bertrand [SQL Server MVP]
    May 16, 2006
Loading...

Share This Page