PGP Scripting with wildcards - gnupg

I am just learning PGP as this has become a critical process at my job. I am currently manually encrypting/decrypting files with PGP and I would like to script this out but I am not seeing too much documentation on that type of process.
I got the command line to encrypt the files but it puts the files with a .gpg and I need it to be .pgp.
Here is what I have tried so far.
gpg -e -v -r name --output c:\temp\test*.txt.pgp c:\temp\test*.txt
This fails, but if I do gpg -e -v -r name --output c:\temp\test\test.txt.pgp c:\temp\test\test.txt, it works as designed.
The issue here is that I will have multiple files with different names but the same file extension so knowing the exact name is not going to happen until that file is created. As we are building out automation these files can range in the hundreds in the coming weeks. I am using Kleopatra and set the settings on there to pgp instead of gpg but it appears that setting does not apply to the command line.
Can anyone provide any suggestions? This is driving me nuts!

Nevermind, I figured it out. I am not sure why I didn't think of it sooner, perhaps I was focused in the gpg commandline. Anyhow, this can be done with PowerShell.
$files = Get-ChildItem "C:\temp\test"
ForEach($file in $files)
{
gpg -e -v -r great-west --output c:\temp\test\$file.pgp c:\temp\test\$file
}

Related

Bash Mutt email with attachment error Can't stat : No such file or directory

So i,ve read some other posts and tried out the answers, but i still keep running into this problem so i wanted to post a question here and see if anyone else had any other ideas. Keeping in mind i am pretty new to bash so i am iffy on whats available currently for what i,m looking for.
I am trying to automate a process that creates a file then sends it to me. All the above is fine, until i try to automatically email myself the file.
I have this line of code for it
echo "report" | mutt -- "$USEREMAIL" -s "report" -a "my_scripts/cid_reports/trb345432.csv"
When it tries to run this command it throws an error like
Can't stat my_scripts/cid_reports/trb345432.csv: No such file or directory
my_scripts/cid_reports/trb345432.csv: unable to attach file.
Any ideas on how i can fix this? I thought mutt was good to handle this, I am going to play with the mail feature and see if i can get any success with that.
The system looks to be running
Mutt 1.4.2.2i (2006-07-14)
Copyright (C) 1996-2002 Michael R. Elkins and others.
Mutt comes with ABSOLUTELY NO WARRANTY; for details type `mutt -v
mutt -h
-a <file> [...] -- attach file(s) to the message
The list of files must be terminated with the "--" sequence, so,
echo "hello world" | mutt -s "title" -a /home/test.txt -- ***#**.com
You need to add "--".
SIMPLE ANSWER
export EMAIL="sender_mail#example.com" | mutt -e "set content_type=text/html" -s "Test Mail" -c "cc_recipient#example.com" -a /tmp/attachment.txt -- "recipient_mail#example.com" < /tmp/message.html
Please use the following syntax while attaching files with 'mutt'.
# mutt -a file -- user#domain
For example;
# mutt -a /tmp/test.txt -- john#abc.com
Relevant snippets from the man page of mutt is given below.
Raw
-a file [...]
Attach a file to your message using MIME. When attaching single or multiple files, separating filenames and recipient addresses with "--" is mandatory,
e.g. mutt -a image.jpg -- addr1 or mutt -a img.jpg *.png -- addr1 addr2. The -a option must be placed at the end of command line options.
Ref: https://access.redhat.com/solutions/43567
The no such file or directory in general means that the file cannot be found. Because you're using a relative path, it might be that you are in a different directory?
If you type cat my_scripts/cid_reports/trb345432.csv from the same directory as you run the script, is the file there?
Or otherwise. if you use an absolute path (usually '/home/'uid'/my_scripts/cid_reports/trb345432.csv` but your path may be duifferent), does the script find the file?
(or should this have been a comment in stead of an answer, eventhough it tries to guide through finding the error?)
From your comments to Ljm Dullaart's answer, you're actually storing the file path in a variable, then using it something like this:
FILE="~/my_scripts/cid_reports/file.csv"
echo "report" | mutt -- "$USEREMAIL" -s "report" -a "$FILE"
If that's what you're doing, the problem is that ~ is in double-quotes, and therefore doesn't get expanded to the actual path to your home directory. Thus, it's looking for a sub directory literally named "~" under the current working directory, and not finding it. In order for ~ to be expanded to the path to your home directory, leave it and the following / unquoted, like this:
file=~/"my_scripts/cid_reports/file.csv"
echo "report" | mutt -- "$useremail" -s "report" -a "$file"
Note that I've also switched the variable names to lowercase. This is a best practice to avoid conflicts with the various environment variables that have special meanings; the special ones are all caps, so if you use lower- or mixed-case, you'll never accidentally trip over them.

