r_bash | Unsorted

Telegram-канал r_bash - r_bash

36

Credits: @r_channels & @reddit2telegram

Subscribe to a channel

r_bash

Any way to tell if script is ran via command line versus cron?

Inside of a bash script, is there a way to tell whether the script was ran via command line versus crontab?

I know that I can send a variable, like so:

# bash foo.sh bar

And then in the script, use:

if [ $1 -eq "bar" ]
then
# it was ran via command line
fi

but is that the best way?

The goal here would be to printf results to the screen if it's ran via command line, or email them if it's ran via crontab.

https://redd.it/1f93hp3
@r_bash

Читать полностью…

r_bash

Running via cronjob, any way to check the server load and try again later if it's too high?

I'm writing a script that I'll run via cronjob at around 1am. It'll take about 15 minutes to complete, so I only want to do it if the server load is low.

This is where I am:

attempt=0

# server load is less than 3 and there have been less than 5 attempts
if (( $(awk '{ print $1; }' < /proc/loadavg) < 3 && $attempt < 5))
then
# do stuff

else
# server load is over 3, try again in an hour
let attempt++
fi

The question is, how do I get it to stop and try again in an hour without tying up server resources?

My original solution: create an empty text file and touch it upon completion, then the beginning of the script would look at the lastmodified time and stop if the time is less than 24 hours. Then set 5 separate cronjobs, knowing that 4 of them should fail every time.

Is there a better way?

https://redd.it/1f90a2b
@r_bash

Читать полностью…

r_bash

single quote (apostrophe) in filename breaks command

I have a huge collection of karaoke (zip) files that I'm trying to clean up, I've found several corrupt zip files while randomly opening a few to make sure the files were named correctly. So I decided to do a little script to test the zips, return the lines with "FAILED" and delete them. This one-liner finds them just fine

find . -type f -name "*.zip" -exec bash -c 'zip -T "{}" | grep FAILED' \;

But theres the glaring error "sh: 1: Syntax error: Unterminated quoted string" every time grep matches one, so I can't get a clean output to use to send to rm. I've been digging around for a few days but haven't found a solution

https://redd.it/1f8fmv5
@r_bash

Читать полностью…

r_bash

This is official Google script

https://redd.it/1f89ord
@r_bash

Читать полностью…

r_bash

Quitting a Script without exiting the shell

I wrote a simple bash script that has a series of menus made with if statements. If a user selects an invalid option, I want the script to quit right away.

The problem is that exit kills the terminal this script is running in, & return doesn’t work since it’s not a “function or sources script.”

I guess I could put the whole script in a while loop just so I can use break in the if else statements, but is there a better way to do this?

What’s the proper way to quit a script? Thanks for your time!

UPDATE:
I’m a clown. I had only ever run exit directly from a terminal, & from a sourced script. I just assumed it always closed the terminal. My bad.

I really appreciate all the quick responses!

https://redd.it/1f85r2h
@r_bash

Читать полностью…

r_bash

0

https://redd.it/1f7nimj
@r_bash

Читать полностью…

r_bash

Which PubkeyAcceptedAlgorithm Should I Choose for SSHD, Now that "ssh-rsa" is Less Recommended?

Hi all

Since SSHD removed "ssh-rsa" from the Default List for PubkeyAcceptedAlgorithms,
I conclude that it's an old algorithm and SSHD is trying to push users to something newer and more secure.

So in man sshd_config,
we can see the following list of Algorithms that are now in the default list:

ssh-ed25519-cert-v01@openssh.com,
ecdsa-sha2-nistp256-cert-v01@openssh.com,
ecdsa-sha2-nistp384-cert-v01@openssh.com,
ecdsa-sha2-nistp521-cert-v01@openssh.com,
sk-ssh-ed25519-cert-v01@openssh.com,
sk-ecdsa-sha2-nistp256-cert-v01@openssh.com,
rsa-sha2-512-cert-v01@openssh.com,
rsa-sha2-256-cert-v01@openssh.com,
ssh-ed25519,
ecdsa-sha2-nistp256,
ecdsa-sha2-nistp384,
ecdsa-sha2-nistp521,
sk-ssh-ed25519@openssh.com,
sk-ecdsa-sha2-nistp256@openssh.com,
rsa-sha2-512,
rsa-sha2-256

Which one should I choose?

And why some of them resemble the format of an Email Address?

Thank you

https://redd.it/1f7gb20
@r_bash

Читать полностью…

r_bash

Is It Possible to Make SSHD Generate a New sshd_config File?

