I'm playing with some "defaults write" for my dotfiles & can found a way to write hexa values.
When I do a read I get something like this
$ defaults read com.ragingmenace.MenuMeters CPUSystemColor
<040b7374 7265616d 74797065 6481e803 84014084 8484074e 53436f6c 6f720084 84084e53 4f626a65 63740085 84016301 84046666 6666831b 677c3f00 83d70ea8 3d0186>
So I tried this commands without success (after checking the man defaults)
$ defaults write com.ragingmenace.MenuMeters CPUSystemColor -data "<040b7374 7265616d 74797065 6481e803 84014084 8484074e 53436f6c 6f720084 84084e53 4f626a65 63740085 84016301 84046666 66668364 79783f83 1b677c3f 83bf8073 3f0186>"
Command line interface to a user's defaults.
Syntax:
'defaults' [-currentHost | -host <hostname>] followed by one of the following:
read shows all defaults
read <domain> shows defaults for given domain
read <domain> <key> shows defaults for given domain, key
read-type <domain> <key> shows the type for the given domain, key
write <domain> <domain_rep> writes domain (overwrites existing)
write <domain> <key> <value> writes key for domain
rename <domain> <old_key> <new_key> renames old_key to new_key
delete <domain> deletes domain
delete <domain> <key> deletes key in domain
import <domain> <path to plist> writes the plist at path to domain
import <domain> - writes a plist from stdin to domain
export <domain> <path to plist> saves domain as a binary plist to path
export <domain> - writes domain as an xml plist to stdout
domains lists all domains
find <word> lists all entries containing word
help print this help
<domain> is ( <domain_name> | -app <application_name> | -globalDomain )
or a path to a file omitting the '.plist' extension
<value> is one of:
<value_rep>
-string <string_value>
-data <hex_digits>
-int[eger] <integer_value>
-float <floating-point_value>
-bool[ean] (true | false | yes | no)
-date <date_rep>
-array <value1> <value2> ...
-array-add <value1> <value2> ...
-dict <key1> <value1> <key2> <value2> ...
-dict-add <key1> <value1> ...
You can see that the format seems unrecognized.
So some variation without success
$ defaults write com.ragingmenace.MenuMeters CPUSystemColor -data <040b73747265616d747970656481e803840140848484074e53436f6c6f72008484084e534f626a656374008584016301840466666666836479783f831b677c3f83bf80733f0186>
# same error as the first one
$ defaults write com.ragingmenace.MenuMeters CPUSystemColor -data "<040b73747265616d747970656481e803840140848484074e53436f6c6f72008484084e534f626a656374008584016301840466666666836479783f831b677c3f83bf80733f0186>"
# same error as the first one
$ defaults write com.ragingmenace.MenuMeters CPUSystemColor -data <040b7374 7265616d 74797065 6481e803 84014084 8484074e 53436f6c 6f720084 84084e53 4f626a65 63740085 84016301 84046666 66668364 79783f83 1b677c3f 83bf8073 3f0186>
zsh: parse error near `\n'
$ defaults write com.ragingmenace.MenuMeters CPUSystemColor -data <'040b7374 7265616d 74797065 6481e803 84014084 8484074e 53436f6c 6f720084 84084e53 4f626a65 63740085 84016301 84046666 66668364 79783f83 1b677c3f 83bf8073 3f0186'>
zsh: parse error near `\n'
Any idea how to get this working ?
It's really simple, just remove <, > & all spaces.
$ defaults write com.ragingmenace.MenuMeters CPUSystemColor -data "040b73747265616d747970656481e803840140848484074e53436f6c6f72008484084e534f626a656374008584016301840466666666836479783f831b677c3f83bf80733f0186"
You can verify it's working by doing a defaults read
$ defaults read com.ragingmenace.MenuMeters CPUSystemColor
<040b7374 7265616d 74797065 6481e803 84014084 8484074e 53436f6c 6f720084 84084e53 4f626a65 63740085 84016301 84046666 6666831b 677c3f00 83d70ea8 3d0186>
Related
Idea: I'm trying to write a program that after using command line arguments it will send the arguments to database. There are 4 arguments - username, email, address and version. All arguments except username should be mandatory.
What have I complished so far:
My code:
parser = argparse.ArgumentParser()
parser.add_argument("-u", "--username", nargs = '?' , help="Insert your username", action="store_true")
parser.add_argument("uName", help="Insert your username", type=str)
parser.add_argument("-e", "--email", help="Insert your email", action="store_true")
parser.add_argument("eMail", help="Insert your email", type=str)
parser.add_argument("-a", "--address", help="Insert your address", action="store_true")
parser.add_argument("address", help="Insert your address", type=str)
parser.add_argument("-v", "--version", help="Insert your app version", action="store_true")
parser.add_argument("ver", help="Insert your app version", type=str)
args = parser.parse_args()
So if I write cmd using all the arguments:
$python test.py -u tom -e tom#company.com -a 45xx -v 1.1
It gives me correct output. No problem.
Now, if I write cmd leaving one argument out:
$python test.py -u tom -e tom#company.com -a 45xx
It gives me error:
usage: test.py [-h] [-u] [-e] [-b] [-a]
uName eMail aadress ver
test.py: error: the folowing arguments are required: ver
The way I understand it is that in my code all of them are positional arguments that are required to appear in the input and in my case all the "optional" arguments need a positional argument to save the input. Is it possible to make username argument that is not required in the input because the way I understand it in this case it is not and if thats true, can you suggest any alternative approach? Im thinking maybe there is a way to make username to give "Null" if its not in the input but I can't find a way to do it.
I would like to call a bash script in the mailing of Fail2Ban. The bash script calls a rest api to get the username of the blocked ip-address by fail2ban.
# Fail2Ban configuration file
#
# Author: *
#
#
[INCLUDES]
before = sendmail-common.conf
[Definition]
# Option: actionban
# Notes.: command executed when banning an IP. Take care that the
# command is executed with Fail2Ban user rights.
# Tags: See jail.conf(5) man page
# Values: CMD
#
actionban = printf %%b "Subject: [Fail2Ban] <name>: banned <ip> from `uname -n`
Date: `LC_ALL=C date +"%%a, %%d %%h %%Y %%T %%z"`
From: <sendername> <<sender>>
To: <dest>\n
The following IP <ip> has just been banned by Fail2Ban after <failures> attempts against the acceptance environment of <name> on server.\n
One or the following users could be the victim: \n
`/etc/fail2ban/restapi/getHttpSessions.sh | grep <ip>` \n | /usr/sbin/sendmail -f <sender> <dest>
[Init]
But however I got errors in the fail2ban log that no e-mail is sended
`/bin/sh /etc/fail2ban/scripts/getHttpSessions.sh | grep` 10.100.00.00 \n |/usr/sbin/sendmail -f fail2ban test#test.com -- returned 1
Is there some way to use a bash script in a fail2ban configuration file that lookups the IP-address in the bash created overview?
Thanks!
I got it working. Probably some issues within the syntax. Working code:
# Fail2Ban configuration file
#
# Author: Danny van den Berg
#
#
[INCLUDES]
before = sendmail-common.conf
[Definition]
# Option: actionban
# Notes.: command executed when banning an IP. Take care that the
# command is executed with Fail2Ban user rights.
# Tags: See jail.conf(5) man page
# Values: CMD
#
actionban = printf %%b "Subject: [Fail2Ban] <name>: banned <ip> from `uname -n`
Date: `LC_ALL=C date +"%%a, %%d %%h %%Y %%T %%z"`
From: <sendername> <<sender>>
To: <dest>\n
The following IP <ip> has just been banned by Fail2Ban after <failures> attempts against the production environment of <name> on server.\n
More information about the user:\n
[IP | KEY] \n
`/usr/bin/gethttpsessions | grep <ip> `\n\n" | /usr/sbin/sendmail -f <sender> <dest>
[Init]
# Default name of the chain
#
name = default
what is wrong with my code this is it -
export -P ttiUsername="Username: " || ^
i get an error about command ^ and the -p
here is my error
/Users/michaelgray/Desktop/ToontownWorld/ToontownInfiniteRetro/start_game.sh: line 3: export: -P: invalid option
export: usage: export [-nf] [name[=value] ...] or export -p
/Users/michaelgray/Desktop/ToontownWorld/ToontownInfiniteRetro/start_game.sh: line 3: ^: command not found
edit: i lowercased the p but it doesnt prompt u to enter username like it should
The problem with your code is this line:
export -P ttiUsername="Username: " || ^
It's wrong because export doesn't read data, -P is not a valid option, and ^ is not a valid command. None of it makes sense if the goal is to read data.
To read data from the user, use read:
read -p "Username: " ttiUsername
echo "You wrote: $ttiUsername"
On *nix, using BASH (etc) you ask the system where a command is located (etc) by using the 'type' shell built-in like this:
$ type cat
cat is /bin/cat
Is there an equivalent of this 'type' command in Microsoft PowerShell 2.0 ?
An equivalent is Get-Command.
PS C:\> Get-Command ls
CommandType Name Definition
----------- ---- ----------
Alias ls Get-ChildItem
Application ls.exe D:\usr\local\wbin\ls.exe
Application ls.exe C:\Program Files (x86)\Git\bin\ls.exe
Windows 10 Update:
Since I've posted this answer, it appears that the behavior of Get-Command has changed. To include all results (in the style of Un*x) type), now I need to pass the -All flag, like so:
PS C:\> Get-Command -All ls
CommandType Name Version Source
----------- ---- ------- ------
Alias ls -> Get-ChildItem
Application ls.exe 0.0.0.0 C:\Program Files (x86)\Git\usr\bin\ls.exe
As noted in a comment, this doesn't include the Definition column as was the previous behavior. I can't determine a command-line argument to add the definition column, but as noted by #voutasaurus in the comment below, one can use:
PS C:\> (Get-Command -All ls).Definition
Get-ChildItem
C:\Program Files (x86)\Git\usr\bin\ls.exe
Version information for reference (I odn't have the version information associated with the original answer text, but I'm guessing that it was Windows 7):
PS C:\> [System.Environment]::OSVersion.Version
Major Minor Build Revision
----- ----- ----- --------
10 0 15063 0
Get-Command has a -ShowCommandInfo parameter that does this. It also works for functions defined in $profile :
PS C:\Users\vp937ll> Get-Command l -ShowCommandInfo
Name : l
ModuleName :
Module : #{Name=}
CommandType : Function
Definition : Get-ChildItem | Sort-Object -Property LastWriteTime -Descending
ParameterSets : {#{Name=__AllParameterSets; IsDefault=False; Parameters=System.Management.Automation.PSObject[]}}
Since you tagged this with Shell, in addition to PowerShell's Get-Command, there's where.exe:
PS C:\> where.exe notepad
C:\Windows\System32\notepad.exe
C:\Windows\notepad.exe
The command just looks for a file with the specified name through the path:
PS C:\> where.exe readme.*
C:\Python31\README.txt
C:\Program Files (x86)\wget\README
C:\Program Files (x86)\SysinternalsSuite\readme.txt
Note that when calling this command from PowerShell, you must call it as where.exe because Where-Object is aliased to where.
Get-Command seems right, aliased as gcm
Just pipe it to Select-Object * aliased as select *
E.g., in $profile I have a function that opens total commander from pwd (it silently kills the existing instance first)
function start-totalCommanderhere {
$here = (Get-Location).path
kill -n TOTALCMD64 -ErrorAction Ignore
start "c:\totalcmd\TOTALCMD64.EXE" $here
}
Set-Alias tc start-totalCommanderhere
Here is all the info about the function — it however does not tell you directly that it is a function, you would need to query its proper name not alias to get that information from CommandType attribute. (than it would be same as type in bash which tells you hello_world is a function in case it is one)
▶ gcm tc | select *
HelpUri :
ResolvedCommandName : start-totalCommanderhere
DisplayName : tc -> start-totalCommanderhere
ReferencedCommand : start-totalCommanderhere
ResolvedCommand : start-totalCommanderhere
Definition : start-totalCommanderhere
Options : None
Description :
OutputType : {}
Name : tc
CommandType : Alias
Source :
Version :
Visibility : Public
ModuleName :
Module :
RemotingCapability : PowerShell
Parameters : {}
ParameterSets :
gcm <module_name> | select * gives you the path if you ask for a CommandType which is Application
▶ gcm ping | select *
HelpUri :
FileVersionInfo : File: C:\Windows\system32\PING.EXE
InternalName: ping.exe
OriginalFilename: ping.exe.mui
FileVersion: 10.0.19041.320 (WinBuild.160101.0800)
FileDescription: TCP/IP Ping Command
Product: Microsoft® Windows® Operating System
ProductVersion: 10.0.19041.320
Debug: False
Patched: False
PreRelease: False
PrivateBuild: False
SpecialBuild: False
Language: English (United States)
Path : C:\Windows\system32\PING.EXE
Extension : .EXE
Definition : C:\Windows\system32\PING.EXE
Source : C:\Windows\system32\PING.EXE
Version : 10.0.19041.1
Visibility : Public
OutputType : {System.String}
Name : PING.EXE
CommandType : Application
ModuleName :
Module :
RemotingCapability : PowerShell
Parameters :
ParameterSets :
But with gcm you do not get paths for imported modules — for that you need to use Get-Module | Select-Object Name, Path aliased as gmo | select name,path
▶ gmo | select name, path
Name Path
---- ----
assign-vault-keys-to-env-vars C:\Users\Admin\Documents\workspace\work.log\kb\powershell\assign-vault-keys-to-env-vars.ps1
CimCmdlets C:\Program Files\PowerShell\7\Microsoft.Management.Infrastructure.CimCmdlets.dll
decode-base64 C:\Users\Admin\Documents\workspace\work.log\kb\powershell\decode-base64.ps1
DnsClient C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\DnsClient\DnsClient.psd1
...
I have a BASH/shell script, running on Linux, but it sometimes has a problem getting the drive labels, using blkid.. blkid gets the drive labels nad UUIDs of the given drive.
Example of blkid output:
# blkid /dev/sda1
/dev/sda1: LABEL="Home" UUID="f1e5e82b-1c75-4fd7-8841-6ad766152dcf" TYPE="ext2"
The problem occurs when the drive label has a newline character... I have included the relevant function. The problem seems to be with the eval command.. Can someone help me?
blkid_name_generator() {
#Gather blkid output and filter out required device
if [ ! -f /tmp/blkid.txt ]; then
blkid -c /dev/null > /tmp/blkid.txt
fi
blkid_cleaner &
LABEL=""
LABEL1=""
eval $( cat /tmp/blkid.txt | grep "$#:" | cut --delimiter=" " -f 2- | sed -e 's/ /;/g')
LABEL1=${LABEL//;/ } # akita beta4 fix: remove newlines below
LABEL=`echo $LABEL1 | tr -d '\n'` # akita beta5 fix, remove newlines from drive label
#Generate drive label
[ "$LABEL" = "" ] && ONEDRVLABEL="$#\n \n " || ONEDRVLABEL="${LABEL}\n($#)\n "
echo "${LABEL}" and "${ONEDRVLABEL}"
}
blkid_name_generator /dev/sda1
Here's my bash snippet:
while read curline; do
if [[ ${curline:0:1} == "/" ]]; then
[[ $prevline ]] && echo "$prevline"
prevline="$curline"
else
prevline+="$curline"
fi
done < $INPUTFILE
[[ $prevline ]] && echo "$prevline"
Here's my test file:
/dev/sda1: LABEL="Boot" UUID="fdc5e51d-3da6-4edf-bf07-6397b1765797" TYPE="ext2"
/dev/sda2: LABEL="Root" UUID="f883df24-1b93-46fb-8990-840774c380c4" TYPE="ext4"
/dev/sda3: LABEL="Usr" UUID="ba759de3-e0e4-4603-a324-f11dc25fa784" TYPE="reiserfs"
/dev/sdb1: LABEL="Persis
tents" UUID="50b81ef0-a38b-4677-b9d9-9548b29ce2bb" TYPE="ext4"
/dev/sdc1: LABEL="Tempo
raries" UUID="a649c535-1a8a-4b2b-b0a0-4afbdc60a3bc" TYPE="reiserfs"
/dev/sdd1: LABEL="Usr
Portage" UUID="a228148e-6405-4bbe-990f-df6eaebb1b1d" TYPE="reiserfs"
/dev/sda1: LABEL="Boot" UUID="fdc5e51d-3da6-4edf-bf07-6397b1765797" TYPE="ext2"
/dev/sda2: LABEL="Root" UUID="f883df24-1b93-46fb-8990-840774c380c4" TYPE="ext4"
Here's my test file after processed by the bash snippet:
/dev/sda1: LABEL="Boot" UUID="fdc5e51d-3da6-4edf-bf07-6397b1765797" TYPE="ext2"
/dev/sda2: LABEL="Root" UUID="f883df24-1b93-46fb-8990-840774c380c4" TYPE="ext4"
/dev/sda3: LABEL="Usr" UUID="ba759de3-e0e4-4603-a324-f11dc25fa784" TYPE="reiserfs"
/dev/sdb1: LABEL="Persistents" UUID="50b81ef0-a38b-4677-b9d9-9548b29ce2bb" TYPE="ext4"
/dev/sdc1: LABEL="Temporaries" UUID="a649c535-1a8a-4b2b-b0a0-4afbdc60a3bc" TYPE="reiserfs"
/dev/sdd1: LABEL="UsrPortage" UUID="a228148e-6405-4bbe-990f-df6eaebb1b1d" TYPE="reiserfs"
/dev/sda1: LABEL="Boot" UUID="fdc5e51d-3da6-4edf-bf07-6397b1765797" TYPE="ext2"
/dev/sda2: LABEL="Root" UUID="f883df24-1b93-46fb-8990-840774c380c4" TYPE="ext4"
Hope this helps!
I don't see exactly what your script is intending to do (apart from the missing bits).
Here's what I came up with:
blkid | perl -ne 'print "$1\n" if m/LABEL="(.*?)"/o'
It will show this (on my system):
test^J123
WIN7VIRT
TEMP
Note how the newline in the label is depicted as ^J ? Viewing through a hex encoder shows that it is indeed a two-character combination 0x5e 0x4a
Hope you can get some further with that.
PS: 'proof' that the newline is in fact correctly in the volume label
tune2fs /dev/sdc1 -l
tune2fs 1.41.14 (22-Dec-2010)
Filesystem volume name: test
123
Last mounted on: /media/9868d90c-aede-4e7a-b105-d9312f8b17ab
Filesystem UUID: 9868d90c-aede-4e7a-b105-d9312f8b17ab
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
...
and on block level:
dd if=/dev/sdc1 count=10 | xxd | grep 123
0000470: b105 d931 2f8b 17ab 7465 7374 0a31 3233 ...1/...test.123
Thanks to this question... I could find a solution for another problem. Example (sda2 = ntfs partition):
blkid /dev/sda2
Segmentation Fault (core dump)
Hard to find troubleshooting guide for this issue (ubuntu based OS or any other)
The reason behind is a malformed LABEL in NTFS partition.
(of course, mal-formation we cant see, as newlines ;-)
Solution: Clear LABEL with GParted; then rewrite it or set a new LABEL.