Java Code Examples for ch.ethz.ssh2.Connection#authenticateWithPassword()
The following examples show how to use
ch.ethz.ssh2.Connection#authenticateWithPassword() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ScpService.java From wecube-platform with Apache License 2.0 | 6 votes |
public void put(String ip, Integer port, String user, String password, String localFile, String remoteTargetDirectory) { Connection conn = new Connection(ip, port); try { conn.connect(); boolean isconn = conn.authenticateWithPassword(user, password); if (isconn) { log.info("Connection is OK"); SCPClient scpClient = conn.createSCPClient(); log.info("scp local file [{}] to remote target directory [{}]", localFile, remoteTargetDirectory); scpClient.put(localFile, remoteTargetDirectory, "7777"); } else { log.info("User or password incorrect"); throw new WecubeCoreException("User or password incorrect"); } } catch (Exception e) { e.printStackTrace(); throw new WecubeCoreException("Run 'scp' command meet error: " + e.getMessage()); } finally { conn.close(); } }
Example 2
Source File: SSH2Util.java From util with Apache License 2.0 | 6 votes |
/** * 功能:登录 * @author 朱志杰 QQ:862990787 * Sep 2, 2013 9:41:45 AM * @param hostname ssh2的hostname * @param port 端口 SSH2的默认端口是22 * @param userName 用户名 * @param pwd 密码 * @throws IOException */ public boolean login(String hostname,int port,String userName,String pwd) throws IOException{ /* Create a connection instance */ conn = new Connection(hostname,port); /* Now connect */ conn.connect(); /* Authenticate. * If you get an IOException saying something like * "Authentication method password not supported by the server at this stage." * then please check the FAQ. */ boolean isAuthenticated = conn.authenticateWithPassword(userName, pwd); //sftp sftp=new SFTPv3Client(conn); return isAuthenticated; }
Example 3
Source File: SSHTemplate.java From cachecloud with Apache License 2.0 | 6 votes |
/** * 获取连接并校验 * @param ip * @param port * @param username * @param password * @return Connection * @throws Exception */ private Connection getConnection(String ip, int port, String username, String password) throws Exception { Connection conn = new Connection(ip, port); conn.connect(null, CONNCET_TIMEOUT, CONNCET_TIMEOUT); boolean isAuthenticated = false; if (ConstUtils.SSH_AUTH_TYPE == SshAuthTypeEnum.PASSWORD.getValue()) { isAuthenticated = conn.authenticateWithPassword(username, password); } else if (ConstUtils.SSH_AUTH_TYPE == SshAuthTypeEnum.PUBLIC_KEY.getValue()) { isAuthenticated = conn.authenticateWithPublicKey(username, new File(ConstUtils.PUBLIC_KEY_PEM), password); } if (isAuthenticated == false) { if (ConstUtils.SSH_AUTH_TYPE == SshAuthTypeEnum.PASSWORD.getValue()) { throw new Exception("SSH authentication failed with [ userName: " + username + ", password: " + password + "]"); } else if (ConstUtils.SSH_AUTH_TYPE == SshAuthTypeEnum.PUBLIC_KEY.getValue()) { throw new Exception("SSH authentication failed with [ userName: " + username + ", pemfile: " + ConstUtils.PUBLIC_KEY_PEM + "]"); } } return conn; }
Example 4
Source File: SSHUtil.java From EserKnife with Apache License 2.0 | 6 votes |
/** * 将 * * @param ip * @param port * @param username * @param password * @param localPath * @param remoteDir * @return */ public static boolean scpFileToRemote(String ip, int port, String username, String password, String localPath, String remoteDir) throws SSHException { boolean isSuccess = true; Connection connection = new Connection(ip, port); try { connection.connect(); boolean isAuthed = connection.authenticateWithPassword(username, password); if (!isAuthed) { throw new SSHException("auth error."); } SCPClient scpClient = connection.createSCPClient(); scpClient.put(localPath, remoteDir, "0644"); } catch (IOException e) { isSuccess = false; throw new SSHException("scp file to remote error.", e); } finally { if (connection != null) { connection.close(); } } return isSuccess; }
Example 5
Source File: ScpClientUtil.java From eladmin with Apache License 2.0 | 6 votes |
public void getFile(String remoteFile, String localTargetDirectory) { Connection conn = new Connection(ip, port); try { conn.connect(); boolean isAuthenticated = conn.authenticateWithPassword(username, password); if (!isAuthenticated) { System.err.println("authentication failed"); } SCPClient client = new SCPClient(conn); client.get(remoteFile, localTargetDirectory); } catch (IOException ex) { Logger.getLogger(SCPClient.class.getName()).log(Level.SEVERE, null, ex); }finally{ conn.close(); } }
Example 6
Source File: ScpClientUtil.java From eladmin with Apache License 2.0 | 6 votes |
public void putFile(String localFile, String remoteFileName, String remoteTargetDirectory, String mode) { Connection conn = new Connection(ip, port); try { conn.connect(); boolean isAuthenticated = conn.authenticateWithPassword(username, password); if (!isAuthenticated) { System.err.println("authentication failed"); } SCPClient client = new SCPClient(conn); if ((mode == null) || (mode.length() == 0)) { mode = "0600"; } if (remoteFileName == null) { client.put(localFile, remoteTargetDirectory); } else { client.put(localFile, remoteFileName, remoteTargetDirectory, mode); } } catch (IOException ex) { Logger.getLogger(ScpClientUtil.class.getName()).log(Level.SEVERE, null, ex); }finally{ conn.close(); } }
Example 7
Source File: SUTTest.java From bestconf with Apache License 2.0 | 6 votes |
public Connection getConnection(){ int round = 0; while(round<maxRoundConnection){ try{ connection = new Connection(server); connection.connect(); boolean isAuthenticated = connection.authenticateWithPassword(username, password); if (isAuthenticated == false) { throw new IOException("Authentication failed..."); } break; }catch (Exception e) { e.printStackTrace(); connection.close(); connection = null; System.err.println("================= connection is null in round "+round); try { Thread.sleep(sshReconnectWatingtime*1000); } catch (InterruptedException e1) { e1.printStackTrace(); } } round++; } return connection; }
Example 8
Source File: SUTSystemOperation.java From bestconf with Apache License 2.0 | 6 votes |
public Connection getConnection(){ int round = 0; while(round<maxRoundConnection){ try{ connection = new Connection(server); connection.connect(); boolean isAuthenticated = connection.authenticateWithPassword(username, password); if (isAuthenticated == false) { throw new IOException("Authentication failed..."); } break; }catch (Exception e) { e.printStackTrace(); connection.close(); connection = null; System.err.println("================= connection is null in round "+round); try { Thread.sleep(sshReconnectWatingtime*1000); } catch (InterruptedException e1) { e1.printStackTrace(); } } round++; } return connection; }
Example 9
Source File: CtrCommond.java From myrover with Apache License 2.0 | 6 votes |
/** * 获取服务器链接 * @param hostname * @param username * @param password * @return */ public static Connection getConn(String hostName,String userName,int sshPort,String passWord){ try { Connection conn = new Connection(hostName,sshPort); //连接到主机 conn.connect(); //使用用户名和密码校验 boolean isconn = conn.authenticateWithPassword(userName, passWord); if(!isconn){ System.out.println("用户名称或者是密码不正确"); }else{ return conn; } } catch (IOException e) { System.out.println("获取服务器链接出现异常:"+e.toString()); return null; } return null; }
Example 10
Source File: Tools.java From fastdfs-zyc with GNU General Public License v2.0 | 5 votes |
public static List<String> exeRemoteConsole(String hostname, String username, String password, String cmd) { List<String> result = new ArrayList<String>(); //指明连接主机的IP地址 Connection conn = new Connection(hostname); Session ssh = null; try { //连接到主机 conn.connect(); //使用用户名和密码校验 boolean isconn = conn.authenticateWithPassword(username, password); if (!isconn) { logger.error("用户名称或者是密码不正确"); } else { logger.info("已经连接OK"); ssh = conn.openSession(); //使用多个命令用分号隔开 // ssh.execCommand("pwd;cd /tmp;mkdir shb;ls;ps -ef|grep weblogic"); ssh.execCommand(cmd); //只允许使用一行命令,即ssh对象只能使用一次execCommand这个方法,多次使用则会出现异常 // ssh.execCommand("mkdir hb"); //将屏幕上的文字全部打印出来 InputStream is = new StreamGobbler(ssh.getStdout()); BufferedReader brs = new BufferedReader(new InputStreamReader(is)); for (String line = brs.readLine(); line != null; line = brs.readLine()) { result.add(line); } } //连接的Session和Connection对象都需要关闭 if (ssh != null) { ssh.close(); } conn.close(); } catch (IOException e) { logger.error("", e); } return result; }
Example 11
Source File: ScpService.java From wecube-platform with Apache License 2.0 | 5 votes |
public boolean isAuthedWithPassword(String ip, Integer port, String user, String password) { Connection connection = new Connection(ip, port); try { return connection.authenticateWithPassword(user, password); } catch (IOException e) { e.printStackTrace(); } return false; }
Example 12
Source File: SSHUtil.java From xnx3 with Apache License 2.0 | 5 votes |
/** * 登陆 * @return boolean * @throws IOException */ public boolean open(){ conn = new Connection(ip); try { conn.connect(); return conn.authenticateWithPassword(this.username, this.password); } catch (IOException e) { e.printStackTrace(); } return false; }
Example 13
Source File: SSHTemplate.java From javabase with Apache License 2.0 | 5 votes |
/** * 获取连接并校验 * @param ip * @param port * @param username * @param password * @return Connection * @throws Exception */ private Connection getConnection(String ip, int port, String username, String password) throws Exception { Connection conn = new Connection(ip, port); conn.connect(null, CONNCET_TIMEOUT, CONNCET_TIMEOUT); boolean isAuthenticated = conn.authenticateWithPassword(username, password); if (isAuthenticated == false) { throw new Exception("SSH authentication failed with [ userName: " + username + ", password: " + password + "]"); } return conn; }
Example 14
Source File: SSHExecutor.java From PoseidonX with Apache License 2.0 | 5 votes |
/** * 获取连接并校验 * * @param ip 目标服务器ip * @param port 目标服务器port * @param username 用户名 * @param password 密码 * @throws Exception */ private Connection getConnection(String ip, int port, String username, String password) throws Exception { Connection conn = new Connection(ip, port); conn.connect(null, CONNCET_TIMEOUT, CONNCET_TIMEOUT); boolean isAuthenticated = conn.authenticateWithPassword(username, password); if (!isAuthenticated) { throw new Exception("SSH authentication failed with [ userName: " + username + ", password: " + password + "]"); } return conn; }
Example 15
Source File: SSHExecutor.java From zkdoctor with Apache License 2.0 | 5 votes |
/** * 获取ssh连接 * * @param ip 目标服务器ip * @param port 目标服务器port * @param username 用户名 * @param password 密码 * @throws Exception */ private Connection getConnection(String ip, int port, String username, String password) throws Exception { Connection conn = new Connection(ip, port); conn.connect(null, CONNCET_TIMEOUT, CONNCET_TIMEOUT); boolean isAuthenticated = conn.authenticateWithPassword(username, password); if (!isAuthenticated) { throw new Exception("Get connection failed because SSH authentication failed, username:" + username + ", passsword:" + password); } return conn; }
Example 16
Source File: SshUtil.java From dk-fitting with Apache License 2.0 | 5 votes |
static Connection createConn(String hostIp, String hostName, String hostPassword) throws Exception { Connection conn = new Connection(hostIp); conn.connect(); boolean isAuthenticated = conn.authenticateWithPassword(hostName, hostPassword); if (isAuthenticated == false) throw new IOException("Authentication failed."); return conn; }
Example 17
Source File: PooledRemoteCommandExecutor.java From wecube-platform with Apache License 2.0 | 5 votes |
protected void buildConnection() throws IOException { Connection conn = new Connection(getConfig().getRemoteHost(), getConfig().getPort()); conn.connect(); boolean isAuthenticated = conn.authenticateWithPassword(getConfig().getUser(), getConfig().getPsword()); if (!isAuthenticated) { if (conn != null) { conn.close(); } throw new IOException("authentication failed"); } this.sshConn = conn; }
Example 18
Source File: RemoteShellTool.java From Transwarp-Sample-Code with MIT License | 4 votes |
public boolean login() throws IOException { conn = new Connection(ipAddr); conn.connect(); // 连接 return conn.authenticateWithPassword(userName, password); // 认证 }
Example 19
Source File: MysqlUtil.java From newblog with Apache License 2.0 | 4 votes |
/** * export database; */ public void exportDataBase() { logger.info("start backup database"); String username = Config.getProperty("jdbc.username_dev"); String password = Config.getProperty("jdbc.password_dev"); String database = Config.getProperty("jdbc.database"); String host = Config.getProperty("jdbc.host_dev"); String os = System.getProperty("os.name"); String file_path = null; // if (os.toLowerCase().startsWith("win")) { //根据系统类型 // file_path = System.getProperty("user.dir") + "\\sql\\"; // } else { // file_path = System.getProperty("user.dir") + "/sql/";//保存的路径 // } file_path = System.getProperty("myblog.path") + "sql"; String file_name = "/myblog" + DateTime.now().toString("yyyyMMddHHmmss") + ".sql"; String file = file_path + file_name; logger.info("file_path and file_name: " + file); //server String s_host = Config.getProperty("server.host"); Integer s_port = Config.getIntProperty("server.port"); String s_username = Config.getProperty("server.username"); String s_password = Config.getProperty("server.password"); try { StringBuffer sb = new StringBuffer(); sb.append(Common.MYSQL_DUMP).append(" -u ").append(username).append(" -p").append(password).append(" -h ").append(host).append(" ").append(database).append(" >").append(file); String sql = sb.toString(); logger.info(sql); //connect to server Connection connection = new Connection(s_host, s_port); connection.connect(); boolean isAuth = connection.authenticateWithPassword(s_username, s_password); if (!isAuth) { logger.error("server login error"); } Session session = connection.openSession(); session.execCommand(sql); InputStream stdout = new StreamGobbler(session.getStdout()); BufferedReader br = new BufferedReader(new InputStreamReader(stdout)); while (true) { String line = br.readLine(); if (line == null) break; System.out.println(line); } session.close(); connection.close(); stdout.close(); br.close(); logger.info("backup finish"); logger.info(sb.toString()); } catch (Exception e) { logger.error("error", e); } }