Hi all

I have made some changes to my `/etc/ssh/sshd_config` file,
and I would like to compare them to the original untouched file.

Is it possible to ask SSHD to somehow generate a new sshd_config file?
Like what I had before changing any settings..

Thank you

https://redd.it/1f79iiz
@r_bash

Читать полностью…

r_bash

Escaping characters is grep

I am trying to grep some text between two values but I can't escape the characters.

viewME('jkhkjhkjhkjhudydsdvvytvd')

I use this command but it keeps giving me a ( error. I tested the regex in a tester and it works without issue yet when I try grep I get errors on Arch linux. What am I missing?

grep -E '(?<=viewME\\(\\').*(?=\\'\\))'

https://redd.it/1f6ulzc
@r_bash

Читать полностью…

r_bash

sed not working within for loop

I'm trying to do this loop

for ALLSERVER in "$HOME/Games/Servers/Minecraft/"
do

echo $( sed '53!d' "$ALLSERVER/server-properties" )

done

but `sed` is interpreting the wildcard character incorrectly, in a way that `echo` doesn't, producing the following error:

sed: can't read /home/user/Games/Servers/Minecraft/
/server-properties: No such file or directory

How can I make it properly substitute the wildcard for the directory in the current iteration?

https://redd.it/1f63mbt
@r_bash

Читать полностью…

r_bash

"$pass" | my_app
unset pass


# The script itself can store the key though it doesn't mix well with
# version control and seperation of concerns.
printf '%s' 'my_api_key' | my_app


# Two examples of using process substitution `<()` in place of a password
# file as it expands to the path of a private file descriptor.
my_app --pass-file <( read -sr -p 'enter password: ' pass; printf '%s' "$pass" )

my_app --pass-file <( printf '%s' 'my_api_key' )

# Summary
---

- Secrets should be delivered as a path to a secure file or written over an anonymous pipe.
- Secrets can be stored in local variables though it's always better to reduce attack surface and opportunity for mistakes if you have the option.
- Secrets should never be present in exported variables or parameters of commands that execute as a new process.

https://redd.it/1f5sern
@r_bash

Читать полностью…

r_bash

using qpdfview: recently I get this message before showme the pdf file

Hi, recently I get the message saying me Icon Theme "abc...." not found before qpdfview showme the pdf

screenshot: https://imgbox.com/ReZm0aBp

I don't know why and the pdf is simply, or text or and img into the pdf

I don't use templates, models of pages. I just use LO for create pdf files.

recently I am starting to use convert for get pdf files.

How can delete these messages?

https://redd.it/1f5b6ur
@r_bash

Читать полностью…

r_bash

Can you help me understand which.debianutils

I'm having a problem where `which` doesn't find java that is first in my PATH. That led to me looking at `/usr/bin/which.debianutils` on ubuntu 24.04. I don't understand what is going on here:

case $PATH in
(*[!:]:) PATH="$PATH:" ;;
esac

And this:

for PROGRAM in "$@"; do
RET=1
IFS_SAVE="$IFS"
IFS=:
case $PROGRAM in
*/*)
if [ -f "$PROGRAM" ] && [ -x "$PROGRAM" ]; then
puts "$PROGRAM"
RET=0
fi
;;
*)
for ELEMENT in $PATH; do
if [ -z "$ELEMENT" ]; then
ELEMENT=.
fi
if [ -f "$ELEMENT/$PROGRAM" ] && [ -x "$ELEMENT/$PROGRAM" ]; then
puts "$ELEMENT/$PROGRAM"
RET=0
[ "$ALLMATCHES" -eq 1 ] || break
fi
done
;;
esac
IFS="$IFS_SAVE"
if [ "$RET" -ne 0 ]; then
ALLRET=1
fi
done

`PROGRAM` is "java" and the script starts with:

set -ef

What does `*` mean with globbing turned off? What is the for loop doing?

`puts` is:

printf '%s\n' "$*"

https://redd.it/1f4siaf
@r_bash

Читать полностью…

r_bash

While loop reading inotifywait--run the loop at least once

I have the following script to continually detects changes to a file then does something. Works well, but it only does something after the first change to a file and I'm trying to make it so that it does something first (i.e. runs the loop one time then detects changes before re-running the loop):

# Kill script on C-c but restarts the inner loop on file closeexec. `set
# -m` (monitor mode) puts the async command in its own pgid so -pid can be used
# to kill all of it, but it also ignores sigint, so added a trap back for it
# once pid is set. on't need to set trap in the loop, so can do it
# outside and check if pid is set
set -m

init=1
while IFS='' read -rd '' file; do
if [[ "$pid" ]]; then
kill -- -"$pid" &>/dev/null && printf "\n%s" "*** Paused - no valid uncomment URLs"
wait "$pid"
fi

# does something
while read -r line; do

url="${line%% *}"

yt-dlp --output "$template" -P "$dir" -- "$url" || break
# Filter out comments and empty lines
done < <(awk 'NF && !index($0, "#") == 1' < "$file") &

pid=$!
trap 'kill -- -"$pid"' INT
done < <(
if [[ "$init" == 1 ]]; then
echo "$list"
init=0
else
# Watching the directory of the file instead of the file itself handles
# whatever strategies (settings) vim uses to save file and is a more
# editor-agnostic approach. Filters only for events for this file See:
#
https://vi.stackexchange.com/a/25040
inotifywait -qme close
write --format %w%f%0 --no-newline "$(dirname "$list")" --include ".$list."
fi
)

For example, I added an init state. inotifywait returns filename for outer while loop to process, I echo the same filename but the script exists. How can I ensure the inner while loop runs once? Currently, the outer loop doesn't even run once. I guess it has to do with echo exiting immediately while inotifywait waits?

Any ideas much appreciated.

https://redd.it/1f4ob4l
@r_bash

Читать полностью…

r_bash

built-in printf giving crazy results

In a shell script I’m using bc to calculate a floating point value, assigning it to a variable then using the built-in printf function in bash – version 5.2.32(1)-release from Debian testing – and getting crazy results. Here’s a simplified example:

N1=37; N2=29; D=$(echo "scale=2; $N1 / $N2" | bc); printf "%2.2f\n" $D
0.00

Sometimes instead of 0.00 i get a number with so many digits it scrolls past what my terminal can display on one page.

If instead use the external printf command, I get the expected results:

N1=37; N2=29; D=$(echo "scale=2; $N1 / $N2" | bc); /usr/bin/printf "%2.2f\n" $D
1.27

Any ideas what’s going on? Maybe a bug in this version of bash?

https://redd.it/1f4e1io
@r_bash

Читать полностью…

r_bash

Sending mail through bash, is mailx still the right option?

I'm writing a script that will be run via cronjob late at night, and I'd like for it to email the results to me.

When I use man mail, the result is mailx. I can't find anyone talking about mailx in the last decade, though! Is this still the best way to send mail through bash, or has it been replaced with someone else?

If mailx is still right, does the [-r from_address] need to be a valid account on the server? I don't see anything about it being validated, so it seems like it could be anything :-O Ideally I would use root@myserver.com, which is the address when I get other server-related emails, but I'm not sure that I have a username/password for it.

This is the man for mailx:

NAME
mailx - send and receive Internet mail

SYNOPSIS
mailx -BDdEFintv~ -s subject -a attachment -c cc-addr -b bcc-
addr -r from-addr -h hops -A account -S vari-
able[=value] to-addr . . .
mailx -BDdeEHiInNRv~ -T name -A account -S variable[=value] -f
name
mailx -BDdeEinNRv~ -A account -S variable[=value] -u user



https://redd.it/1f8zn0x
@r_bash

Читать полностью…

r_bash

SSID's not showing up when trying to echo them.

Hi all,

I am trying to write a script that will make connecting to wifi on my openbsd laptop easier. I have the script mostly complete; however, I get the error:

./wifiscanner.sh: line 7: echo "SSID: $ssid": command not found


here is my script so far:

#! /usr/local/bin/bash

doas ifconfig iwn0 up

for i in $(ifconfig iwn0 scan |

sed '/iwn0/,/network/d' |

sed -e 's/.*nwid\(.*\)chan.* /\1/' |

sed '/""/d' |sed '/0x0/d' | tr -d '""'|

head -n 10| while read ssid;

do
ssid_var="${ssid}"
'echo "SSID: $ssid"' :**

done) ;


do

read -p "Select your SSID: (e.g, ssid1) " ssid_name

done

read -p "Enter your WPA key: " WPAkey

echo "ifconfig iwn0 nwid ${ssid_name} wpakey ${WPAkey}"


without the apostrophes nothing shows up. How do I go about fixing this?



Thank you

https://redd.it/1f8kh5h
@r_bash

Читать полностью…

r_bash

Help parsing a text file

I'm writing a script that needs to parse a text file and call another script depending on what it finds.

This is an example of the text file data:

555555:
- x.x.x.x/32
- x.x.x.x/24
- x.x.x.x/32
555556:
555557:
555558:
- x.x.x.x/32
- x.x.x.x/24
555559:
555560:

From the above file, think of each number as a VM. I need to run one script on each VM without trailing IPs, and the same script plus a different script on the VMs with trailing IPs.

Grabbing the VMs without IPs is easy enough, of course. I'm having a hard time determining how I'll grab each VM with IPs and all their IPs (since the number of IPs vary wildly). I thought I'd bounce this off the interwebz and see if anyone could give me an idea or three?

Maybe a while loop for when I find IPs but even though I'm at a loss thinking how I'll grab only those IPs with the corresponding VM.

https://redd.it/1f8bgpi
@r_bash

Читать полностью…

r_bash

AutoPilot - it's siimple | Automate the setup of a new system with ease

# AutoPilot - It's simple.

[**AutoPilot**](https://github.com/Noam-Alum/AutoPilot/) is a free-to-use, [well documented](https://docs.alum.sh/AutoPilot/Introduction.html) bash script (for both **Debian** and **RHEL** related operating systems) written by [me](https://www.linkedin.com/in/noam-alum/) meant to automate the process of setting up a new system.

It uses [YAML](https://en.wikipedia.org/wiki/YAML) for its configuration file, so it is very easy to set up, and you can create numerous configuration files for different occasions. (I like to call them *"Profiles"* 🙃)

https://preview.redd.it/hc8790ev8nmd1.png?width=269&amp;format=png&amp;auto=webp&amp;s=480f775b873bad386577d30c524fcfefc7b6ca64

**Current available directives (**[v1.0.0](https://github.com/Noam-Alum/AutoPilot/releases/tag/v1.0.0)**):**

* [SELinux](https://docs.alum.sh/AutoPilot/directives/SELinux.html)
* [Users](https://docs.alum.sh/AutoPilot/directives/Users.html)
* [Run\_Lines](https://docs.alum.sh/AutoPilot/directives/Run_Lines.html)
* [Installed\_packages](https://docs.alum.sh/AutoPilot/directives/Installed_packages.html)
* [Plugins](https://docs.alum.sh/AutoPilot/directives/Plugins.html)
* [Network\_Configuration](https://docs.alum.sh/AutoPilot/directives/Network_Configuration.html)
* [Environment\_configuration](https://docs.alum.sh/AutoPilot/directives/Environment_configuration.html)
* [Cronjobs](https://docs.alum.sh/AutoPilot/directives/Cronjobs.html)
* [Repo](https://docs.alum.sh/AutoPilot/directives/Repo.html)
* [Time](https://docs.alum.sh/AutoPilot/directives/Time.html)



**Use cases:**

|**Use Case**|**Description**|
|:-|:-|
|**Educational Institutions**|Educational institutions can leverage AutoPilot to quickly deploy standardized environments for students and faculty.|
|**Development Environments**|Developers can use New System to configure their development machines with the necessary programming languages, libraries, frameworks, and tools.|
|**Personal Use**|Individuals who frequently set up new machines or reinstall their operating systems can benefit from AutoPilot by automating the setup process.|
|**Testing and QA**|AutoPilot automates test environment setup, providing quality assurance teams and testers with consistent, repeatable configurations and necessary tools.|
|**Temporary Setups**|For temporary or event-based setups like trade shows or conferences, AutoPilot quickly prepares machines with the required software and settings, making deployment and management easier for short periods.|
|**Rescue and Recovery**|When a system needs recovery or rebuilding after a failure, AutoPilot automates software reinstallation and settings reconfiguration, reducing the time to restore it to its original state.|
|**Company Deployment**|A company can use AutoPilot to quickly configure new machines, ensuring consistent software and settings. This includes installing productivity tools, setting up configurations, and applying security policies.|
|**OS Migration**|When switching operating systems, AutoPilot automates setup of applications, configurations, and settings, ensuring a smooth transition and minimizing manual reinstallation and reconfiguration.|
|**System Formatting**|If you need to format and reinstall your operating system, AutoPilot handles post-installation setup. It automates software installation, configuration, and personalization, helping you get back to work faster.|



>I hope someone could find this helpful 😁, if you want to request a new feature you can do that [here](https://github.com/Noam-Alum/AutoPilot/issues/new?assignees=Noam-Alum&amp;labels=feature+request&amp;projects=&amp;template=feature-request.md&amp;title=Feature+request+%7C+%5Bfeature+request+short+description%5D).



**Links:**

* [GitHub](https://github.com/Noam-Alum/AutoPilot/)
* [Documentation](https://docs.alum.sh/AutoPilot/Introduction.html)
* [Contribute](https://github.com/Noam-Alum/AutoPilot/blob/main/CONTRIBUTING.md)

https://redd.it/1f88k58
@r_bash

Читать полностью…

r_bash

Read Upwork request data

For URL

https://www.upwork.com/nx/search/jobs/?nbs=1&amp;page=5&amp;per\_page=50

In my Firefox Web Developer Tools the request to https://www.upwork.com/api/graphql/v1 is made and the type is 'application/x-thrift+json'.

The request headers include:

Accept: */*

Accept-Language: en-US,en;q=0.5

Accept-Encoding: gzip, deflate, br

Content-Type: application/json

Also I see the JSON response in the Web Developer tab.

But when I copy the request as curl and paste it to command line it returns gibberish which can be either a binary gzip archive or some thrift data (I have no idea what thrift is). piping to gunzip and using curl --compressed option gives an error saying it's not an archive data. How can I read that response and see JSON ?

https://redd.it/1f7vo0e
@r_bash

Читать полностью…

r_bash

[Critique] Aria2 moving downloads script

I’ve developed a script that moves completed downloads from Aria2. I’m seeking feedback on potential improvements. You can review the script here: [GitHub](https://github.com/macg4dave/aria_move).

I’m considering replacing the mv command with rsync and refining the variable management. Are there any other enhancements or best practices I should consider?

#!/bin/sh

# Variables for paths (no trailing slashes)
DOWNLOAD="/mnt/World/incoming"
COMPLETE="/mnt/World/completed"
LOG_FILE="/mnt/World/mvcompleted.log"
TASK_ID=$1
NUM_FILES=$2
SOURCE_FILE=$3
LOG_LEVEL=1 # 1=NORMAL, 2=NORMAL+INFO, 3=NORMAL+INFO+ERROR, 4=NORMAL+DEBUG+INFO+ERROR

# Function to log messages based on log level
log() {
local level=$1
local message=$2
local datetime=$(date '+%Y-%m-%d %H:%M:%S')

case $level in
NORMAL)
echo "$datetime - NORMAL: $message" >> "$LOG_FILE"
;;
ERROR)
[ $LOG_LEVEL -ge 2 ] && echo "$datetime - ERROR: $message" >> "$LOG_FILE"
;;
INFO)
[ $LOG_LEVEL -ge 3 ] && echo "$datetime - INFO: $message" >> "$LOG_FILE"
;;
DEBUG)
[ $LOG_LEVEL -ge 4 ] && echo "$datetime - DEBUG: $message" >> "$LOG_FILE"
;;
esac
}

