sftp JSch transfer file to remote cygwin - cygwin

I am trying copy files from local windows machine to remote windows machine using sftp JSch. Remote machine has cygwin, but file is not getting transferred. While doing, it is not throwing any error.
I tried with different destination path format like /cygdrive/d/ and also d://.
String destination = "/cygdrive/d/Test1";
String source = "D:\\Test";
List<String> files = NFileUtils.listFiles(source);
for (String f : files) {
String fileName = NFileUtils.getFilename(f);
try {
sftp.put(f, destination + "\\" + fileName);
} catch (Exception e) {
System.out.println(e);
}
}

sftp.put(f, destination + "\\" + fileName);
SFTP uses a file-naming model in which "/" is the directory separator character. Try using "/" as the path separator instead of "\":
sftp.put(f, destination + "/" + fileName);
If fileName contains any "\" characters, you'll need to change them as well.

Related

How to handle windows pop up to upload a file in Linux machine using selenium script

I am trying to automate a windows popup for file upload in selenium and want to trigger the script on LINUX.
There are tools like autoIT, JACOB which can handle these kinda pop-ups, but these doesn't work on LINUX directly.
I need help in any tool which does the job.
I have tried AutoIT, but due to autoIt exe not able to run the code on linux.
Then I tried JACOB but to use jacob needs to save two .dll files and did some exploration and found out taht Linux doen't support .dll it supports .so and as i am very new to linux don't have ides how to perform this.
private void fileUploadData(String fileName) {
String workingDir = "";
AutoItX uploadWindow = new AutoItX();;
try {
workingDir = System.getProperty("user.dir");
final String jacobdllarch =
System.getProperty("sun.arch.data.model")
.contains("32") ? "jacob-1.18-x86.dll" : "jacob-1.18-x64.dll";
String jacobdllpath = workingDir + "/" + jacobdllarch;
File filejacob = new File(jacobdllpath);
System.setProperty(LibraryLoader.JACOB_DLL_PATH,
filejacob+System.getProperty("user.dir"));
}
catch(Exception e) {
e.printStackTrace();
}
String filepath = workingDir + "\\src\\test\\resources\\testdata\\" +
fileName ;
File file=new File(filepath);
try {
file.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}
browseButton.click();
uploadWindow.winActive( "File Upload", "" );
uploadWindow.sleep(2000);
uploadWindow.ControlSetText( "Open", "", "Edit1", filepath);
uploadWindow.controlClick( "", "", "&Open" );
}
Getting below error on LINUX code is working fine on Windows -
Unsatisfied link error: no jacob-1.18-x64 in java.library.path: [/usr/java/packages/lib, /usr/lib64, /lib64, /lib, /usr/lib]

Switch to what ever path is input nodejs

Lets assume you have installed an electron app and you are asked to input the path to your current project. You might do something like: ~/Documents/projectName.
How do I, in node take that input and check if it exists, specifically if you entered in the path as shown above?
the reason for this is that I want to see if A) the path exists and B) if theres a specific file there (I'll be using path.join(dirEntered, fileName.extension).
Is there a way to do what I want? I see chdir but that changes where the working directory is. which I guess would be fine but doing:
process.chdir('~/Documents') Shows: no such file or directory, uv_chdir(…)
I want to avoid having the user to enter the full absolute path of their project. That seems "bad to me". And uploading their project isn't necessary, Im reading a single file (so theres no need for upload here).
Any ideas?
Is it possible to tap into the cli commands and take this input feed it there and get the result? Or is that over kill?
Here's an idea how to solve it. If the path starts with a tilde, it replaces that tilde with the full home directory of the current user. It then uses fs.stat to see if the given path actually exists.
const fs = require("fs");
const os = require("os");
var path = "~/Documents";
if (path.indexOf("~") === 0) {
path = os.homedir() + path.substring(1);
}
fs.stat(path, (err, stats) => {
if (!err) {
// document or path exists
if (stats.isFile()) {
console.log("Path " + path + " is a file");
} else if (stats.isDirectory()) {
console.log("Path " + path + " is a directory");
}
} else {
// document or path does not exist
}
});

Azure DSC - copying files from Storage Account with SAS - error "Relative path is not supported"

When I try to copy website (even a single file) to VM with DSC, with the following configuration:
File WebSiteContent {
Ensure = "Present"
SourcePath = "https://MYBLOB.blob.core.windows.net/prod/index.html?sv=2016-05-31&ss=b&srt=s&sp=rl&se=2017-11-29T21:47:36Z&st=2017-07-26T13:47:36Z&spr=https&sig=SIG_HERE"
DestinationPath = "C:\inetpub\backend\app_data"
Type = "Directory"
}
I get an error:
[ERROR] Relative path is not supported. The related file/directory is: https://MYBLOB.blob.core.windows.net/prod/index.html?sv=2016-05-31&ss=b&srt=s&sp=rl&se=2017-11-29T21:47:36Z&st=2017-07-26T13:47:36Z&spr=https&sig=SIG_HERE. \r\n
Not sure what is the reason, since this is an absolute path to the file. Any suggestions to make it work are appreciated :)
You need to use xRemoteFile from the xPSDesiredConfiguration module
File SetupDir {
Type = 'Directory'
DestinationPath = 'c:\Setup'
Ensure = "Present"
}
xRemoteFile SQLServerMangementPackage {
Uri = "http://go.microsoft.com/fwlink/?LinkID=824938"
DestinationPath = "c:\Setup\SSMS-Setup-ENU.exe"
DependsOn = "[File]SetupDir"
MatchSource = $false
}

