Why I get an error when I try to execute a command remotely? - linux

I have a problem about execute command remotely via SSH. I am trying below.
ssh xx.xx.xx.xx "psql -U qradar -c "select count(id) from offense_view where to_timestamp(start_time/1000) > NOW() - interval '180 minutes'"
It gives an error like:
Pseudo-terminal will not be allocated because stdin is not a terminal.
ERROR: syntax error at or near "180"
LINE 1: ... to_timestamp(start_time/1000) > NOW() - interval 180 minute...

The problem is that you're using double quotes to delimit the argument to ssh and also the argument to psql inside the command. This is causing your strings to be parsed incorrectly. You're also missing the ending double quote for the psql command.
Nesting quotes is tricky in shell, and it's even harder when you're using ssh. It's easier if you use a here-doc.
ssh xx.xx.xx.xx <<EOF
psql -U qradar -c "select count(id) from offense_view where to_timestamp(start_time/1000) > NOW() - interval '180 minutes'"
EOF

Related

Using SSH with expected user input for picking an environment on the server

I am working on a script in linux in which i need to SSH to another server.
I am using this syntax ssh user#ip
When doing a manual SSH when we type "ssh user#ip" a few second, a second prompt will show and ask what environment are we choosing ex 1 to 4
(4) - this is the number of the environment i will be choosing.
But when doing ssh user#ip 4 - error received (bash command not found)
Here is the image on what is the prompt when using ssh
Without knowing what happens after you select the environment, its probably something like:
echo 4 | ssh user#ip
Feed 4 to whatever is prompting for environment, then exit.
If you need to stay connected, then something like
echo 4 | ssh -tt user#ip "ksh -l"
ksh -l could be replaced by some other shell or command like vi foo.txt (here when the command exits, the connection closes).
The above is similar to this previous question:
Run ssh and immediately execute command

Use Expect script with Expect/send

I want to get the value from the SQL command below. But I've got an error message every time.
The Expect code:
#!/usr/bin/expect
set timeout 60
spawn ssh -i id_rsa user#xxx.xxx.xxx.xxx
sleep 10
send -- "sudo su\r"
sleep 10
expect '[sudo] password for user: '
send -- "secret\r"
sleep 2
send "mysql -u root\r"
sleep 2
send -- "use nextcloud;\r"
sleep 2
send -- "SELECT * FROM oc_accounts;\r"
Error message:
spawn ssh -i id_rsa user#xxx.xxx.xxx.xxx
invalid command name "sudo"
while executing
"sudo"
invoked from within
"expect '[sudo] password for user: ' "
(file "./test.sh" line 8)
Single quotes have no special meaning in Expect. Expect is an extension of Tcl, where braces are the non-interpolating quoting mechanism.
You get the "invalid command name" error because square brackets are the Tcl command substitution syntax.
You want
expect {[sudo] password for user: }
Instead of sleep, with idiomatic Expect you should expect some pattern: your remote shell's prompt, the MySQL prompt, etc.
#pynexj has a good point. Try
expect -exact {[sudo] password for user:}

bash + how to avoid specific messages in the log file

when I run the bash script on my Linux machine we get the following errors in my log ,
note - we set in the script:
exec > $log 2>&1 , ( in order to send all standard error/output to $log )
the errors messages:
tput: No value for $TERM and no -T specified
in order to filter this errors messages we try to set in the bash script that:
export TERM=xterm
but without help
after digging we found that happened in some case for example when we perform ssh to remote machine and runs commands on remote machine VIA ssh
in order to avoid that we set TERM=xterm in the bash script as the following:
ssh user#$remote_machine "TERM=xterm /tmp/script.sh"
but it’s very not elegant solution and because my script use a lot of ssh and curl command then it’s not practical solution to set this on each SSH or curl etc
so my big question is
how to filter the message – tput: No value for $TERM and no -T specified ?
so we not get these ugly message in my $log file
Filter out all tput: No value from logging:
exec > >(grep -v 'tput: No value for $TERM and no -T specified' >$log) 2>&1

Bash shell commanding to vpn connection from command line

I am trying to write a bash shell (very first time) so that I can auto connect to a VPN server. I have never written a bash script before. can anyone tell me what command syntax I need for this?
#!/bin/bash
echo "Hi would you like to connect to vpn labs? Yes or No"
sleep 2
read answer
$connect = "rdesktop -u offsec -p ******** 192.168.***.***"
if $ answer != "No" then $connect
else
exit
For the right command to connect vpn, read the following. https://askubuntu.com/questions/57339/connect-disconnect-from-vpn-from-the-command-line.
First, run this in the terminal and see if it works. rdesktop -u offsec -p ******** 192.168.***.***"
Then you want to watch your syntax for your bash script.
#!/usr/bin/env bash
read -e -p "Hi would you like to connect to vpn labs? Yes or No " answer
sleep 2
if [ $answer != "No" ];
then rdesktop -u offsec -p ******** 192.168.***.***
else
exit
fi
I'd advise that you:
Learn the syntax of the main shell constructs (if-statement in
your case). You may use man bash and help if for that.
run each line in the shell and see if it's properly written or not, and
whether it does what you want or not.
Quick observations:
There is no '$' when you are setting the variable. It's used
when you reference the variable.
There is no space between $ and the variable name.

Executing CQL through Shell Script?

I am trying to execute the CQL commands from shell script.
I am able to connect to the cqlsh (CQL version i'm using is 1.1.18) but unable to send the queries to cql.
Any ideas or suggestion how to proceed on this?
Do I need to connect to Cassandra and execute few commands (select/update ) with shell script ??
cqlsh -e "select * from ks.table limit 1;" > ~/output
I'm not sure about Cassandra 1.1.18, but you should be able to accomplish this with the -f flag of cqlsh. Let's say have a file of CQL commands called "commands.cql". I can invoke those commands against my local Cassandra instance like this:
$ cqlsh -f commands.cql -u myusername -p mypassword localhost
If I wanted to invoke that from within a Bash script, the script's code would look something like this:
#!/bin/bash
cqlsh -f commands.cql -u myusername -p mypassword localhost
Save that as an executable file, and run it like any other.
Need to connect to cassandra and execute few commands (select / update ) with shell script
You can execute your commands with shell script in next way:
echo "some QUERY; exit" | cqlsh CASSANDRA_HOST -u 'USER' -p 'PASS'
The "exit" command in the last suggestion is a bit hacky.
I would propose using xargs with cqlsh -e.
echo "some QUERY;" | xargs cqlsh CASSANDRA_HOST -u 'USER' -p 'PASS' -e
I recently had to use this approach when working with docker, because clqsh -f was not an option (too complex to configure access to the file needed).
echo "some QUERY;" | xargs cqlsh CASSANDRA_HOST -u 'USER' -p 'PASS' -e
But what if you Cassandra instance is on a different server to where the shell script is being executed? (Specifically in StreamSets - wouldn't the above require Cassandra installed on the same server such that it has access to the cqlsh lib?)

Resources