How to authenticate from command line with redis-cli? - caching

Till now am using below command to remove all keys which follows some pattern
redis-cli --raw keys "abc*" | xargs redis-cli del
but recently we decided to add password for our redis server , so in one environment i added password for redis server , and then when i try to execute the above command to remove keys , it is not working , do i need to use password in the above command ? how to use password in the above command ?

You can specify -a followed by your password.
redis-cli -a yourpwd [plus other commands, see full example below]
You can pass multiple keys to one call of DEL command (DEL key1 key2 key3 ... keyN). Regarding setting of the password when calling redis-cli - you need to use -a option:
Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]
...
-a Password to use when connecting to the server.
Thus your final call can look like this:
redis-cli -a PASS --raw keys "abc*" | xargs redis-cli -a PASS del

Related

script to connect to a "list.txt" of servers

I am trying to find a way to connect to a list of servers written in a simple textfile to run one command and write the output to a file...
The small problem is, I have to login with a password... but it would not a problem to paste the password into the script.
the full command would be:
ssh "server_from_list.txt uptime | awk -F, '{sub(".*up ",x,$1);print $1}' >> /home/kauk2/uptime.out
lets assume the password is: abcd1234
Any suggestions??? I am not fit in scripting, sorry...
Many thanks to you all in advance...
regards,
Joerg
Ideally you should set up password-less login, but failing that you can use sshpass. First, get a single command working by trying the following:
export SSHPASS=abcd1234
Then you can try:
sshpass -e ssh user#server1 'uname -a'
When you get that debugged and working, you can use GNU Parallel to run the command on all servers in a file called list.txt
user#server1
user#server2
user#server3
user#server4
The command will be:
parallel -k -a list.txt sshpass -e ssh {} 'uptime'

How to make mosquitto_sub print ONLY current message when subscribed to one topic

mosquitto_sub.exe -h host-p port -t topic-P password -u username > log_file.txt
By using this command line all the message are saved in log_file.txt but how to save just the latest message to the log file .
As far as I know > works for overwriting the file , using this only at the start it overwrite the text file , after that it start appending all the messages in on file.
mosquitto_sub has a command line option -C to control the number of messages to receive before existing.
mosquitto_sub.exe -h host-p port -t topic -P password -u username -C 1 > log_file.txt
From the mosquitto_sub man page
-C
Disconnect and exit the program immediately after the given count of messages have been received. This may be useful in shell scripts
where on a single status value is required, for example.
Really ugly loop:
#echo off
:top:
mosquitto_sub.exe -h host-p port -t topic -P password -u username -C 1 > log_file.txt
goto top
You could embed the command in a for /f loop processing the output line by line overwriting the file content
#Echo off
for /f "delims=" %%A in ('
mosquitto_sub.exe -h host -p port -t topic -P password -u username
') Do #Echo(%%A>log_file.txt

passing password to curl on command line

I have a requirement where I am trying to write a shell script which is calling curl command internally. I have the password, username and url stored as variables in the script. However, since I want to avoid using user:password format of curl command in the script, I am just using curl --user command. My intention is to pass the password through stdin. So, I am trying something like this -
#!/bin/bash
user="abcuser"
pass="trialrun"
url="https://xyz.abc.com"
curl --user $user $url 2>&1 <<EOF
$pass
EOF
But this is not working. I know there are variations to this question being asked, but I didn't quite get the exact answer, hence posting this question.
You can use:
curl -u abcuser:trialrun https://xyz.abc.comp
In your script:
curl -u ${user}:${pass} ${url}
To read from stdin:
curl https://xyz.abc.com -K- <<< "-u user:password"
When using -K, --config specify - to make curl read the file from stdin
That should work for HTTP Basic Auth, from the curl man:
-u, --user <user:password>
Specify the user name and password to use for server authentication.
To expand on #nbari's answer, if you have a tool "get-password" that can produce a password on stdout, you can safely use this invocation:
user="abcuser"
url="https://xyz.abc.com"
get-password $user | sed -e "s/^/-u $user:/" | curl -K- $url
The password will be written to a pipe. We use sed to massage the password into the expected format. The password will therefore never be visible in ps or in the history.

Run cassandra queries from command line

I want to execute cql queries from bash command.
[cqlsh 3.1.8 | Cassandra 1.2.19 | CQL spec 3.0.5 | Thrift protocol 19.36.2]
[root#hostname ~]# /opt/apache-cassandra-1.2.19/bin/cqlsh -k "some_keyspace" -e "SELECT column FROM Users where key=value"
I got:
cqlsh: error: no such option: -e
Options:
--version show program's version number and exit
-h, --help show this help message and exit
-C, --color Always use color output
--no-color Never use color output
-u USERNAME, --username=USERNAME
Authenticate as user.
-p PASSWORD, --password=PASSWORD
Authenticate using password.
-k KEYSPACE, --keyspace=KEYSPACE
Authenticate to the given keyspace.
-f FILE, --file=FILE Execute commands from FILE, then exit
-t TRANSPORT_FACTORY, --transport-factory=TRANSPORT_FACTORY
Use the provided Thrift transport factory function.
--debug Show additional debugging information
--cqlversion=CQLVERSION
Specify a particular CQL version (default: 3.0.5).
Examples: "2", "3.0.0-beta1"
-2, --cql2 Shortcut notation for --cqlversion=2
-3, --cql3 Shortcut notation for --cqlversion=3
Any suggestions ?
First of all, you should seriously consider upgrading. You are missing out on a lot of new features and bug fixes.
Secondly, with cqlsh in Cassandra 1.2 you can use the -f flag to specify a file containing cql statements:
$ echo "use system_auth; SELECT role,is_superuser FROM roles WHERE role='cassandra';" > userQuery.cql
$ bin/cqlsh -u aploetz -p reindeerFlotilla -f userQuery.cql
role | is_superuser
-----------+--------------
cassandra | True
(1 rows)
You can use -f to execute from a file or SOURCE once you start CQLSH. I don't think -e is a valid option with that version.
It's bit dirty and unstable, but here is the answer:
/opt/apache-cassandra-1.2.19/bin/cqlsh -k "keyspace" -f /path/to/file.cql > /path/to/output.txt
tail -2 /path/to/output.txt | head -1 > /path/to/output-value.txt

How can I pipe the hostname into a call to SSH?

I'm trying to write a script to automate connecting to various remote instances but am having a hard time piping the resulting hostname to as an argument to ssh. I'm basically trying to do the following:
echo "example.com" | xargs -I {} ssh {}
I also tried a bunch of combinations but to no avail. The closest I got was with the following but it loses interactivity.
echo "example.com" | xargs -0 ssh -t -t
The end goal is to be able to have a script that will return a hostname/ipaddress that can then be connected to via ssh. For example:
my_random_script | ... ssh
Maybe, you want to run:
ssh `echo example.com`
ssh `your_random_script`
When you insert some command into back-quotes, this command is executed, and result stored in unnamed variable. For save/reuse, you can invoke something like:
VAR=`your_random_script`
ssh $VAR

Resources