In some cases, you have to check server status whether server is running or not using telnet command before executing commands on the server application. This code can be used to check whether there is a running something on the given IP and Port.
import java.io.DataInputStream;
import java.io.InputStream;
import java.net.*;
public class Telnet {
public static void main(String args[]) {
try {
String ip = "192.168.*.*";
int port = 23;
Socket s1 = new Socket(ip, port);
InputStream is = s1.getInputStream();
DataInputStream dis = new DataInputStream(is);
if (dis != null) {
System.out.println("Connected IP : " + ip + ", Port : " + port);
} else {
System.out.println("Connection Invalid.");
}
dis.close();
s1.close();
} catch (Exception e) {
System.out.println("Not Connected, check Ip and Port.");
}
}
}
Monday, January 12, 2015
Wednesday, December 31, 2014
Create .tar file in Linux
Following command can be used to create .tar file in Linux.
tar -cvf all.tar *.txt
Above command ceate tar files for all .txt files in the directory.
tar -cvf all.tar *.txt
Above command ceate tar files for all .txt files in the directory.
Wednesday, December 10, 2014
Find port usage
Following command can be used to check port usage.
In windows command prompt type following command.
In windows command prompt type following command.
netstat -aon |find /i "listening" |find
"port"
This will return port usage.
Thursday, December 4, 2014
SSH vfs2 Change FTP Directory
Following configuration can be used to change logged user directory. In normally, FTP directory is set to logging user home. Sometimes, you have to access directories outside of the logging SFTP user home.
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, false);
This code snip can be used, when you are using vfs2 SFTP client.
Now, you can FTP files in any location using following SFTP URI.
"sftp://" + userID + ":" + password + "@" + sftpHost + "/" + remoteDirectory + fileName
Change remote directory as you preferred.
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, false);
This code snip can be used, when you are using vfs2 SFTP client.
Now, you can FTP files in any location using following SFTP URI.
"sftp://" + userID + ":" + password + "@" + sftpHost + "/" + remoteDirectory + fileName
Change remote directory as you preferred.
Wednesday, November 12, 2014
Ubuntu Graphical Desktop
In Ubuntu VM installations, you can't get graphical desktop. It only prompt for terminal view. You can use following command to start Ubuntu in graphical view.
startx
If this command not found, hen you have to update Ubuntu desktop using following command.
sudo apt-get install ubuntu-desktop
Then use startx command.
startx
If this command not found, hen you have to update Ubuntu desktop using following command.
sudo apt-get install ubuntu-desktop
Then use startx command.
Tuesday, October 28, 2014
SFTP in JAVA
Following JAVA functions can be used to execute SFTP(Secure FTP).
public boolean putSFTPFile(String sftpHost, String userID, String password, String remoteDirectory, String localDirectory, String fileName) throws FileSystemException {
boolean put = false;
StandardFileSystemManager manager = new StandardFileSystemManager();
try {
String filepath = localDirectory + fileName;
File file = new File(filepath);
if (!file.exists()) {
throw new RuntimeException("Error. Local file not found.");
}
manager.init();
FileSystemOptions opts = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(
opts, "no");
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
String sftpUri = "sftp://" + userID + ":" + password + "@" + sftpHost + "/" + remoteDirectory + fileName;
FileObject localFile = manager.resolveFile(file.getAbsolutePath());
System.out.println(file.getAbsolutePath());
FileObject remoteFile = manager.resolveFile(sftpUri, opts);
remoteFile.copyFrom(localFile, Selectors.SELECT_SELF);
put = true;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
manager.close();
}
return put;
}
public boolean getSFTPFile(String sftpHost, String userID, String password, String remoteDirectory, String localDirectory, String fileName) {
boolean get = false;
StandardFileSystemManager manager = new StandardFileSystemManager();
try {
manager.init();
FileSystemOptions opts = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(
opts, "no");
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
String sftpUri = "sftp://" + userID + ":" + password + "@" + sftpHost + "/"
+ remoteDirectory + fileName;
String filepath = localDirectory + fileName;
File file = new File(filepath);
FileObject localFile = manager.resolveFile(file.getAbsolutePath());
FileObject remoteFile = manager.resolveFile(sftpUri, opts);
localFile.copyFrom(remoteFile, Selectors.SELECT_SELF);
get = true;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
manager.close();
}
return get;
}
public boolean deleteSFTFile(String sftpHost, String userID, String password, String remoteDirectory, String fileName) {
boolean delete = false;
StandardFileSystemManager manager = new StandardFileSystemManager();
try {
manager.init();
FileSystemOptions opts = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(
opts, "no");
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
String sftpUri = "sftp://" + userID + ":" + password + "@" + sftpHost + "/"
+ remoteDirectory + fileName;
FileObject remoteFile = manager.resolveFile(sftpUri, opts);
if (remoteFile.exists()) {
if (remoteFile.delete()) {
delete = true;
}
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
manager.close();
}
return delete;
}
public Collection getSFTPFileList(String sftpHost, String userID, String password, String remoteDirectory) throws FileSystemException {
Collection fileCollection = new ArrayList();
StandardFileSystemManager manager = new StandardFileSystemManager();
FileObject localFileObject = manager.resolveFile("sftp://" + userID + ":" + password + "@" + sftpHost + "/" + remoteDirectory);
FileObject[] children = localFileObject.getChildren();
for (int i = 0; i < children.length; i++) {
fileCollection.add(children[ i].getName().getBaseName());
}
return fileCollection;
}
public boolean isRemoteFileExist(String sftpHost, String userID, String password, String remoteDirectory, String fileName) {
StandardFileSystemManager manager = new StandardFileSystemManager();
boolean isExist = false;
try {
manager.init();
//Setup our SFTP configuration
FileSystemOptions opts = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(
opts, "no");
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
String sftpUri = "sftp://" + userID + ":" + password + "@" + sftpHost + "/"
+ remoteDirectory + fileName;
FileObject remoteFile = manager.resolveFile(sftpUri, opts);
isExist = remoteFile.exists();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
manager.close();
}
return isExist;
}
public boolean putSFTPFile(String sftpHost, String userID, String password, String remoteDirectory, String localDirectory, String fileName) throws FileSystemException {
boolean put = false;
StandardFileSystemManager manager = new StandardFileSystemManager();
try {
String filepath = localDirectory + fileName;
File file = new File(filepath);
if (!file.exists()) {
throw new RuntimeException("Error. Local file not found.");
}
manager.init();
FileSystemOptions opts = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(
opts, "no");
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
String sftpUri = "sftp://" + userID + ":" + password + "@" + sftpHost + "/" + remoteDirectory + fileName;
FileObject localFile = manager.resolveFile(file.getAbsolutePath());
System.out.println(file.getAbsolutePath());
FileObject remoteFile = manager.resolveFile(sftpUri, opts);
remoteFile.copyFrom(localFile, Selectors.SELECT_SELF);
put = true;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
manager.close();
}
return put;
}
public boolean getSFTPFile(String sftpHost, String userID, String password, String remoteDirectory, String localDirectory, String fileName) {
boolean get = false;
StandardFileSystemManager manager = new StandardFileSystemManager();
try {
manager.init();
FileSystemOptions opts = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(
opts, "no");
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
String sftpUri = "sftp://" + userID + ":" + password + "@" + sftpHost + "/"
+ remoteDirectory + fileName;
String filepath = localDirectory + fileName;
File file = new File(filepath);
FileObject localFile = manager.resolveFile(file.getAbsolutePath());
FileObject remoteFile = manager.resolveFile(sftpUri, opts);
localFile.copyFrom(remoteFile, Selectors.SELECT_SELF);
get = true;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
manager.close();
}
return get;
}
public boolean deleteSFTFile(String sftpHost, String userID, String password, String remoteDirectory, String fileName) {
boolean delete = false;
StandardFileSystemManager manager = new StandardFileSystemManager();
try {
manager.init();
FileSystemOptions opts = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(
opts, "no");
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
String sftpUri = "sftp://" + userID + ":" + password + "@" + sftpHost + "/"
+ remoteDirectory + fileName;
FileObject remoteFile = manager.resolveFile(sftpUri, opts);
if (remoteFile.exists()) {
if (remoteFile.delete()) {
delete = true;
}
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
manager.close();
}
return delete;
}
public Collection getSFTPFileList(String sftpHost, String userID, String password, String remoteDirectory) throws FileSystemException {
Collection fileCollection = new ArrayList();
StandardFileSystemManager manager = new StandardFileSystemManager();
FileObject localFileObject = manager.resolveFile("sftp://" + userID + ":" + password + "@" + sftpHost + "/" + remoteDirectory);
FileObject[] children = localFileObject.getChildren();
for (int i = 0; i < children.length; i++) {
fileCollection.add(children[ i].getName().getBaseName());
}
return fileCollection;
}
public boolean isRemoteFileExist(String sftpHost, String userID, String password, String remoteDirectory, String fileName) {
StandardFileSystemManager manager = new StandardFileSystemManager();
boolean isExist = false;
try {
manager.init();
//Setup our SFTP configuration
FileSystemOptions opts = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(
opts, "no");
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
String sftpUri = "sftp://" + userID + ":" + password + "@" + sftpHost + "/"
+ remoteDirectory + fileName;
FileObject remoteFile = manager.resolveFile(sftpUri, opts);
isExist = remoteFile.exists();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
manager.close();
}
return isExist;
}
Monday, October 27, 2014
Password Authenticate with SALT value
In password authentication, most developers use hash value generation. Hash value has a unique value for every word. If two users have same password, both has same hash value for their password. The solution is to generate salt value for password. Before proceeding hash generating, password is appended with salt value. The salt value is not a secret value. For validation, hash value and salt value must be known from the developer.
Following article for user authentication described clearly with sources..
https://crackstation.net/hashing-security.htm#normalhashing
Following article for user authentication described clearly with sources..
https://crackstation.net/hashing-security.htm#normalhashing
Subscribe to:
Posts (Atom)