fs.unlink doesn't delete the file

On my Express server I want to take the file uploaded by the user and rename it to match their username. If the username uploads a new file, the previous file is replaced.
Here's the code:
var newPath = 'uploads/' + user.username + '.' + (file.extension).toLowerCase();
var basePath = path.resolve(__dirname + '../../../') + '/';
// Copy, rename and delete temp file
var is = fs.createReadStream(basePath + file.path);
var os = fs.createWriteStream(basePath + newPath);
is.pipe(os);
is.on('end', function (error) {
if (err) return res.send(500);
fs.unlink(basePath + file.path);
});
Problem is that fs.unlink(basePath + file.path); doesn't actually delete the old file on my machine (OSX 10.9.2). How can i make sure the temp file is deleted?
The file basePath + file.path has link reference by the read stream is. The removal of the file contents shall be postponed until all references to the file are closed. You might want to call fs.unlink on the close event.
I just use fs.writeFile which overwrites a file. I have code in my app to this synchronously, but it looks like this:
if( !fs.existsSync( filename ) ) fs.openSync( filename, "wx" );
fs.writeFileSync( filename, data );
Basically I check if the file exists, and if it doesn't I open it. Then, I just write to the file. If it is new or already existing, only my data is present in the file when I look at it, overwriting what was there.

NODE.JS - how to handle a mix of OS and URL-style "paths" correctly?

In my node.js app I have functions which can be passed either
OS-style paths e.g. c:\my\docs\mydoc.doc (or /usr/docs/mydoc.doc or whatever is local)
File URLS e.g. file://c:/my/docs/mydoc.doc (which I'm not sure about the validity of '\'s in??)
Either way, I need to check to see if they refer to a specific location which will always exist as a local OS-style path e.g. c:\mydata\directory\ or /usr/mydata/directory
Obviously for OS-style paths I can just compare them as strings - they SHOULD always be the same (they're created with path) but FILE:// URLS don't necessarily use path.sep and so won't "string match"?
Any suggestions as to the best way to handle this (I'm personally tempted to break EVERYTHING by one-or-more slashes of either sort and then check each piece??
I'm going to post my own take on this - as it came from a suggestion I got from someone on Facebook (no - really!) and it uses path in a way it probably wasn't intended for - e.g. I'm not sure it's the right 'solution' - Im not sure I'm not exploiting path a bit.
The Facebook tip was that path is really just a utility for handing strings with "/" and "\" separators - it ignores everything else - doesn't care what's in there at all.
On that basis, we can use
path.normalize(ourpath)
which will convert all separators to the local OS preferred ones (path.sep)
That means they will match my OS-style directory (which is also made with path) and so I can compare those - without resorting to manually gutting-out slashes...
e.g.
Before
file://awkward/use/of\\slashes\in/this/path
After
file:\awkward\use\of\slashes\in\this\path (Windows)
or
file:/awkward/use/of/slashes/in/this/path (everywhere else)
Removing file:// before (or file: + path.sep after) = local OS-style path!?
Just do some manipulation of the string and check to make sure they are the same after correcting for the differences:
var path = require('path');
var pathname = "\\usr\\home\\newbeb01\\Desktop\\testinput.txt";
var pathname2 = "file://usr/home/newbeb01/Desktop/testinput.txt"
if(PreparePathNameForComparing(pathname) == PreparePathNameForComparing(pathname2))
{ console.log("Same path"); }
else
{ console.log("Not the same path"); }
function PreparePathNameForComparing(pathname)
{
var returnString = pathname;
//try to find the file uri prefix, if there strip it off
if(pathname.search("file://") != -1 || pathname.search("FILE://") != -1)
{ returnString = pathname.substring(6, pathname.length); }
//now make all slashes the same
if(path.sep === '\\') //replace all '/' with '\\'
{ returnString = returnString.replace(/\//g, '\\'); }
else //replace all '\\' with '/'
{ returnString = returnString.replace(/\\/g, '/'); }
return returnString;
}
I checked to see if the URI path name indicator "file://" was there, if so, I deleted it off my compare string. Then I normalized based on the path separator node path module will give me. This way it should work in either Linux or Windows environment.

Resources