2016-01-20 70 views
2

我正在構建一個簡單的程序,該程序使用Ganymed for SSH2庫對Linux服務器執行單個命令。這裏是我的代碼...System.out.println同時打印

public class Main { 

static Scanner reader = new Scanner(System.in); 


public static void main(String[] args) throws IOException, InterruptedException{   


    String hostname; // = "192.168.1.241"; 
    int port; // = 22 
    String username; // = "root"; 
    String password; 
    String cmd; // = "echo 'Hello World'"; 

    //ask user for hostname/IP 
    System.out.print("Enter hostname or IP: "); 
    hostname = reader.nextLine(); 


    //ask user for port # 
    System.out.print("Enter port number: "); 
    port = reader.nextInt();   

     //create connection 
     Connection connect = new Connection(hostname, port); 

    //ask user for username 
    System.out.println("Enter Username (best to use 'root'): "); 
    username = reader.nextLine(); 

    //ask user for password 
    System.out.println("Enter Password: "); 
    password = reader.nextLine(); 


    System.out.println("Attempting to log in..."); 

    reader.close(); 

     //establish connection 
     connect.connect(); 

     //login using password 
     connect.authenticateWithPassword(username, password); 

     Thread.sleep(1000); 
     if (Thread.interrupted()) // Clears interrupted status! 
       throw new InterruptedException(); 

     Boolean didConnect = connect.isAuthenticationComplete(); 
     if (didConnect==false){ 
      throw new IOException("Authentication Unsucessful \nCheck hostname and port number, then try again."); 
     } 

      //open session 
      Session session = connect.openSession(); 
      System.out.println("Opened session!");    




     System.out.println("Enter a command to execute: "); 
     cmd = reader.nextLine(); 

     //execute command 
     session.execCommand(cmd); 


     //get output 
     InputStream stdout = new StreamGobbler(session.getStdout()); 

     BufferedReader br = new BufferedReader(new InputStreamReader(stdout)); 

     String line = br.readLine(); 
     if (line!=null){ 
      System.out.println("Command sent succesfully!" + "\nOutput: " + "\n" + "\n" + line); 
     }else{ 
      System.out.println("Command sent succesfully!" + "\nNo Output"); 
     } 

     //close buffered reader 
     br.close(); 

     //close session 
     session.close(); 

     //close connection 
     connect.close(); 


    } 



} 

出於某種原因......同時

//ask user for username 
    System.out.println("Enter Username (best to use 'root'): "); 
    username = reader.nextLine(); 

    //ask user for password 
    System.out.println("Enter Password: "); 
    password = reader.nextLine(); 

打印,我不知道爲什麼!當使用打印ln並且在使用打印時在相同的行上,它也在完全不同的行上獲得用戶輸入。

我希望他們先詢問用戶名,然後再輸入密碼,將用戶輸入放在輸出旁邊。但是,當我要求他們單獨/按順序打印主機名和端口號時,就像我希望的那樣。我究竟做錯了什麼?

回答

1

你做port = reader.nextInt();後,您需要將一個reader.nextLine()讀取線(和換行符),其餘它具有INT。

//ask user for port # 
System.out.print("Enter port number: "); 
port = reader.nextInt();   

reader.nextInt()不閱讀次數後換行或任何東西。

添加reader.nextLine()然後將代碼的其餘部分應遵循:

reader.nextLine(); 

//ask user for username 
System.out.println("Enter Username (best to use 'root'): "); 
username = reader.nextLine(); 

//ask user for password 
System.out.println("Enter Password: "); 
password = reader.nextLine(); 
+0

謝謝,這工作,很好的解釋 –

-1

您的問題是您多次使用相同的掃描儀對象來讀取一行。它跳過代碼:

//ask user for username 
System.out.println("Enter Username (best to use 'root'): "); 
username = reader.nextLine(); 

//ask user for password 
System.out.println("Enter Password: "); 
password = reader.nextLine(); 

因爲對象已經爲您調用的方法賦值。

解決方案:重新實例每次使用它時的對象:

reader = new Scanner(System.in); 
+0

我想是因爲它正在執行的代碼順序單線程程序(如果讓任何意義...即時通訊新的編程)我好的,重新使用掃描儀。以前的答案工作 –

+0

我也過早關閉掃描儀,我仍然需要它來檢索命令輸入 –

相關問題