Subject: libssh2 running a command as root

libssh2 running a command as root

From: Phillip Wu <>
Date: Wed, 16 Mar 2011 10:36:06 +1100


I am using Visual Studio C++ 2010 and libssh2 1.2.7.

I want to run a command as root on the target UNIX machine. However the UNIX machine
does not allow direct login to root even with the correct password. If the command
was being run interactively:
a. Logon to server as a non-root user
b. su - root

1. Is there any libssh2 call that can help in this situation?
2. If the answer to question 1. is NO would I do the following:
   authenticate non-root user using libssh2_userauth_publickey_fromfile() or libssh2_userauth_password()
   create a pty with libssh2_channel_request_pty()
   create a shell on the remote server with libssh2_channel_shell()
   run "su - root" with libssh2_channel_write()
       reading password prompt with libssh2_channel_read()
       sending password with libssh2_channel_write()
   run the command with libssh2_channel_write()
   get the run status with libssh2_channel_write("echo $?") assuming I am using Bourne shell
       read the status with libssh2_channel_read()
3. Is there a better way than that shown in 2. ?

Thanks in advance for any help with this issue.

This message is intended for the addressee named and may contain confidential information. If you are not the intended recipient, please delete it and notify the sender. Views expressed in this message are those of the individual sender, and are not necessarily the views of the Land and Property Management Authority. This email message has been swept by MIMEsweeper for the presence of computer viruses.
Please consider the environment before printing this email.

Received on 2011-03-16