libSSH

Exported from Notepad++
libSSH wget https://www.libssh.org/files/0.9/libssh-0.9.0.tar.xz tar -xf libssh-0.9.0.tar.xz cd libssh-0.9.0/ mkdir build cd build bei Raspbian: apt-get install cmake cmake /root/temp/libssh/libssh-0.9.0 -DCMAKE_INSTALL_PREFIX=/usr make install ################################################ BSP. //clear; rm -frv heizkkoerpersteuerung_gpio; g++ -g -fPIC heizkkoerpersteuerung_gpio.cpp -o heizkkoerpersteuerung_gpio -lwiringPi -lssh -static-libstdc++ //Info http://wiringpi.com/reference/core-functions/ //C_C++ Header //#include <dirent.h> //#include <sys/stat.h> //#include <sys/types.h> //#include unistd.h für sleep #include <unistd.h> #include <stdio.h> #include <string> #include <stdlib.h> //#include <iomanip> #include <iostream> //#include <vector> //#include <sys/time.h> //#include <sstream> #include <fstream> //#include <pthread.h> //#include <thread> //#include <time.h> #include <errno.h> #include <signal.h> //#include <malloc.h> //#include <errno.h> //#include <sys/mman.h> //#include <csignal> //#include <iterator> //#include <pthread.h> //Header für setrlimit (ulimits setzen) //#include <sys/resource.h> #include <cstring> //strlen #include <algorithm> //string erase //GPIO wiring PI Header #include <wiringPi.h> #include <libssh/libssh.h> //SSH using namespace std; void signalHandler( int signum) { cout << “Interrupt signal (” << signum << “) received.\n”; cout<<“PROBLEM Heater Control Daemon”<<endl; //exit (666); exit (- 1); } int main(int argc, char *argv[]) { //#################################################################### //Error Signals signal(SIGSEGV, signalHandler); signal(SIGABRT, signalHandler); signal(SIGFPE, signalHandler); signal(SIGILL, signalHandler); signal(SIGTERM, signalHandler); signal(SIGINT, signalHandler); //#################################################################### string temperatur_soll; temperatur_soll = argv[1]; cout<<“Eingabe Temeperatur SOLL: “<<temperatur_soll<<endl; //string temperatur_ist; //temperatur_ist = argv[2]; //cout<<“Eingabe Temeperatur IST: “<<temperatur_ist<<endl; //libssh inistialiseren#### ssh_session my_ssh_session = ssh_new(); if (my_ssh_session == NULL) { cout<<“Problem my_ssh_session#”<<my_ssh_session<<endl; exit(-1); } //cout<<“my_ssh_session#”<<my_ssh_session<<endl; //libssh Optionen## //int verbosity = SSH_LOG_PROTOCOL; int verbosity = SSH_LOG_NOLOG; int port = 22; ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, “localhost”); ssh_options_set(my_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity); ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port); //libssh connect zu server### int rc; rc = ssh_connect(my_ssh_session); if (rc != SSH_OK) { //fprintf(stderr, “Error connecting to localhost: %s\n”,ssh_get_error(my_ssh_session)); exit(-1); } //cout<<“ssh_connect rc#”<<rc<<endl; //libssh authentification#### enum ssh_known_hosts_e state; unsigned char *hash = NULL; ssh_key srv_pubkey = NULL; size_t hlen; char buf[10]; char *hexa; char *p; int cmp; rc = ssh_get_server_publickey(my_ssh_session, &srv_pubkey); if (rc < 0) { return -1; } //cout<<“ssh_get_server_publickey rc#”<<rc<<endl; rc = ssh_get_publickey_hash(srv_pubkey,SSH_PUBLICKEY_HASH_SHA1,&hash,&hlen); ssh_key_free(srv_pubkey); if (rc < 0) { return -1; } //cout<<“ssh_get_publickey_hash rc#”<<rc<<endl; state = ssh_session_is_known_server(my_ssh_session); //cout<<“state#”<<state<<endl; /* char *password; password = getpass(“Password: “); rc = ssh_userauth_password(my_ssh_session, NULL, password); if (rc != SSH_AUTH_SUCCESS) { fprintf(stderr, “Error authenticating with password: %s\n”, ssh_get_error(my_ssh_session)); ssh_disconnect(my_ssh_session); ssh_free(my_ssh_session); exit(-1); } cout<<“ssh_userauth_password rc#”<<rc<<endl; */ //Public Keys auth## rc = ssh_userauth_publickey_auto(my_ssh_session, NULL, NULL); if (rc == SSH_AUTH_ERROR) { //fprintf(stderr, “Authentication failed: %s\n”,ssh_get_error(my_ssh_session)); return SSH_AUTH_ERROR; } //cout<<“ssh_userauth_publickey_auto rc#”<<rc<<endl; //Kommando ausführen## ssh_channel channel; channel = ssh_channel_new(my_ssh_session); rc = ssh_channel_open_session(channel); //cout<<“ssh_channel_open_session rc#”<<rc<<endl; rc = ssh_channel_request_exec(channel, “/home/pi/scripte/temperaturmessung”); //cout<<“ssh_channel_request_exec rc#”<<rc<<endl; if (rc != SSH_OK) { ssh_channel_close(channel); ssh_channel_free(channel); return rc; } //cout<<“ssh_channel_request_exec rc#”<<rc<<endl; char buffer[256]; int nbytes; nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0); while (nbytes > 0) { //if (write(1, buffer, nbytes) != (unsigned int) nbytes) //{ // ssh_channel_close(channel); // ssh_channel_free(channel); // return SSH_ERROR; //} nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0); cout<<“buffer#”<<buffer<<“#”<<endl; } if (nbytes < 0) { ssh_channel_close(channel); ssh_channel_free(channel); return SSH_ERROR; } ssh_channel_send_eof(channel); ssh_channel_close(channel); ssh_channel_free(channel); ssh_disconnect(my_ssh_session); ssh_free(my_ssh_session); } ################################################

Post Revisions:

This post has not been revised since publication.

Tags:  , ,