# Function to find a unique name if there's a conflict
find_unique_name() {
local base=$(basename "$1")
local dir=$(dirname "$1")
local count=0
local new_base=$base

log DEBUG "Finding unique name for $1"

while [ -e "$dir/$new_base" ]; do
count=$((count + 1))
new_base="${base%.*}"_"$count.${base##*.}"
done

log DEBUG "Unique name found: $dir/$new_base"
echo "$dir/$new_base"
}

# Function to move files and handle errors
move_file() {
local src=$1
local dst_dir=$2

log DEBUG "Attempting to move file $src to directory $dst_dir"

if [ ! -d "$dst_dir" ]; then
mkdir -p "$dst_dir" || { log ERROR "Failed to create directory $dst_dir."; exit 1; }
fi

local dst=$(find_unique_name "$dst_dir/$(basename "$src")")
mv --backup=t "$src" "$dst" >> "$LOG_FILE" 2>&1 || { log ERROR "Failed to move $src to $dst."; exit 1; }

log INFO "Moved $src to $dst."
}

# Function to move all files within a directory
move_directory() {
local src_dir=$1
local dst_dir=$2

log DEBUG "Attempting to move directory $src_dir to $dst_dir"

mkdir -p "$dst_dir" || { log ERROR "Failed to create directory $dst_dir."; exit 1; }

mv --backup=t "$src_dir" "$dst_dir" >> "$LOG_FILE" 2>&1 || { log ERROR "Failed to move $src_dir to $dst_dir."; exit 1; }

log INFO "Moved directory $src_dir to $dst_dir."
}

