Advertisement
Guest User

checkServers()

a guest
Jun 28th, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     func checkServers() {
  2.         dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) {
  3.             var servers = getServers()
  4.             if (servers.count != 0){
  5.                 for index in 0...(servers.count - 1) {
  6.                     //dispatch_async(dispatch_get_main_queue()) {
  7.                         setStatusForAlias(servers[index]["alias"]!, status: "-1")
  8.                         self.drawStatus(NSIndexPath(forRow: ServerIndexForAlias(servers[index]["alias"]!), inSection: 0), status: -1)
  9.                         (self.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: ServerIndexForAlias(servers[index]["alias"]!), inSection: 0))!.viewWithTag(3) as! UIImageView).image = UIImage(named:"finding")
  10.                     //}
  11.                 }
  12.                 for index in 0...(servers.count - 1) {
  13.                    
  14.                     let alias = servers[index]["alias"]!
  15.                    
  16.                     //NMSSHLogger.sharedLogger().enabled = false
  17.                    
  18.                     let session = NMSSHSession.connectToHost(servers[index]["ip"]!, port: (servers[index]["port"]! != "" && Int(servers[index]["port"]!) != nil) ? Int(servers[index]["port"]!)! : 22, withUsername: servers[index]["ip"])
  19.                     session.timeout = 1
  20.                    
  21.                     dispatch_sync(
  22.                         dispatch_get_main_queue(), {
  23.                             print(session.lastError)})
  24.                    
  25.                     if (session.connected){
  26.                        
  27.                         dispatch_sync(
  28.                             dispatch_get_main_queue(), {
  29.                                
  30.                                 if(session.supportedAuthenticationMethods().contains({$0 as! String == "password"})){
  31.                                     var start = true
  32.                                     while (start || session.lastError.code == -9){
  33.                                         print(servers[index]["username"])
  34.                                         start = false
  35.                                         session.authenticateByPassword(servers[index]["password"]!)
  36.                                         print(session.lastError)
  37.                                     }
  38.                                 } else {
  39.                                     var start = true
  40.                                     while (start || session.lastError.code == -9){
  41.                                         print(servers[index]["username"])
  42.                                         start = false
  43.                                         //print(session.host)
  44.                                         session.authenticateByKeyboardInteractiveUsingBlock(
  45.                                             {(test: String!) -> String! in
  46.                                                 //print("attention: \(test)")
  47.                                                 return servers[index]["password"]!
  48.                                             }
  49.                                         )
  50.                                         print(session.lastError)
  51.                                     }
  52.                                 }
  53.                             }
  54.                         )
  55.                        
  56.                         if session.authorized == true {
  57.                             dispatch_async(dispatch_get_main_queue()) {
  58.                                 setStatusForAlias(alias, status: "0")
  59.                                 self.drawStatus(NSIndexPath(forRow: ServerIndexForAlias(alias), inSection: 0), status: 0)
  60.                                 (self.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: ServerIndexForAlias(servers[index]["alias"]!), inSection: 0))!.viewWithTag(3) as! UIImageView).image = UIImage(named:"unknown")
  61.                             }
  62.                         } else {
  63.                             dispatch_async(dispatch_get_main_queue()) {
  64.                                 setStatusForAlias(alias, status: "1")
  65.                                 self.drawStatus(NSIndexPath(forRow: ServerIndexForAlias(alias), inSection: 0), status: 1)
  66.                                 (self.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: ServerIndexForAlias(servers[index]["alias"]!), inSection: 0))!.viewWithTag(3) as! UIImageView).image = UIImage(named:"notFound")
  67.                             }
  68.                         }
  69.                     } else {
  70.                         dispatch_async(dispatch_get_main_queue()) {
  71.                             setStatusForAlias(alias, status: "2")
  72.                             self.drawStatus(NSIndexPath(forRow: ServerIndexForAlias(alias), inSection: 0), status: 2)
  73.                             (self.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: ServerIndexForAlias(servers[index]["alias"]!), inSection: 0))!.viewWithTag(3) as! UIImageView).image = UIImage(named:"notFound")
  74.                         }
  75.                     }
  76.                     session.disconnect()
  77.                 }
  78.             }
  79.         }
  80.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement