Bharathi Nandhan
2018-04-23 08:29:31 UTC
Hello,
Am trying to write a program using crypt library, which can connect to
ssh server and execute commands remotely.
I was successful in creating ssh session and writing data to the
session. But, i couldnt able to read data from the session.
Below is the code which i used for creating, writing and reading
output back from ssh session.
Please let me know your thoughts and correct me.
cryptInit();
CRYPT_SESSION cryptSession;
int bytesCopied, status;
/* Create a client session */
status = cryptCreateSession( &cryptSession, CRYPT_UNUSED /* CRYPT_UNUSED */,
CRYPT_SESSION_SSH );
if( cryptStatusError( status ) )
{
qDebug() << "Couldnt able to create session";
}
/* Set servername and password to the session */
const char c_username[30] = "ssh://***@xx.xx.x.x";
const char c_password[10] = "xx";
status = cryptSetAttributeString( cryptSession, CRYPT_SESSINFO_SERVER_NAME,
c_username, strlen(c_username) );
if( cryptStatusError( status ) )
{
cryptDestroySession( cryptSession );
qDebug() << "Couldnt able to input servername to the session";
}
status = cryptSetAttributeString( cryptSession, CRYPT_SESSINFO_PASSWORD,
c_password, strlen(c_password) );
if( cryptStatusError( status ) )
{
cryptDestroySession( cryptSession );
qDebug() << "Couldnt able to input password to the session";
}
else
{
qDebug() << "Success";
}
/* Activate the session */
status = cryptSetAttribute( cryptSession, CRYPT_SESSINFO_ACTIVE, TRUE );
if( cryptStatusError( status ) )
{
cryptDestroySession( cryptSession );
if ( status = CRYPT_ERROR_WRONGKEY)
qDebug() << "Couldnt able to activate session" << status;
}
else
{
qDebug() << "Session activated";
}
/* Send a command to the remote system */
char command[30] = "ls -l\r\n";
status = cryptPushData( cryptSession, command, strlen( command ),
&bytesCopied );
if( cryptStatusOK( status ) )
{
if( bytesCopied != ( int ) strlen( command ) )
status = -1; /* Not all data was processed */
else
{
status = cryptFlushData( cryptSession );
qDebug() << "Flushed Data"<< status;
}
}
/* Read back the response */
int *outDataLength = 0, outDataMaxLength = 500;
char outData[499];
status = cryptPopData( cryptSession, outData, outDataMaxLength,
outDataLength );
if( cryptStatusError( status ) )
{
cryptDestroySession( cryptSession );
qDebug() << "Error Reading data" << status;
}
//cryptDestroySession( cryptSession );
qDebug() << "Return Value:"<<outData;
qDebug() << "Bytes Copied:"<<*outDataLength;
cryptDestroySession( cryptSession );
cryptEnd();
Regards,
Bharathinandhan R
Am trying to write a program using crypt library, which can connect to
ssh server and execute commands remotely.
I was successful in creating ssh session and writing data to the
session. But, i couldnt able to read data from the session.
Below is the code which i used for creating, writing and reading
output back from ssh session.
Please let me know your thoughts and correct me.
cryptInit();
CRYPT_SESSION cryptSession;
int bytesCopied, status;
/* Create a client session */
status = cryptCreateSession( &cryptSession, CRYPT_UNUSED /* CRYPT_UNUSED */,
CRYPT_SESSION_SSH );
if( cryptStatusError( status ) )
{
qDebug() << "Couldnt able to create session";
}
/* Set servername and password to the session */
const char c_username[30] = "ssh://***@xx.xx.x.x";
const char c_password[10] = "xx";
status = cryptSetAttributeString( cryptSession, CRYPT_SESSINFO_SERVER_NAME,
c_username, strlen(c_username) );
if( cryptStatusError( status ) )
{
cryptDestroySession( cryptSession );
qDebug() << "Couldnt able to input servername to the session";
}
status = cryptSetAttributeString( cryptSession, CRYPT_SESSINFO_PASSWORD,
c_password, strlen(c_password) );
if( cryptStatusError( status ) )
{
cryptDestroySession( cryptSession );
qDebug() << "Couldnt able to input password to the session";
}
else
{
qDebug() << "Success";
}
/* Activate the session */
status = cryptSetAttribute( cryptSession, CRYPT_SESSINFO_ACTIVE, TRUE );
if( cryptStatusError( status ) )
{
cryptDestroySession( cryptSession );
if ( status = CRYPT_ERROR_WRONGKEY)
qDebug() << "Couldnt able to activate session" << status;
}
else
{
qDebug() << "Session activated";
}
/* Send a command to the remote system */
char command[30] = "ls -l\r\n";
status = cryptPushData( cryptSession, command, strlen( command ),
&bytesCopied );
if( cryptStatusOK( status ) )
{
if( bytesCopied != ( int ) strlen( command ) )
status = -1; /* Not all data was processed */
else
{
status = cryptFlushData( cryptSession );
qDebug() << "Flushed Data"<< status;
}
}
/* Read back the response */
int *outDataLength = 0, outDataMaxLength = 500;
char outData[499];
status = cryptPopData( cryptSession, outData, outDataMaxLength,
outDataLength );
if( cryptStatusError( status ) )
{
cryptDestroySession( cryptSession );
qDebug() << "Error Reading data" << status;
}
//cryptDestroySession( cryptSession );
qDebug() << "Return Value:"<<outData;
qDebug() << "Bytes Copied:"<<*outDataLength;
cryptDestroySession( cryptSession );
cryptEnd();
Regards,
Bharathinandhan R