# Main script starts here
log INFO "Task ID: $TASK_ID Completed."
log DEBUG "SOURCE_FILE is $SOURCE_FILE"

if [ "$NUM_FILES" -eq 0 ]; then
log INFO "No file to move for Task ID $TASK_ID."
exit 0
fi

# Determine the source and destination directories
SOURCE_DIR=$(dirname "$SOURCE_FILE")
DESTINATION_DIR=$(echo "$SOURCE_DIR" | sed "s,$DOWNLOAD,$COMPLETE,")

log DEBUG "SOURCE_DIR is $SOURCE_DIR"
log DEBUG "DESTINATION_DIR is $DESTINATION_DIR"

# Check if SOURCE_FILE is part of a directory and move the entire directory
if [ "$(basename "$SOURCE_DIR")" != "$(basename "$DOWNLOAD")" ]; then
log DEBUG "Moving entire directory as the source file is within a subdirectory"
move_directory "$SOURCE_DIR" "$COMPLETE"
else
log DEBUG "Moving a single file $SOURCE_FILE"
move_file "$SOURCE_FILE" "$DESTINATION_DIR"
fi

log NORMAL "Task ID $TASK_ID completed successfully."
log NORMAL "Moving $SOURCE_FILE completed successfully."
exit

Читать полностью…

