Login/Logout Script

Discussion in 'Mac Programming' started by xfallen86, Oct 5, 2016.

  1. xfallen86 macrumors newbie

    Joined:
    Oct 5, 2016
    #1
    Hi everybody,
    First, thanks for the job done on that forum !!

    I'm an IT specialist and I manage a hundred of iMacs with 10.12 installed. I have to log when user login and logout with the Mac address and IP address.

    Have you an idea if I can do a script which is saved automatically on a shared point with each information when user login and logout of a computer ?

    Have a good day.
     
  2. DennisBlah macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #2
    Depends on what kind of sollution you want.
    You might consider to write a small application 'background' application with xCode.
    Register it to launch in LaunchDaemons

    On applicationDidFinishLaunching you can log NSUsername() as being 'logged in' on IP: [[[NSHost currentHost] addresses] lastObject]
    afterwards call [self LogoutNotifications];
    The function might look something like this
    Code:
    -(void)LogoutNotifications {
       NSNotificationCenter *notCenter;
        notCenter = [[NSWorkspace sharedWorkspace] notificationCenter];
        [notCenter addObserver:self
                      selector:@selector(loggingOut)
                          name:NSWorkspaceWillPowerOffNotification object:nil];
    [[NSRunLoop currentRunLoop] run];
    }
    
    This observer traps the Restart, logout and shutdown, and calls function loggingOut
    Then create a function 'loggingOut' and log the NSUsername to logout again with the IP.
     
  3. xfallen86 thread starter macrumors newbie

    Joined:
    Oct 5, 2016
    #3
    Hi,
    thanks for you answer.

    I worded on the script yesterday morning and I have that :

    Code:
    #!/bin/bash
    # logout script
    username=${1}
    date=`date "+%m-%d-%Y"`
    time=`date "+%H:%M:%S"`
    computer=`hostname`
    ip=$(ip addr show en0 | awk '/inet / {print $2}' | cut -d/ -f 1)
    mac=$(ip link show en0 | awk '/ether/ {print $2}')
    logfile="/var/log/usertracking.log"
    echo $date" "$time," "$username," "$computer," "$ip," "$mac," logout" >> "$logfile"
    But ..... in the log there is no IP and no MAC address ... I don't know why :

    That the first thing.

    After I will have to export that file to a Sharepoint
     
  4. DennisBlah macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #4
    On the mac you don't have a command ip addr

    Try the following:
    Code:
    ....
    ip=$(ifconfig en0 | awk '/inet /{split($2,a," "); print a[1]}')
    mac=$(ifconfig en0 | awk '/ether/{split($2,a," "); print a[1]}')
    ....
    
    Please note the interface, en0 is the interface of my WiFi.
    This might be eth0 ?
     

Share This Page