wangysh 发表于 2013-1-28 18:43:56

ne7ssh使用(ssh client)

1、准备工作
wget http://botan.randombit.net/files/Botan-1.8.8.tgz
wget http://www.cmake.org/files/v2.8/cmake-2.8.0.tar.gz
wget http://www.netsieben.com/files/ne7ssh-1.3.2.tar.gz
a、Botan安装:python configure.py; 
                      make install
                      cp libbotan.so /usr/lib 或cp libbotan.so /usr/lib64这个要看你os位数的
                       export LD_LIBRARY_PATH=/usr/lib
 
b、cmake安装:./bootstrap; make; make install
 
c、ne7ssh安装:cmake .
                         make all
                         make install
 
2、写个入门类吧
 
 
/*
*Version 0.1 by jack.wang
*
* g++ ne7ssh_test.cpp  -lnet7ssh -lbotan -o ne7sshtest
* ./ne7sshtest
**/
 
#include <ne7ssh.h>
int main(int argc,char *argv[])
{
        ne7ssh *_ssh = new ne7ssh();
        int channel1;     
        // Set SSH connection options.
        _ssh->setOptions ("aes192-cbc", "hmac-md5");
        channel1 = _ssh->connectWithPassword ("10.249.148.110", 22, "root", "hell05a");
        if (channel1 < 0)
        {
           const char *errmsg = _ssh->errors()->pop();
           if (errmsg != NULL)    
                  printf ("Connection failed with error: %s\n\n", errmsg);
           exit(-1);
        }
 
        char cmdstr={0}; 
        strcat( cmdstr, "ls -ln \n" ); 
 
        if (!_ssh->send (cmdstr, channel1))
        {
             const char *errmsg = _ssh->errors()->pop(channel1);
             if (errmsg != NULL)
                 printf ("Could not send the command . error: %s.\n\n", errmsg);
              exit(-1);
        }
        // Wait for bash prompt, or die in 5 seconds
        if (!_ssh->waitFor (channel1, "0", 5))
        {
              const char *errmsg = _ssh->errors()->pop(channel1);
              if (errmsg == NULL) 
                    printf ("Timeout while waiting for remote site.error: %s.\n\n", errmsg);
              exit(-1);
        }
        const char *result;            
        result = _ssh->read (channel1);
        printf("%s\n", result);
        exit(0);
}
 
页: [1]
查看完整版本: ne7ssh使用(ssh client)