Can a Fabric command be given as a list of args? - fabric

I'd like to specify a remote command in Fabric in the form
['mkdir', '-p', path]
where path is the path to some directory I want to create.
Most people seem to be doing things like this:
run('mkdir -p {}').format(path)
But that doesn't work in the general case where path may contain spaces, quotes, and any other character that will mess with the shell parsing.
Is there a reliable way to do this?

I had the same issue, the easiest solution for me was something like this:
print directory name to the file,
upload that file to the remote host
execute shell command/script to load line from the file

Related

Using regular expression in lftp to ignore some strings from file name

Get specific file with name like abc_yyyymmdd_hhmmss.csv from directory using mget.
Example files in a folder:
abc_20221202_145911.csv
abc_20221202_145921.csv
abc_20221202_145941.csv
abc_20181202_145941.csv
But, I want to ignore hhmmss part. I want to get all files with abc_20221202_*.csv
How to include * in mget.
My code below:
File=abc_
Date=20221202
Filename=$File$Date"_*".csv
// Assume I have sftp connection established and I am in directory //where files with above naming convention are present. As I can //download the file when hardcoding exact file name during testing
conn=`lftp $protocol://$user:$password#$sftp_server -p $port <<EOF>/error.log
cd $path
mget $Filename
EOF`
The script is able to find the file but not able to retrieve it from the server.
But, if I remove * and provide the entire file name abc_20221202_145941.csv it will download the file. Why is * causing issue in retrieving the file
Assuming mget actually accepts regex:
Currently your regexp is looking for files that match abc_20221202_(underscore any number of times).csv
Just add a . before the * so it matches any character after the underscore any number of times before the .csv
Like so:
Filename=$File$Date"_.*".csv
If mget doesn't actually support regex, just use wget instead:
wget -r -np -nH -A "abc_20221202_.*\.csv" --ftp-user=user --ftp-password=psd ftp://ip/*
I think the backtick symbol was causing the problem when using *. Once I removed the ` (backtick) and used below command, it worked fine.
lftp -p $port $protocol://$user:$password#$sftp_server <<EOF>/error.log
cd $path
lcd $targetPath
mget $Filename
EOF
You probably missed an underscore between File and Date. A good way to debug such problems is to enable debug (“debug” command) and command logging (set cmd:trace true)

Getting Bad configuration option: \377\376h

I am setting my systems for codecommit. but getting following error
I followed the below link :
https://docs.aws.amazon.com/codecommit/latest/userguide/setting-up-ssh-windows.html
/c/Users/Prasanna/.ssh/config: line 1: Bad configuration option: \377\376h
/c/Users/Prasanna/.ssh/config: terminating, 1 bad configuration options
here is the config file
Host git-codecommit.*.amazonaws.com
User ********
IdentityFile ~/.ssh/codecommit_rsa
Am I missing anything to configure ?
You probably have some illegal characters in the config file. I had this problem while creating a config file on Windows. Unfortunately, simply opening the file in a Windows text editor may not show the illegal characters.
I was able to find this problem by running cat filename from a Bash prompt in Windows (git bash) and was able to fix it by running dos2unix filename in git bash. The same may work for you as well.
Just had the same issue. Open the file with Notepad++. On the bottom right it tells you the encoding the file is in. It has to be UTF-8 without BOM. You can fix that via selecting a new encoding at the top and saving the file.
This happened to me today, and I just recreated the config file and put my configs there, it works.

urllib.urlretrieve creates file on disk

I do this:
thing = urllib.urlretrieve(url, "somefile.jpg")
It works, it gets the file, but it actually creates a file on the file system in the cwd. I write the file a little later to an appropriate path, but I don't want the file in the cwd at that time. What can I do?
Windows 10
If you want the file to go in a different directory, simply specify a path to where you want the file to go.
If you don't want the file to still be in the cwd after copying it, then delete the copy you don't want.
If you don't want to create a file at all, use something like urlopen.
There's a couple solutions 1. Change the cwd or 2. specify an alternate directory (as suggested by Scott Hunter), to do these:
1. Change your cwd (current working directory):
os.chdir("/Users/Desktop/SpecificDirectory/")
-now try your script.
OR
2. Specify an alternate directory in the download urllib command:
`thing = urllib.urlretrieve(url,"/Users/Desktop/SpecificDirectory/somefile.jpg")`

Using regex in FTP for filenames for downloading files

Is it possible to use regex for matching file names in FTP to get files from server ?
I need to do FTP to server and need to download the files whose file names are ending with the same value. In my case, it is 14_04_25_144238.
I am not sure if it is doable. But, just out of curiosity, asking this.
Can we use regex like .*14_04_25_144238 in the ftp get command ?
Thanks in advance.
Dinesh S
You want the mget command.
From the Unix man page
mget remote-files
Expand the remote-files on the remote machine and
do a get for each file name thus produced. See glob for details on
the filename expansion. Resulting file names will then be
processed according to case, ntrans, and nmap settings. Files are
transferred into the local working directory, which can be changed
with 'lcd directory'; new local directories can be created with '!
mkdir directory'.
If you want to turn of the prompting of each file, then you also need this:
prompt
Toggle interactive prompting. Interactive prompting occurs
during multiple file transfers to allow the user to selectively retrieve or store files. If prompting is turned off (default is on), any mget or mput will transfer all files, and any
mdelete will delete all files
.

Using tree Command in C++ on C://

I want to use the Windows CMD tree command in my C++ console application. My code:
system("cd c:/");
system("tree");
The problem is that the command tree will execute on the folder path where the program is running and not on C://. Is there a way to fix this?
Why not :
system("tree c:\");
?
TREE [drive:][path] [/F] [/A]
/F Display the names of the files in each folder.
/A Use ASCII instead of extended characters.
You can use SetCurrentDirectory from windows.h. This page has a demonstration:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363806%28v=vs.85%29.aspx
Your problem is that the system("cd c:/") is executed in a shell, and then the shell exits. [It's also wrong, because you use the wrong kind of slash, it should be "cd c:\\" - the double backslash is needed to make one backslash in the output, assuming we're talking about a Windows system].
There are a couple of different ways to do this:
Use chdir() (or SetCurrentDirectory) function call to change the main processes current working directory, and then call system("..."). This is the simplest solution.
Generate all your commands into a batch file, then pass the batch file to system.
Open a command shell with something like _popen() and pass commands into the pipe that you get from that.
Manually create pipes and connect them to the standard in and standard out of a process that runs the command prompt.
Just for programs in Windows, include "windows.h", then
SetCurrentDirectory("c:/");
system("pwd");
While I'm still curious why would you want to do this, you can try to run all commands in one system() call:
system("cd c: && c: && tree");
Second c: is needed to change drive letter, in case if you're not currently on drive c: (because cd doesn't do it).