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;
}
Tuesday, October 28, 2014
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
Wednesday, October 1, 2014
Cron to STOP/ START Program
In Linux, you can use crontab to stop/ start applications.
Following shell script (stop.sh) can be used to grep your program & kill it.
Following shell script (stop.sh) can be used to grep your program & kill it.
#!/bin/bash
PGID=`ps -eaf | grep YourProgram | grep YourName | grep -v grep | awk '{print $2}'`
if [ -n $PGID ]
then
/bin/kill -9 $PGID
else
echo "Not Running"
fi
After stopping, you can run the following program(start.sh) to restart the application. This will grep to check whether application runs or not. If runs, shell terminates with "Already Running" error. Else, it will run the application with nohup and send a mail to owner using mailx linux API.
#!/bin/bash
PRGHOME=/home/sujith/ReminderSMS
ps –eaf | grep YourProgram| grep YourName|grep -v grep |
awk '{print $2}' > /home/ sujith/.tmp.ReminderAlert
if [ -s /home/ sujith/.tmp.ReminderAlert ]
then
echo "Already Running"
else
#. /home/ sujith/.bash_profile
cd $PRGHOME
echo 'program_alert_started' | mailx sujith@test1.lk
nohup java YourProgram &
rm /home/ sujith/.tmp.ReminderAlert
fi
Subscribe to:
Posts (Atom)