Check if user logged into remote machine via C++ / Java

Hi,

I have a program running on HP-UX and it must checkwhether a user has already logged-in to another machine. The hostname of the other machine is known when the check has to be made.

Is there a way which this can be accomplished using C++ or Java? If not I could parse the output of a shell command, given one which does the above.

Regards,

John

Are you asking if the account running your code has another process that made a remote connection? And the connection is still active? ssh, telnet, what type of connection?

The language you code in for things like this is usually not the problem.

Hi jim,

thanks for the reply. The other account is logged in and running a different process on a different machine.

So:

Machine A: running my program as user admin
Machine B: running some other process as user johnmcpa

Machine B authenticated johnmcpa before they started running their process.

I found a potential solution to my problem by using a shell command;

ssh hostname 'ps -xu username | grep -i processname'

and I even got this working in a C++ program

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

using namespace std;

int main(int argc, char** argv)
{
    string cmd = "ssh t9wst2 'ps -xu admin | grep -i applicationbar'";
    cout << "Executing" << endl;
    int result = system(cmd.c_str());
    cout << "Done: " << result << endl;
    if (0 != result)
    {
        cout << "It is not running" << endl;
    }
    else
    {
        cout << "It is running" << endl;
    }
    return 0;
}

But alas the user running my program doesn't have permission to use ssh. Any other ideas?

can the user telnet over?

experiment with this script:

#!/bin/ksh
# tst.sh
telnet t9wst2 <<EOF | grep -i applicationbar 
username
password
ps -xu admin | grep -i applicationbar
exit
EOF
exit

This will filter out everything but a successful find

Thanks for the reply. Unfortunately however anything that requires interaction won't work inside the process running on the local machine.

The only other solution I have is for the remote process to write a file into a directory mounted from the local machine on the remote one. The local process can then check for its existence and remove it afterwards. This isn't ideal since theoretically anyone with write access to that directory can create this file but it might have to do.

The here document will work non-interactively with telnet. It's not like ssh.