To help myself remember that I am logged into a different system, quickly and visually, I use different colors for the \h field in the Bash PS1 environment variable.
I am looking for a way to reliably generate the PS1, tied to a static host-identifier; say, via the hostid command.
Originally, my PS1, which was adapted from something generated by bashrcgenerator.com, looked like:
export PS1="\u#\[$(tput bold)\]\[$(tput sgr0)\]\[\033[38;5;35m\]\h\[$(tput sgr0)\]\[$(tput sgr0)\]\[\033[38;5;15m\]\[$(tput sgr0)\]:\[\033[38;5;245m\]\w\[$(tput sgr0)\]\[\033[38;5;15m\]\[$(tput sgr0)\]\\$ "
What a mess.
So to start any progress, the first step was to do some refactoring. This landed me at the following script:
bold="$(tput bold)"
reset="$(tput sgr0)"
green="\e[38;5;35m"
gray="\e[38;5;245m"
directory='\w'
host='\h'
user='\u'
function colorize() {
echo -n "${2}${1}${reset}"
}
export PS1="${user}#$(colorize $host $green):$(colorize $directory $gray)\\$ "
At this point, you can at least see what the heck is going on.
Now, I need to write a function like:
get_repeatable_color_for_hostid() {
# hash the $(hostid) into a valid color escape string
# e.g. 16ab1d60 --> \e[38;5;35m
}
In order to do so, I need to understand:
What are the meanings of the field portions xx;y;zzm inside of e.g. \e[38;5;35m?
How do I do the hashing from the hostid to that color escape sequence, s.t. colors are randomized as much as possible.
Regarding colors this page helped me a lot to understand all details: http://misc.flogisoft.com/bash/tip_colors_and_formatting
Since there aren't so many colors to chose from one idea would be to simply assign a color to each of the host, and in case you have more hosts then group the hosts based on importance (dev, prod etc.) each group with it's own color.
There is also this idea to generate the color (from 1 to 256) using the checksum of the hostname: COLOR=$(($(hostname | cksum | cut -f1 -d" ")%256+1))
Related
I want to have a standard BASH PS1 prompt that will automatically have a colour set based upon the hostname of the server.
This would meant that whenever logging into a server, it would instantly be clear and familiar that you are on that server as the prompt colour would be different to other servers
A given hostname must always give the same colour
I have found similar ideas with hex codes, but this is specifically for use with BASH colours
The idea would then be that there can be a standard bash prompt code snippet that can be included everywhere and will always give different colours for different servers without any further code changes
In a nutshell, the question is what bash function could you write that will take 2 arguments - a string and a hash. It should echo out the string in a colour that is determined by the hash, and the colour should always be the same for any given hash
EDIT - TO CLARIFY
the answers so far assume that host names are known in advance
I am looking for something that will assign the same random colour deterministically based on whatever host name is for that server
I am definitely not looking for something that requires any kind of code change when installing the PS1 on a new server
This post is along the lines but doesn't seem to have a simple PS1 snippet that I can use https://aweirdimagination.net/2015/02/28/better-hash-based-colors/
I am looking for something that will assign the same random colour deterministically based on whatever host name is for that server
Take a hashing algorithm and compute the hash of the hostname - as to convert a string to numbers. Then use this pseudorandom-number to generate a color.
gen_prompt_function() {
local number
number=$(
# get "random" string that depends on hostname
md5sum <<<"$HOSTNAME" |
# meh - take first byte and convert it to decimal
cut -c-2 | xargs -i printf "%d\n" "0x{}" |
# convert 0-255 range into 30-37 range
awk '{print int($0/255.0*(37-30)+30)}'
)
printf '\[\e[%d;1m\]%s\[\e[m\]' "$number" "$HOSTNAME"
}
PS1="$(gen_prompt_function)"'$ '
Read ANSI escpe sequences, tput and about color handling in terminfo and bash manual controlling prompt. Remember to add \[ \] around color codes.
PS. In my bash adventures solely for hostname prompt coloring I have written a color handling script that generates a rainbow from 3 RGB colors taken from first 18 characters extracted from hash of a string, like in screeshot below. That script is used in my PS1 configuration.
declare -A color=([hosty]=33 [hostr]=31 [hostb]=34 [hostm]=35)
function color () {
host=$1
printf $'\e[%d;1m%s\e[m' "${color[$host]}" "$host"
}
PS1='$(color $HOSTNAME)$ '
Determine what the host name is, then define PS1.
case $HOSTNAME in
foo.com) color='...' ;;
bar.org) color='...' ;;
esac
PS1="..." # using $color as necessary
i have config file like this for example:
# Blah blah, this is sample config file blah blah
# Something more blah blah
value1=YES
value2=something
#value3=boom
# Blah blah
valueN=4145
And i want to make script to read and edit config files like this. I thinking about make a menu with groups of config options, then after write an option console output will be like this:
Group of funny options (pick option to change value):
1. value1=YES
2. value2=something
3. [disabled]value3=boom
After picking 1 for exaple i can change value1 from YES to NO or disable and activate other (hash unhash) plus adding new variables to the end of file. Then in the end save all changes in this config file. Any tips what i need to use? Actually trying with read line + awk to skip # lines (with space), but still i have problem to get all this variables and making changes in config file. I will be grateful for your help.
Edit.
while read line
do
echo $line | awk '$1' != "#" && / / { print $1 $3 }'
done < config.conf
Thinking about this for now to read informations what i want. Plus i'm gonna use something like this to change values:
sed -c -i "s/("one" *= *).*/\1$two/" config.conf
I have completly no idea how i can get this variables to my script and use it like i write before. Actually i search for any tips, not someone who write this script for me. I'm beginner at linux scripting :V
I would recommend to abstain from such an, seemingly generic configuration program, because the comments might contain important informations about the current value and will be outdated, if the values change, while the comments don't.
Second problem is, that I would expect, if activating an entry is possible, deactivating it should be possible too. So now you have 2 options what to do with each value.
Third problem: In most cases, guessing a type by the value might work. YES seems to be a boolean, 47 an int, foobar a name - or is it a file? - but often a wider type is possible too, so YES can be just a string or a file, 47.3 might be valid where 47 is or might be not and so on.
However, for experimenting and trying things out, select and grep might be a start:
select line in $(grep "=" sample.conf) "write" "abort"
do
case $line in
"write") echo write; break ;;
"abort") echo abort; break ;;
'#'*=*) echo activate $line;;
*=[0-9]*) echo int value $line;;
*=YES|NO) echo boolean value $line;;
*) echo text value $line ;;
esac
done
Instead of 'echo intvalue $line' you would probably call a function "intconfigure" where only int values are accepted. For "write", you would write back to the file, but I omitted, conserving the comments without assignment and sorting them in again at the right position has to be done, which isn't trivial, given the opportunity to activate or deactivate comments.
But read up on the select command in shell and try it out and see how far you come.
If you think you have reached a usable solution, use this for all your configuration files privately and see, whether you prefer it over using a simple editor or not.
My current snippet of code looks like this ...
#Location of network config files
nfds="/etc/sysconfig/network-scripts/"
#Standard prefer of network config files
fil="ifcfg-"
#Array variable that feeds "$nic"
cards= array loop built from "nic=$(ls /sys/class/net | grep en)"
#Set color for Divice labile
div="\033[38;5;39m"
#Set Fix format and colour info
fix="\033[38;5;118m"
#Set color for OK
ok="\033[38;5;28m"
#Clear All font and color info
ctf="\033[0m"
function currentCardDefRoute(){
defr=$(grep DEFROUTE $nfds$fil$cards | cut -d = -f 2)
if [[ $defr = "yes" ]] || [[ $defr = "no" ]]; then
echo -e " "$div$cards$ctf"'s current default route is\t"$div$defr$ctf"\t\t\t\t ["$ok"OK"$ctf"]"
$st
else
echo -e " "$div$cards$ctf"'s current default route is \t"$fix"Missing"$ctf"\t\t\t ["$fix"PLEASE FIX"$ctf"]"
$st
fi
}
I indent 1 space on all echo lines for readability and consistent formatting. Keeping output readable and easy to understand.
Im looking to us the "columns" option and make the output more dynamic and have the format consistent no matter the screen size or var result. I would love to also get rid of all the "\t"s in my code. I have tried printf to no success.
I googled a lot of different ways and not seen the specific answer Im looking for or a variation I can draw an answer from.
Thank you for your help.
btw. This is the first code I have ever written so go easy guys :)
You may want to try using the column utility. It's sole purpose is for formatting output into columns. That may be easier than trying to do the same thing with echo or printf.
If you have to use printf, you'll want to use a format specifier like "%25.25s". The first number is the "minimum field width", which (in this case) causes the output to be at least 25 characters wide. If the output is shorter, it's padded with whitespace. The second number indicates the maximum number of characters to print. When these two numbers are the same, it effectively says to print the string in a field that's exactly 25 characters wide. You can use this to force varying-length strings to take up the same amount of space on the screen.
We have a mail server and I am trying to write a script that will find all users with similar names to avoid malicious users from impersonating legitimate users. For example, a legit user may have the name of james2014#domain.com but a malicious user may register as james20l4#domain.com. The difference, if you notice carefully, is that I replaced the number 'one' with the letter 'l' (el). So I am trying to write something that can consult my /var/vmail/domain/* and find similar names and alert me (the administrator). I will then take the necessary steps to do what I need. Really appreciate any help.
One hacky way to do this is to derive "normalized" versions of your usernames, put those in an associative array as keys mapping to the original input, and use those to find problems.
The example I posted below uses bash associative arrays to store the mapping from normalized name to original name, and tr to switch some characters for other characters (and delete other characters entirely).
I'm assuming that your list of users will fit into memory; you'll also need to tweak the mapping of modified and removed characters to hit your favorite balance between effectiveness and false positives. If your list can't fit in memory, you can use a single file or the filesystem to approximate it, but honestly if you're processing that many names you're probably better off with a non-shell programming language.
Input:
doc
dopey
james2014
happy
bashful
grumpy
james20l4
sleepy
james.2014
sneezy
Script:
#!/bin/bash
# stdin: A list of usernames. stdout: Pairs of names that match.
CHARS_TO_REMOVE="._\\- "
CHARS_TO_MAP_FROM="OISZql"
CHARS_TO_MAP_TO="0152g1"
normalize() {
# stdin: A word. stdout: A modified version of the same word.
exec tr "$CHARS_TO_MAP_FROM" "$CHARS_TO_MAP_TO" \
| tr --delete "$CHARS_TO_REMOVE" \
| tr "A-Z" "a-z"
}
declare -A NORMALIZED_NAMES
while read NAME; do
NORMALIZED_NAME=$(normalize <<< "$NAME")
# -n tests for non-empty strings, as it would be if the name were set already.
if [[ -n ${NORMALIZED_NAMES[$NORMALIZED_NAME]} ]]; then
# This name has been seen before! Print both of them.
echo "${NORMALIZED_NAMES[$NORMALIZED_NAME]} $NAME"
else
# This name has not been seen before. Store it.
NORMALIZED_NAMES["$NORMALIZED_NAME"]="$NAME"
fi
done
Output:
james2014 james20l4
james2014 james.2014
I have a folder of files which need to be renamed.
Instead of a simple incrimental numeric rename function I need to first provide a naming convention which will then incriment in order to ensure file name integrity within the folder.
say i have files:
wei12346.txt
wifr5678.txt
dkgj5678.txt
which need to be renamed to:
Eac-345-018.txt
Eac-345-019.txt
Eac-345-020.txt
Each time i run the script the naming could be different and the numeric incriment to go along with it may also be ddifferent:
Ebc-345-010.pdf
Ebc-345-011.pdf
Ebc-345-012.pdf
So i need to ask for a provided parameter from the user, i was thinking this might be useful as the previous file name in the list of files to be indexed eg: Eac-345-017.txt
The other thing I am unsure about with the incriment is how the script would deal with incrimenting 099 to 100 or 999 to 1000 as i am not aware of how this process is carried out.
I have been told that this is an easy script in perl however I am running cygwin on a windows machine in work and have access to only bash and windows shells in order to execute the script.
Any pointers to get me going would be greatly appreciated, i have some experience programming but scripting is almost entirely new.
Thanks,
Craig
(i understand there are allot of posts on this type of thing already but none seem to offer any concise answer, hence my question)
#!/bin/bash
prefix="$1"
shift
base_n="$1"
shift
step="$1"
shift
n=$base_n
for file in "$#" ; do
formatted_n=$(printf "%03d" $n)
# re-use original file extension whilke we're at it.
mv "$file" "${prefix}-${formatted_n}.${file##*.}"
let n=n+$step
done
Save the file, invoke it like this:
bash fancy_rename.sh Ebc-345- 10 1 /path/to/files/*
Note: In your example you "renamed" a .txt to a .pdf, but above I presumed the extension would stay the same. If you really wanted to just change the extension then it would be a trivial change. If you wanted to actually convert the file format then it would be a little more complex.
Note also that I have formatted the incrementing number with %03d. This means that your number sequence will be e.g.
010
011
012
...
099
100
101
...
999
1000
Meaning that it will be zero padded to three places but will automatically overflow if the number is larger. If you prefer consistency (always 4 digits) you should change the padding to %04d.
OK, you can do the following. You can ask the user first the prefix and then the starting sequence number. Then, you can use the built-in printf from bash to do the correct formatting on the numbers, but you may have to decide to provide enough number width to hold all the sequence, because this will result in a more homogeneous names. You can use read to read user input:
echo -n "Insert the prefix: "
read prefix
echo -n "Insert the sequence number: "
read sn
for i in * ; do
fp=`printf %04d $sn`
mv "$i" "$prefix-$fp.txt"
sn=`expr $sn + 1`
done
Note: You can extract the extension also. That wouldn't be a problem. Also, here I selected 4 numbers fot the sequence number, calculated into the variable $fp.