How to create an encrypted data bag in Chef

I know that this topic has answers here, but I've got some problems and I would like to start from scratch.
First step is to create a key file:
openssl rand -base64 512 | tr -d '\r\n' > encrypted_data_bag_secret
but how to run this command on Windows? The tr command is not recognised.
I generated an openssl key and copied it to a txt file, then I was doing step by step like in Chef Docs but it doesn't work - a data bag isn't encrypted. I think I have to run this command above, but I don't know why to do this on windows
An equivalent in pure Ruby would be:
C:\chef\embedded\bin\ruby -e 'require "securerandom"; STDOUT.write(SecureRandom.base64(512))' > C:\chef\encrypted_data_bag_secret
Tweak the C:\chef path as appropriate, I don't have a Windows box handy to check the current default paths.

Deleting files from a list using shell

I am a beginner with Applescript & Shell and am writing a script that at a certain point requires me to delete files that are listed within a .txt file. I have searched extensively on stackoverflow and was able to come up with the following command that I am running from within my Applescript...
do shell script "while read name; do
rm -r \"$name"\
done < ~Documents/Script\\ Test/filelist.txt"
It seems to recognize and read the file, but I get an error that says this and I cannot figure out why:
error "rm: ~/Documents/Script Test/filetodelete.rtf: No such file or directory" number 1
That said, I can navigate to that exact directory and verify that a file by that name with that extension does indeed exist. Can someone help shed some light on why this error might be occurring?
You have a typo. The path to the file is most probably ~/Documents, not ~Documents (which in Bash would be the home directory of a user whose account name is Documents).
If your shell is not Bash, it might not even support ~ for $HOME.
In the data file, you also cannot use ~ to refer to your home directory. You could augment the loop with a simple substitution to support this:
while read -r file; do
case $file in '~'*) file=$HOME${file#\~};; esac
rm -r "$file"
done < ~/"Documents/Script Test/filelist.txt"
Notice also the use of read -r to avoid some pesky problems with the legacy default behavior of read.

download a file with curl, keep original filename and add timestamp or so one

i've started playing around with curl a few days ago. For any reason i couldn't figure out how to archive the following.
I would like to get the original filename with the output option
-O -J
AND put there some kind of variable, like time stamp, source path or whatever. This would avoid the file overwriting issue and also make it easier for further work with it.
Here are a few specs about my setup
Win7 x64
curl 7.37.0
Admin user
just commandline no PHP or script or so one
no scripting solutions please, need tihs command in a single line for Selenium automation
C:>curl --retry 1 --cert c:\certificate.cer --URL https://blabla.com/pdf-file --user username:password --cookie-jar cookie.txt -v -O -J
I've played around with various things i found online like
-o %(file %H:%s)
-O -J/%date%
-o $(%H) bla#1.pdf
but it always just print out the file as it is named link "%(file.pdf" or some other shitty names. I guess this is something pointing to escaping and quoting issues but cant find it right now.
No scripting solutions please, I need tihs command in a single line for Selenium automation.
Prefered output
originalfilename_date_time_source.pdf
Let me know if you get a solution for this.

-pubkey option in hdiutil

I have written a shell script that creates an encrypted sparsebundle disk image. I want to add a pubkey to the sparsebundle, but the option -pubkey (which is documented by Apple) is getting rejected when I add it to the shell script, or even when I just run a single line command in bash.
Am I missing something about this option? I read the documentation, but I still can't get it to work.
Currently the "create" line of the shell script looks like this (the script works fine as is, but when I add -pubkey it gives me a "usage" return):
echo $password | hdiutil create -size $space -encryption -type SPARSEBUNDLE -fs HFS+J $name
This is often cited as 'never working' or broken, but it can work & has done so for quite some time.
See below for an example, in short you just need to use Keychain Access to create a certificate for the public/private keys.
http://design.caltech.edu/erik/Misc/encrypted_virtual_disk.html

Resources