简体   繁体   中英

Get Windows username from Bash Unix shell?

Is there a way to get the Windows' username of the user who's connecting to a specific Unix environment, using always the same server's credentials, directly in Bash?

Some background

There is a population of N users, who may connect to M different Unix environments always using the same credentials on Unix: someusername , no password. Basically,

  • The Windows user "Matteo" has a Windows session open on the machine " PMatteo.fr.company.com ".
  • This user opens an SSH session on the Linux machine linuxMachine , using Putty.
  • The login is always done with the same "fake" credentials: someusername , no password, and the user is in.

Inside the Unix environment, hence using a bash command, I would like to retrieve the Windows username "Matteo".

My researches so far

The following command:

echo $(who am i) | cut -d "(" -f2 | cut -d ")" -f1

is showing me two kinds of outputs, depending on the machine to which I'm connected:

  • Sometimes, it shows directly the machine name PMatteo.fr.company.com
  • Some other times, it shows just the IP address (eg 45.32.4.19 ), but that's fine because I get to find the machine name using the command nslookup 45.32.4.19 .

My question

Is there any way, using a Bash script, to go directly up to the Windows username? I can (pretty) easily find the machine name, so I believe that the Windows username may be stored somewhere, I just don't know how and I can't find it anywhere in the web. Does anyone have any hint? My bash version is 4.1.2(1).

Purpose

From the windows username, I can always rebuild the mail address of the user. Sometimes the user launches a command that may affect other users, but since they connect (Unix-speaking) always with the same username, nobody knows who is actually performing the command. It would be nice to be able to reach them by email in case someone else wants to ask to stop what they're doing.

The simple answer is no.

You do not transfer this information to your Unix/Linux server, because you always login with the credentials of someuser .

Because you can derive the username from the hostname of your PC, you may be able to derive the windows username, but this has a number of flaws:

  • someone else who (ab)uses a PC gets mapped to the owner of that PC, even if they login with their own account.
  • if someone connects a rogue PC to the network, the username may not be derived from its hostname.'

What you might try is access the windows client from the Linux box. But that requires that all the clients are accessible from Linux (rdp, telnet, powers hell, ssh server or something like that, and of course authorization on Windows). Then you might look at who is logged-in at that time (and hope it is just one user).

In Putty, go to Connection->SSH->Remote command and add this,

env WINUSER=yourname bash

then connect. That should set the environment variable WINUSER in the remote shell environment.

This assumes a trusted environment, since anyone could fake the setting when they log in.

(This might get bumped over to https://superuser.com )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM