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 });
調用:<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 });
備注:
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
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
