Tech C**P
14 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
An easy way to encrypt and decrypt large files using OpenSSL and Linux:

Generate PEM public private key using openssl:

openssl req -x509 -nodes -newkey rsa:2048 -keyout private-key.pem -out public-key.pem
Encrypt file using public key PEM file:

openssl smime -encrypt -binary -aes-256-cbc -in large_file.img -out large_file.img.dat -outform DER public-key.pem


We can generate hash using md5sum for both files so we can compare them once we decrypt our file:

md5sum large_file.img*
#cd573cfaace07e7949bc0c46028904ff large_file.img
#c4d8f1e868d1176d8aa5363b0bdf8e7c large_file.img.dat


Decrypt large file using OpenSSL:

openssl smime -decrypt -in large_file.img.dat -binary -inform DEM -inkey private-key.pem -out decrypted_large_file.img


Check md5sum output:

md5sum *large_file.img*
#cd573cfaace07e7949bc0c46028904ff decrypted_large_file.img
#cd573cfaace07e7949bc0c46028904ff large_file.img
#c4d8f1e868d1176d8aa5363b0bdf8e7c large_file.img.dat

#linux #openssl #pem #encryption #decryption #x509 #public_key #private_key
In cryptography world there are different ways that you can encrypt data (files). We explain both here a bit:

1- In one of these methods you encrypt a file with a secret key. The other party has the same secret key in order to be able to decrypt data. This way of encryption and decryption is called Symmetric.

2- The other method is Asymmetric encryption. Asymmetric encryption involves the use of a public key and a private key. As can be guessed from the names, only the private key needs to be kept private. The public key is made available for anybody that wants to encrypt data to send to the owner of the public key. Using the usual names Alice and Bob as examples: If Bob wanted to send an encrypted file or email to Alice, Bob would encrypt the data using Alice's public key then send the data to Alice. Alice can then decrypt the data using her private key that only she has access to. Once Bob has encrypted the data, nobody but Alice can decrypt it. Note that a private key is usually protected by a passphrase, so even on her own Ubuntu box where the key resides, Alice will still need to enter a passphrase to "unlock" the private key. Bob will have his own public/private key pair so people can send encrypted data to him. So should Alice want to send encrypted data to Bob, she'd encrypt it using Bob's public key whilst Bob could then decrypt it using his own private key. The best asymmetric cryptographic algorithm supported by gpg is called RSA.


#cryptography #asymmetric #symmetric #rsa #gpg
If you have multiple statements and you want to time the whole statements in a bash script you can use time command, but you don't need to use inside of the script. If you have an script called my_long_tasks.sh you need to just:

$ time my_long_tasks.sh
real 0m20.894s
user 0m3.664s
sys 0m0.452s


That's it. In case you want to just time part of your statements in your bash script then you need to:

start=`date +%s`
stuff
end=`date +%s`

runtime=$((end-start))

#linux #bash #time
In bash script you can get count of given arguments to your script using $#. If you want to get a positional argument use $ and then number like $1. If you want to get the exit code of the previous command use $?.

#linux #bash #script
Sometimes you have to send a very large file over network to a remote destination, or want to copy a huge file to a partition which is mounted by NFS (Network File System). This issue will eat up all you network bandwidth until the copy process is finished, it can take an hour or more depending on the file size being copied.

This issue can be an ISSUE when it's a production server and your server is already over load, to copy a file you can use rsync command to limit your bandwidth and prevent network hogs:

rsync --bwlimit=1000 /var/www/html/ [email protected]:~/mysite


--bwlimit is in KB so the above example puts limit of 1000 Kilo Bytes on your copy command.

#rsync #bwlimit #NSF
There are times you run a command in cronjob in a specific interval. Let's say you run that command every hour.

If your command copies a huge file, or you are doing a heavy task that may take longer than 1 hour sometimes, then you need run-one
command in your arsenal.

run-one: run just one instance at a time of some command and unique set of arguments (useful for cronjobs, eg)


Sample run-one command:

run-one rsync -azP $HOME $[email protected]:/srv/backup

See more samples here:
- https://manpages.ubuntu.com/manpages/xenial/man1/run-one.1.html


#linux #cronjob #command #run_one #runone
tarfile is a python library to read and write gzip`/`bz2 compressed files.

How to read a gzip compressed tar archive and display some member information:

import tarfile
tar = tarfile.open("sample.tar.gz", "r:gz")
for tarinfo in tar:
print tarinfo.name, "is", tarinfo.size, "bytes in size and is",
if tarinfo.isreg():
print "a regular file."
elif tarinfo.isdir():
print "a directory."
else:
print "something else."
tar.close()


Create a compressed file:

with tarfile.open(dst, "w:gz") as tar:
print("Archiving " + src + " into " + dst)
tar.add(src, arcname = os.path.basename(src))

NOTE: the flag of w:gz opens the destination in write mode. Used to create a new tar file.

#python #tarfile #tar #bz2 #gzip
Linux in Docker: Wheezy: "ps: command not found"

Solution:

RUN apt-get update && apt-get install -y procps


#docker #linux #ps #command_not_found #procps
How to go to the previous working directory in terminal?

cd -

#linux #cd #last_working_directory