zoukankan      html  css  js  c++  java
  • nodejs ssh2

    https://www.npmjs.com/package/ssh2

    npm install ssh2

     ssh2文件下载:

     1 //前台命令下发
     2 app.get('/test/fileDownload', function (req, res) {
     3     var downloadPath = req.query.downloadPath;
     4     console.log(downloadPath);
     5 
     6     var conn = new Client();
     7     conn.on('ready', function() {
     8         console.log('Client :: ready');
     9         conn.sftp(function(err, sftp) {
    10             if (err) throw err;
    11             //sftp.fastPut('/home/admin/foo/expdat.log','/home/book/foo/expdat.log', function(err, list) {
    12             sftp.fastGet('/home/book/foo/expdat.log','/home/admin/foo/expdat.log' ,function(err, result) {
    13                 if (err) throw err;
    14                 //console.log('----------GET  BEGIN!-------------');
    15                 res.download(downloadPath);
    16                 //console.log('----------GET  END!-------------');
    17                 conn.end();
    18             });
    19         });
    20     }).connect({
    21         host: '192.168.0.111',
    22         port: 22,
    23         username: 'admin',
    24         password: 'admin'
    25     });
    26 });
    app.get('/test/fileDownload', function (req, res)

    调用:<a href="fileDownload?downloadPath=/home/admin/foo/expdat.log" ><input type='button' value='文件下载' ></a>

     ssh2shell命令:

     1 var Client = require('ssh2').Client;
     2 
     3 var conn = new Client();
     4 conn.on('ready', function() {
     5     conn.exec('/home/admin/foo/me.sh', function(err, stream) {
     6         if (err) throw err;
     7 
     8         stream.on('close', function(code, signal) {
     9             conn.end();
    10         }).on('data', function(data) {
    11             console.log('STDOUT: ' + data);
    12         }).stderr.on('data', function(data) {
    13             console.log('STDERR: ' + data);
    14         });
    15     });
    16 }).connect({
    17     host: '10.20.16.73',
    18     port: 22,
    19     username: 'prodis',
    20     password: 'pro'
    21     //privateKey: require('fs').readFileSync('/home/admin/.ssh/id_dsa')
    22 });
    SHELL_CMD

    备注:

    http://blog.sina.com.cn/s/blog_680374c30101iznc.html

     1 在A上执行keygen,转换后cp到B上,那么在B上ssh A时不需要口令
     2 §6.4 使用Public Key (OpenSSH) 不用密码登陆.
     3 步骤如下:
     4 l         创建key
     5 1.           $ mkdir -p ~/.ssh                        If it doesn't already exist
     6 2.           $ chmod 700 ~/.ssh
     7 3.           $ cd ~/.ssh
     8 $ ssh-keygen -t dsa
     9 l         拷贝key到服务器端
    10 $ scp -p id_dsa.pub remoteuser@remotehost:
    11 Password: ********
    12 l         登陆到服务器端安装公钥
    13 $ ssh -l remoteuser remotehost
    14 Password: ********
    15  
    16 remotehost$ mkdir -p ~/.ssh                        If it doesn't already exist
    17 remotehost$ chmod 700 ~/.ssh
    18 remotehost$ cat id_dsa.pub >> ~/.ssh/authorized_keys   (Appending)
    19 remotehost$ chmod 600 ~/.ssh/authorized_keys
    20 remotehost$ mv id_dsa.pub ~/.ssh        可选步骤,该文件甚至可以删除掉
    21 remotehost$ logout
    22 l         用public-key登陆
    23 $ ssh -l remoteuser remotehost
    24 Enter passphrase for key '/home/smith/.ssh/id_dsa': ********
    25  
    26 公钥一般存放在~/.ssh/authorized_keys, 老的OpenSSH版本可能存放在~/.ssh/authorized_keys2.
    27  
    28 Public-key 认证机制比密码要安全, 因为密码不在网络上传输. 而且可以是用加密的方式存储的,如果没有别人没有passphrase,拿到密钥也没有用.为此一定要设置passphrase
    29  
    30 如果以上不步骤不能实现不输密码登陆,请检查:
    31 /etc/ssh/sshd_config:
    32 PubkeyAuthentication yes           If no, change it and restart sshd
    33 可以用ssh –v来显示详细的登陆过程.
    34  
    35 SSH-2 key文件格式:
    36 SSH的两种主要实现方式: OpenSSH and SSH Secure Shell ("SSH2")
    37 OpenSSH 的如下:
    38 ssh-dss A9AAB3NzaC1iGMqHpSCEliaouBun8FF9t8p...
    39 or:
    40 ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEA3DIqRox...
    41 SSH Secure Shell的如下:
    42 ---- BEGIN SSH2 PUBLIC KEY ----
    43 AAAAB3NzaC1kc3MAAACBAM4a2KKBE6zhPBgRx4q6Dbjxo5hXNKNWYIGkX/W/k5PqcCH0J6 ...
    44 ---- END SSH2 PUBLIC KEY ----
    45  
    46 SSH Secure Shell的安装方式也不同,目录在~/.ssh2 要在, ~/.ssh2/authorization中用如下方式提及: Key public_key_filename. 私钥也需要在~/.ssh2/identification用如下方式引用:
    47 IdKey private_key_filename
    48  
    使用Public Key (OpenSSH) 不用密码登陆
      1 AIX6.1默认是没有安装SSH服务的,要使用SSH连接AIX服务器,必须手动安装OpenSSH软件。
      2 
      3 1.在IBM官方网站下载Openssl和OPenSSH软件(可下载本文附件)。
      4 2.将下载的软件上传至AIX系统/tmp目录下。
      5 3.解压软件
      6 
      7     bash-3.2# uncompress openssl-fips-12.9.8.1801.tar
      8     bash-3.2# tar xvf openssl-fips-12.9.8.1801.tar 
      9     x openssl-fips-12.9.8.1801
     10     x openssl-fips-12.9.8.1801/openssl.base, 7648256 bytes, 14938 media blocks.
     11     x openssl-fips-12.9.8.1801/openssl.license, 3072 bytes, 6 media blocks.
     12     x openssl-fips-12.9.8.1801/openssl.man.en_US, 2350080 bytes, 4590 media blocks.
     13 
     14     bash-3.2# uncompress OpenSSH_5.8.0.6102.tar.Z
     15     bash-3.2# cd OpenSSH/
     16     bash-3.2# tar xvf OpenSSH_5.8.0.6102.tar 
     17 
     18 
     19 
     20 
     21 OS:AIX 5.3.10
     22 -------------------------------------安装openssl----------------------------------
     23 1.下载openssl
     24 https://www14.software.ibm.com/webapp/iwm/web/reg/download.do?source=aixbp&S_PKG=openssl&lang=en_US#
     25 选择OpenSSL Install images v0.9.8.1802 ( OpenSSL 0.9.8r ) for AIX 5.3 , AIX 6.1 & 7.0
     26  
     27 2.解压缩安装包
     28 [hxl/root]#ls -1 openssl-0.9.8.1802.tar.Z
     29 openssl-0.9.8.1802.tar.Z
     30 [hxl/root]#uncompress openssl-0.9.8.1802.tar.Z
     31  
     32 3.查看tar的内容
     33 [hxl/root]#tar -vtf openssl-0.9.8.1802.tar
     34 drwxr-xr-x   0 0        0 Jun 08 03:33:11 2012 openssl-0.9.8.1802/
     35 -rw-r-----   0 0  6316032 Jun 08 03:32:42 2012 openssl-0.9.8.1802/openssl.base
     36 -rw-r-----   0 0     3072 Jun 08 03:32:52 2012 openssl-0.9.8.1802/openssl.license
     37 -rw-r-----   0 0  2350080 Jun 08 03:33:41 2012 openssl-0.9.8.1802/openssl.man.en_US
     38 [hxl/root]#mkdir opensshl
     39 [hxl/root]#mv openssl-0.9.8.1802.tar ./openssh1
     40 [hxl/root]#tar -xvf openssl-0.9.8.1802.tar
     41  
     42 4.安装
     43 smitty install指定软件的目录,选择安装:openssl.base
     44 ACCEPT new license agreements? 选择YES
     45 
     46 ---------------------------安装openssh-----------------------------------
     47 1.下载openssh,下载地址:http://sourceforge.net/projects/openssh-aix/files/
     48 [hxl/root]#ls -1 openssh_5.2p1_aix53.tar.z
     49 openssh_5.2p1_aix53.tar.z
     50  
     51 2.修改文件名,将后缀小写的z改为大写,然后解压缩
     52 [hxl/root]#mv openssh_5.2p1_aix53.tar.z openssh_5.2p1_aix53.tar.Z
     53 [hxl/root]#uncompress openssh_5.2p1_aix53.tar.Z
     54  
     55 3.查看tar文件内容
     56 [hxl/root]#tar -vtf openssh_5.2p1_aix53.tar
     57 
     58 解压到当前目录
     59 [hxl/root]#mv openssh_5.2p1_aix53.tar ./openssh
     60 [hxl/root]#tar -xvf openssh_5.2p1_aix53.tar
     61  
     62 4.安装
     63 smitty install指定软件软件存放的目录,选择openssh.base
     64 ACCEPT new license agreements? 选择YES
     65  
     66 ------------------------------------运行ssh服务--------------------------
     67 5.运行ssh服务
     68 默认安装好后系统自动启用ssh服务:
     69 [hxl/root]#lssrc -a | grep ssh
     70  sshd             ssh              708860       active
     71  
     72 启动sshd服务
     73 [hxl/root]#startsrc -s sshd
     74  
     75 停止sshd服务
     76 [hxl/root]#stopsrc -s sshd
     77  
     78 这样就可以通过putty或则ssh登陆了
     79 
     80 4.首先安装Openssl,再安装OpenSSH,安装先后顺序不可颠倒
     81 
     82     bash-3.2# cd openssl-fips-12.9.8.1801
     83     bash-3.2# smitty install
     84 
     85 顺序选择Install and Update Software、Install Software,
     86 INPUT device / directory for software   [.] 输入当前目录.
     87 SOFTWARE to install [_all_latest]   按F4、然后ESC+7选择列出行
     88  "ACCEPT new license agreements?"  [yes] 
     89 回车,执行安装
     90 Command:OK  表示安装完成。
     91 OpenSSH安装方法同上。
     92 
     93 5.安装完成后SSH服务会自动启动,确认:
     94 
     95     bash-3.2# lssrc -s sshd
     96     Subsystem Group PID Status
     97     sshd ssh 7077986 active
     98 
     99 6.启动和停止服务:
    100 
    101     bash-3.2# stopsrc -s sshd
    102     0513-044 The sshd Subsystem was requested to stop.
    103     bash-3.2# lssrc -s sshd
    104     Subsystem         Group            PID          Status 
    105      sshd             ssh                           inoperative
    106 
    107     bash-3.2# startsrc -s sshd 
    108     0513-059 The sshd Subsystem has been started. Subsystem PID is 19660822.
    109     bash-3.2# lssrc -s sshd
    110     Subsystem         Group            PID          Status 
    111      sshd             ssh              19660822     active
    AIX开启SSH
  • 相关阅读:
    [转]Splay算法
    [转]模拟退火算法
    [转]九种背包问题
    [转]C++实现平衡二叉树
    关于set
    __builtin_popcount
    为什么调用线程的join方法,等待的是执行join方法的线程
    volatile and 指令重排序 (单例模式用volatile来声明单例对象)
    线程池
    sts 转用 IDEA随记
  • 原文地址:https://www.cnblogs.com/book-gary/p/5500404.html
Copyright © 2011-2022 走看看