Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
omeredemen committed Dec 12, 2022
0 parents commit 89d1474
Show file tree
Hide file tree
Showing 7 changed files with 336 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"configurations": [
{
"name": "linux-gcc-x64",
"includePath": [
"${workspaceFolder}/**"
],
"compilerPath": "/usr/bin/gcc",
"cStandard": "${default}",
"cppStandard": "${default}",
"intelliSenseMode": "linux-gcc-x64",
"compilerArgs": [
""
]
}
],
"version": 4
}
24 changes: 24 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++ Runner: Debug Session",
"type": "cppdbg",
"request": "launch",
"args": [],
"stopAtEntry": false,
"externalConsole": false,
"cwd": "/home/omer/Desktop/ssh_com",
"program": "/home/omer/Desktop/ssh_com/build/Debug/outDebug",
"MIMode": "gdb",
"miDebuggerPath": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
36 changes: 36 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"C_Cpp_Runner.cCompilerPath": "gcc",
"C_Cpp_Runner.cppCompilerPath": "g++",
"C_Cpp_Runner.debuggerPath": "gdb",
"C_Cpp_Runner.cStandard": "",
"C_Cpp_Runner.cppStandard": "",
"C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat",
"C_Cpp_Runner.useMsvc": false,
"C_Cpp_Runner.warnings": [
"-Wall",
"-Wextra",
"-Wpedantic",
"-Wshadow",
"-Wformat=2",
"-Wconversion",
"-Wnull-dereference",
"-Wsign-conversion"
],
"C_Cpp_Runner.enableWarnings": true,
"C_Cpp_Runner.warningsAsError": false,
"C_Cpp_Runner.compilerArgs": [],
"C_Cpp_Runner.linkerArgs": [],
"C_Cpp_Runner.includePaths": [],
"C_Cpp_Runner.includeSearch": [
"*",
"**/*"
],
"C_Cpp_Runner.excludeSearch": [
"**/build",
"**/build/**",
"**/.*",
"**/.*/**",
"**/.vscode",
"**/.vscode/**"
]
}
30 changes: 30 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <stdio.h>
#include <stdlib.h>


#include "ssh_com.h"

int main(){

ssh_session session;

int port = 22;
// char password[] = "2004";
char command[] = "iwconfig wlp1s0 | grep Signal | \
/usr/bin/awk '{print $4}' | /usr/bin/cut -d'=' -f2";

// char host[] = "192.168.20.246";
// char user[] = "ems";

FILE *fp;
char value;

initSession(&session, port, "ems", "192.168.20.246", "2004");

while(1){

int rssi_value = -getCommandOutputFromChannel(session, command); //this function gives rssi value as positive

}
return 0;
}
109 changes: 109 additions & 0 deletions ssh_com.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#include "ssh_com.h"
#include <stdio.h>
#include <stdlib.h>
#include <libssh/libssh.h>

void initSession(ssh_session* session, int port, const char* user,
const char* host, const char* password){
int rc;
printf("new session\n");
*session = ssh_new();
if(*session == NULL) exit(-1);

ssh_options_set(*session, SSH_OPTIONS_HOST, host);
ssh_options_set(*session, SSH_OPTIONS_PORT, &port);
ssh_options_set(*session, SSH_OPTIONS_USER, user);

printf("connecting to session..\n");
rc = ssh_connect(*session);
if(rc != SSH_OK) errorSession(*session);

printf("Password Autentication...\n");
rc = ssh_userauth_password(*session, NULL, password);
if(rc != SSH_AUTH_SUCCESS) {
fprintf(stderr, "Error authenticating with password: %s\n",
ssh_get_error(*session));
freeSession(*session);
exit(-1);
}
}

void sendCommandToChannel(ssh_session session, ssh_channel *channel, const char* command){
int rc;
printf("\nChannel...\n");
*channel = ssh_channel_new(session);
if (*channel == NULL) exit(-1);

printf("Opening...\n");
rc = ssh_channel_open_session(*channel);
if(rc != SSH_OK) errorSession(session);

printf("Executing remote command...\n");
rc = ssh_channel_request_exec(*channel, command);
if(rc != SSH_OK) errorSession(session);
}