r_bash

Is It Possible to Ask "man" to Show Only a Specific Setting?

Hi all


If you run man man,
you see that man has several options to filter the output,
for example:

> man man options [section page ...] ...

Now assume this:

You want to run man sshd_config,
and thens see only the paragraph for the PubkeyAcceptedKeyTypes setting.

Is it possible to point the command to a specific setting/paragraph?

Thank you

https://redd.it/1f7ay1b
@r_bash

Читать полностью…

r_bash

Script doesn't terminate after simple background process exits

Script:

#!/usr/bin/env bash

# Control Tasmota plug via MQTT
status() {
mosquittosub -h addr -u user -P 1 -t 'stat/plugc/RESULT' -C 1 | jq -r .Timers &
}

status

mosquittopub -h addr -u user -P 1 -t cmnd/plugc/timers -m "OFF"

I run mosquitto_sub in the background so it can listen and return the result of mosquitto_pub, after which it exits. I get that result, but the script appears to "hang" (shell prompt doesn't give me back the cursor) even though the mosquitto_sub process ends (it no longer has a pid). I need to press Enter on the shell and it returns with success code 0.

If I run those commands on the interactive shell directly, it behaves as expected--I get back my command line cursor.

Any ideas?

https://redd.it/1f6zypv
@r_bash

Читать полностью…

r_bash

[Seeking advice + critique] I wrote a collection of scripts on creating and using LUKS volume on Linux natively rather than with third party software like veracrypt

Scripts Link: https://gitlab.com/cy_narrator/lukshelper

Complementary article: https://utsavpoudyal.com.np/posts/Create-Encrypted-File-Container-in-Linux/

So I wanted a way to deal with sensitive files on Linux without necessarily having to encrypt the entire disk of a flash drive. Basically, what I want is a way to create an encrypted file container on Linux, sort of what Veracrypt allows you to do but without any third party software, this ensures that the volume is available even when that third party software is unavailable.

The most concern I have is in my luksCreate.sh script. That script takes in a password from the user and feeds into cryptsetup. This is done for convinience, otherwise, the user has to enter the same password three times, first two times for when cryptsetup luksFormat was performed on the volume, last one when the script opens the volume to format it with a filesystem. I also had to do some calculations to calculate appropriate `count` for the given block size and volume size.

Someone mentioned that it is possible for someone to terminate the script early and read the $password1 and $password2, I tried and it is not the case because they are bash variables, not environment variables. But regardless, the passwords are overwritten with empty string after use.

Some defaults were assumed when creating the volume which is explained in my article in **Notes and Disclaimer** section.

I dont think the password handling concern is present in other scripts as other scripts just call on cryptsetup and make cryptsetup prompt for the password itself. But regardless, please let me know if anything else also can be improved.

I am still learning bash, I have hardly written bash before, those too were written couple of years ago and I have totally forgotten how they were written.

Please also let me know ideas on how to make these scripts better.

https://redd.it/1f6ie7b
@r_bash

Читать полностью…

r_bash

RunBash : Seamlessly Run Bash Scripts and Linux Binaries on Windows from Explorer, Cmd, and PowerShell

Hey everyone! 👋

If you're a developer or a power user who enjoys the flexibility of Linux but often works in a Windows environment, this might be the tool you've been looking for.

# What is RunBash?

RunBash is a handy utility that allows you to run Bash scripts and Linux binaries directly from your Windows system. It integrates seamlessly with both Windows Explorer and the Command Prompt, providing a versatile and efficient way to execute your scripts and binaries without needing a separate terminal or extra steps.

# Key Features:

* **Direct Execution**: Run your Bash scripts and Linux binaries directly from Windows Explorer or the Command Prompt. No need to open a separate terminal.

* **Linux Command Integration:** Easily link and manage Linux commands within your Windows environment.

* **Context Menu Integration**: Add options to the right-click context menu in Explorer, making it easy to execute scripts or commands from any directory.

* **Customizable SourceCode:** add Any code you want to the main batchfile (\\ProgramData\\RunBash\\RunBash.bat) to adjust the execution into your needs.

* **Customizable Execution**: Control output, error handling, and execution behavior with various parameters.

* **Root/Admin Access**: Option to run scripts with root or admin privileges, providing the flexibility to handle system-level tasks.

* **Error and Output Handling**: Fine-tune what outputs and errors are displayed or hidden, making debugging easier.

# Why Use RunBash?

RunBash bridges the gap between Windows and Linux environments, allowing you to leverage the power of Bash and Linux tools without leaving your Windows workspace. Whether you're a developer needing to run cross-platform scripts or a power user looking to streamline your workflow, RunBash offers a robust solution, and get you out the headacke of changing every path in the arguments from windows based to Linux based.

# Getting Started

To get started with RunBash, you can check out the repository on GitHub: [benzaria/runbash](https://github.com/benzaria/RunBash).

1. **Clone the Repo**: `git clone https://github.com/benzaria/RunBash.git`
2. **Run the Setup**: Execute `setup.bat` to install and configure RunBash.
3. **Start Using It**: You can now run Bash scripts or Linux binaries directly from Explorer or the Command Prompt!

# Feedback and Contributions

I'm always looking for feedback and ways to improve RunBash. Feel free to open issues or submit pull requests on the GitHub repo. Let's make running Linux tools on Windows as smooth as possible!

Thanks for checking it out! I hope you find RunBash as useful as I do. 🚀

https://redd.it/1f5up6l
@r_bash

Читать полностью…

r_bash

Fundamentals of handling passwords securely in a shell

I'm making this for a friend though it'd be nice to have a guide to hand people in general.

My gratitude in advance for ferocious criticism. Even if it's just a link or a nitpick it'll be gratefully appreciated so I can improve.

Cheers to everyone,

---

# Fundamentals of Handling Passwords Securely in a Shell
---

While this guide is orientated toward BASH it's relevant to all POSIX shells.

It's scope is the fundamentals of password delivery between programs in a shell enviroment intended to compliment various methods of encryption, file permissioning and software options.

# Parameters
---

Parameters of commands that are executed as a new process are exposed to ALL users through `/proc/$$/cmdline` for as long as that process exists.
See permissions: `ls -la "/proc/$$/cmdline"`

Examples:

#!/usr/bin/env bash

# printf WONT leak as it's a BASH builtin and won't generate a new process.
printf '%s\n' 'my secret'


# Functions WONT leak as they're a feature of the shell.
my_func(){ :; }
my_func 'my secret'


# sshpass WILL leak 'my secret' as it's not a built-in and executes as a
# new process.
sshpass -p 'my secret'


# Some examples of commands resulting in the same leak as expansion occurs
# before execution.
sshpass -p "$(read -sr -p 'enter password: ' pass; printf '%s' "$pass")"

sshpass -p "$(cat /my/secure/file)"

sshpass -p "$(</my/secure/file)"

# Variables
---

Variables used in the CREATION of a process are exposed to the CURRENT user through `/proc/$$/environ` for as long as that process exists, mindful that there's other ways for processes running under the same user to spy on each other.
See permissions: `ls -la "/proc/$$/environ"`

Examples:

#!/usr/bin/env bash

# Variable declaration WONT leak as it's defined within the BASH process.
pass='my secret'


# A function WONT leak a variable exported into it as it's a feature of
# the shell.
my_func(){ :; }
pass='my secret' my_func


# similarly exporting a variable into a built-in won't leak as it
# doesn't run as a new process.
pass='my secret' read -t 1


# sshpass WILL leak the exported variable to `environ` because it's not a
# built-in so the variable is used in the creation of it's process.
pass='my secret' sshpass

# Interactive History
---

This only applies to using BASH's interactive CLI, not the execution of BASH scripts.

By default commands are saved to ~/.bash_history when the terminal is closed and this file is usually readable by all users. It's recommended to `chmod 600` this file if the `$HOME` directory isn't already secured with similar permissions (ex: 700).

If a command contains sensitive information, ex: `printf '%s' 'my_api_key' | my_prog` the following are a few ways to prevent it being written to .bash_history:

1. You can use `history -c` to clear prior history
2. You can add ignorespace to HISTCONTROL so commands beginning with a space are not recorded: `[[ $HISTCONTROL == 'ignoredups' ]] && HISTCONTROL='ignoreboth' || HISTCONTROL='ignorespace'`
3. You can hard kill the terminal with `kill -9 $$` to prevent it writing history before close.


# Good Practices
---

Secrets should never be present in exported variables or parameters of commands that execute as a new process.

Short of an app secific solution, secrets should either be written to a program through an anonymous pipe (ex: `|` or `<()`) or provided in a parameter/variable as the path to a permissioned file that contains them.

Examples:

#!/usr/bin/env bash

# Only the path to the file containing the secret is leaked to `cmdline`,
# not the secret itself in the following 3 examples
my_app -f /path/to/secrets

my_app < /path/to/secrets

PASS_FILE=/path/to/secrets my_app


# Here variable `pass` stores the password entered by the uses which is
# passed as a parameter to the built-in `printf` to write it through an
# anonymous pipe to `my_app`. Then the variable is `unset` so it's not
# accidently used somewhere else in the script.
read -sr -p 'enter password: ' pass
printf '%s'

Читать полностью…

r_bash

One doubt about POSIX-Compliant features

Often I have several questions about if one binary, shell builtin or any of their options are POSIX compliant or not, such as unset -v

I'd like to know is there is any resource where I can check if above stuff is POSIX compliant or not

The truth is it seems as easy as google unset -v is posix compliant or not

But I could not find anything about that.

Probably there's an IEE resource right there or something like that.

Thanks in advance!!



https://redd.it/1f50od4
@r_bash

Читать полностью…

r_bash

Is there a better way to do this with rsync ?

I have a job and this is the logic I want to execute :

if /tmp/flagfile exists : place it in distant folder with rsync

if /tmp/flagfile and flagfile doesn't exist in distant folder : do nothing

if /tmp/flagfile doesn't exist but exists in distant folder : delete it in distant folder

I am not able to use ssh to rm remotely (only rsync available)

I have come up with this command which work, but I find overly complicated :

sudo rsync --archive --verbose --include="flagfile" --exclude="
" --delete /tmp/ /root/testdir/

For example, if I tried with this command, it would fail (file /tmp/flagfile not found)

sudo rsync --archive --verbose --delete /tmp/flagfile /root/testdir/

What do you think ?

https://redd.it/1f4qir8
@r_bash

Читать полностью…

r_bash

Tired of waiting for shutdown before new power-on, I created a wake-up script.

function riseAndShine()
{
local -r hostname=${1}
while ! canPing "${hostname}" > /dev/null; do
wakeonlan "${hostname}" > /dev/null
echo "Wakey wakey ${hostname}"
sleep 5;
done
echo "${hostname} rubs eyes"
}

This of course requires relevant entries in both:

/etc/hosts:

10.40.40.40 remoteHost

/etc/ethers

de:ad:be:ef:ca:fe remoteHost

Used with:

> ssh remoteHost sudo poweroff; sleep 1; riseAndShine remoteHost

Why not just reboot like a normal human you ask? Because I'm testing systemd script with Conflicts=reboot.target.


----

Edit: Just realized I included a function from further up in the script

So for completion sake:

function canPing()
{
ping -c 1 -w 1 ${1};
local -r canPingResult=${?};
return ${canPingResult}
}

Overkill? Certainly.

https://redd.it/1f4i02s
@r_bash

Читать полностью…

r_bash

renaming multiple files using part of its original name?

I am banging my head on this, but I have a feeling I may be over thinking it.

I have a bunch of files that look like this below,

I want to rename them to the original so its just using what's previous to the underscore _

ex:

drwxrwxrwx 2 root root 4096 Aug 29 14:47 ./
drwxrwxrwx 4 root root 4096 Aug 29 13:39 ../
10.102.30.310.10.30.320110531
10.101.30.310.10.30.320110531

so after the the script hoping for

drwxrwxrwx 2 root root 4096 Aug 29 14:47 ./
drwxrwxrwx 4 root root 4096 Aug 29 13:39 ../
10.102.30.3
10.101.30.3

stripping out the other stuff. any easy way to do this?

https://redd.it/1f4ahmz
@r_bash

Читать полностью…
Subscribe to a channel