int getCommandOutputFromChannel(ssh_session session, const char* command){
ssh_channel channel;
char buffer[1024];
unsigned int nbytes;

FILE *fp;
char c;
int16_t value;
int output[4];

sendCommandToChannel(session, &channel, command);

printf("Received:\n");
nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
while(nbytes > 0) {
fp = fopen("ssh_command_log.txt", "wb");
if(fp == NULL) {
printf("Error opening file\n");
return -1;
}
if(fwrite(buffer, sizeof(char), nbytes, fp) != nbytes){
freeChannel(channel);
return SSH_ERROR;
}
fclose(fp);

fp = fopen("ssh_command_log.txt", "rb");
if (fp == NULL) {
printf("Error opening file\n");
return -1;
}
int i = 0;
while(1) {
c = fgetc(fp);
if(feof(fp))break;
output[i] = c - 48; //48 is decimal value of char '0' in ASCII
printf("%d \t", c - '0'); //value - '0' is exactly the line above
i++;
}
fclose(fp);
nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
}
value = ((output[1])*10) + output[2];

freeChannel(channel);
return value;
}

void errorSession(ssh_session session){
fprintf(stderr, "Error: %s\n", ssh_get_error(session));
freeSession(session);
exit(-1);
}

void freeSession(ssh_session session){
ssh_disconnect(session);
ssh_free(session);
}

void freeChannel(ssh_channel channel){
ssh_channel_send_eof(channel);
ssh_channel_close(channel);
ssh_channel_free(channel);
}
17 changes: 17 additions & 0 deletions ssh_com.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef _SSH_COM_
#define _SSH_COM_


#include <libssh/libssh.h>

void initSession(ssh_session *session, int port, const char* user,
const char* host, const char* password);
void sendCommandToChannel(ssh_session session, ssh_channel *channel, const char* command);
int getCommandOutputFromChannel(ssh_session session, const char* command);

void errorSession(ssh_session session);
void freeSession(ssh_session session);
void freeChannel(ssh_channel channel);


#endif
102 changes: 102 additions & 0 deletions ssh_loop.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include <libssh/libssh.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void free_channel(ssh_channel channel) {
ssh_channel_send_eof(channel);
ssh_channel_close(channel);
ssh_channel_free(channel);
}

void free_session(ssh_session session) {
ssh_disconnect(session);
ssh_free(session);
}

void error(ssh_session session) {
fprintf(stderr, "Error: %s\n", ssh_get_error(session));
free_session(session);
exit(-1);
}

int main() {

ssh_session session;
ssh_channel channel;
int rc, port = 22;
char buffer[1024];
unsigned int nbytes;

char host[] = "192.168.20.246";
char user[] = "ems";

FILE *fp;
int c;

printf("Session...\n");
session = ssh_new();
if (session == NULL) exit(-1);

ssh_options_set(session, SSH_OPTIONS_HOST, host);
ssh_options_set(session, SSH_OPTIONS_PORT, &port);
ssh_options_set(session, SSH_OPTIONS_USER, user);

printf("Connecting...\n");
rc = ssh_connect(session);
if (rc != SSH_OK) error(session);

printf("Password Autentication...\n");
rc = ssh_userauth_password(session, NULL, "2004");
if (rc != SSH_AUTH_SUCCESS) error(session);

while (1)
{
printf("Channel...\n");
channel = ssh_channel_new(session);
if (channel == NULL) exit(-1);

printf("Opening...\n");
rc = ssh_channel_open_session(channel);
if (rc != SSH_OK) error(session);

printf("Executing remote command...\n");
rc = ssh_channel_request_exec(channel, "iwconfig wlp1s0 | grep Signal | /usr/bin/awk '{print $4}' | /usr/bin/cut -d'=' -f2");
if (rc != SSH_OK) error(session);

printf("Received:\n");
nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
while (nbytes > 0) {
fp = fopen("rssi.txt", "wb");
if(fp == NULL) {
printf("Error opening file\n");
exit(1);
}
if (fwrite(buffer, sizeof(char), nbytes, fp) != nbytes) {
free_channel(channel);
return SSH_ERROR;
}

fclose(fp);
fp = fopen("rssi.txt","rb");
while(1) {
c = fgetc(fp);
if( feof(fp) ) {
break ;
}
printf("%c", c);
}
fclose(fp);
nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
}

free_channel(channel);
usleep(1000000);

}

free_channel(channel);
free_session(session);

return 0;
}

0 comments on commit 89d1474

Please sign in to comment.