How to use mailx command along with ssh in linux - linux

I am trying to run mailx after remoting into another linux box, but for some reason the command doesn't work. I also want to add the content of a csv file in the mail I am trying to send, but it is not working.
SERVER=host1
rm conncheck.csv
`netstat -na 63.111.184.111 442|grep "ESTABLISHED"|grep "63.111.184.111:442" >> conncheck.csv`
`netstat -na 63.111.184.201 572|grep "ESTABLISHED"|grep "63.111.184.201:572" >> conncheck.csv`
wordcount=`grep "ESTABLISHED" conncheck.csv|wc -l`
if [ $wordcount == 2 ]; then
`scp conncheck.csv $SERVER:/tmp/`
ssh -o "StrictHostKeyChecking=no" $SERVER `cat /tmp/conncheck.csv | mailx -s "LiquidityFIX connection is up" recepient#email.com < /dev/null'
else
ssh -o "StrictHostKeyChecking=no" $SERVER mailx -s 'LiquidityFIX connection is down <eom>' recepient#email.com < /dev/null'
fi
~
I think I am getting the command incorrect. Kindly advise how to get this working

in this line:
ssh -o "StrictHostKeyChecking=no" $SERVER `cat /tmp/conncheck.csv | mailx -s "LiquidityFIX connection is up" recepient#email.com < /dev/null'
you need to change the ` to ' right before the cat

Related

I want to create a script that automatically inputs password to ssh client to do reboot on /etc/rc.local

The code manually is:
ssh -fNv -L 3049:localhost:2049 ubuntu#20.115.5.61
Now I have in /etc/rc.local:
#!/bin/bash
remote_host=ubuntu#20.115.5.61
remote_port=3049
local_port=2049
cmd="ssh -fN -R ${remote_port}:localhost:${local_port} ${remote_host}"
while true; do
pgrep -fx "$cmd" >/dev/null 2>&1 || $cmd
sleep 10
done
¿Help me?

shell script to run commands over SSH on multiple remote servers [duplicate]

This question already has answers here:
While loop stops reading after the first line in Bash
(5 answers)
Closed 1 year ago.
I need to check if telnet is happening or not on multiple remote servers.
I wrote a while loop to SSH over multiple remote servers and triggers an email whenever the telnet fails. But the issue is while loops iterates only over the first server and exits out of the script without reading remaining servers. Below is my shell script
#!bin/bash
while read host_details
do
USERNAME=$(echo $host_details | awk -F"|" '{print $1}')
HOSTIP=$(echo $host_details | awk -F"|" '{print $2}')
PORT=$(echo $host_details | awk -F"|" '{print $3}')
PROXY=$(echo $host_details | awk -F"|" '{print $4}')
PROXY_PORT=$(echo $host_details | awk -F"|" '{print $5}')
STATUS=$(ssh -n ${USERNAME}#${HOSTIP} -p${PORT} "timeout 4 bash -c \"</dev/tcp/${PROXY}/${PROXY_PORT}\"; echo $?;" < /dev/null)
if [ "$STATUS" -ne 0 ]
then
echo "Connection to $PROXY on port $PROXY_PORT failed"
mutt -s "Telnet connection to $PROXY on port $PROXY_PORT failed" abc.def#xyz.com
else
echo "Connection to $PROXY on port $PROXY_PORT succeeded"
mutt -s "Telnet connection to $PROXY on port $PROXY_PORT succeeded" abc.def#xyz.com
fi
done<.hostdetails
I observed my script works only when I remove IF condition and my while loops iterates over all the servers as expected.
Can anyone suggest why when I use IF condition the script exits after first iteration? And how to make it work and get the email alerts?
Thanks to #GordonDavisson recommendation.
The mutt in the script is treating the remaining server details as an input list, hence the script is getting terminated after reading the first server values.
Replace mutt with another email program in your script.
However, users who wish to go with mutt, then it is recommended to add content to their email body or redirect the stdin from mutt to /dev/null
Below is the working solution:
#!bin/bash
while IFS="|" read -r userName hostIp Port Proxy proxyPort
do
STATUS=$(ssh -n -tt -o LogLevel=quiet ${userName}#${hostIp} -p${Port} 'timeout 4 /bin/bash -c' \'"</dev/tcp/${Proxy}/${proxyPort}"\'; echo $? < /dev/null | tr -d '\r')
if [ "$STATUS" -ne 0 ]
then
echo "Connection to $Proxy on port $proxyPort failed" | mutt -s "${hostIp}:Telnet connection to $Proxy on port $proxyPort failed" abc.def#xyz.com
else
echo "Connection to $Proxy on port $proxyPort succeeded" | mutt -s "${hostIp}:Telnet connection to $Proxy on port $proxyPort succeeded" abc.def#xyz.com
fi
done<.hostdetails
The trim command in the solution is to delete the carriage return(\r) I was getting in the variable(STATUS)

How to log non-interactive bash command sent through ssh

I'm sending a command through ssh:
ssh server.org 'bash -s' << EOF
ls -al
whoami
uptime
EOF
How to log it in the system (remote server)? I'd like to log those commands in some file (.bash_history or /tmp/log).
I've tried to add the line below to sshd_config:
ForceCommand if [[ -z $SSH_ORIGINAL_COMMAND ]]; then bash; else echo "$SSH_ORIGINAL_COMMAND" >> .bash_history; bash -c "$SSH_ORIGINAL_COMMAND"; fi
But it logs "bash -s" only.
I'll appreciate any help.
When bash shell exits, bash reads and executes commands from the ~/.bash_logout file. Probably you can run the history command at the end in the .bash_logout(of the server) and save it to some location.
If it suffices to work with the given command, we can put the necessary additions to enable and log command history at the beginning and end, e. g.
ssh server.org bash <<EOF
set -o history
ls -al
whoami
uptime
history|sed 's/ *[0-9]* *//' >>~/.bash_history
EOF
Or we could put them into the awfully long ForceCommand line:
… if [[ "$SSH_ORIGINAL_COMMAND" == bash* ]]; then echo "set -o history"; cat; echo "history|sed 's/ *[0-9]* *//' >>~/.bash_history"; else cat; fi | bash -c "$SSH_ORIGINAL_COMMAND"; fi

shell script ssh command not working

I have a small list of servers, and I am trying to add a user on each of these servers. I can ssh individually to each server and run the command.
sudo /usr/sbin/useradd -c "Arun" -d /home/amurug -e 2014-12-12 -g users -u 1470 amurug
I wrote a script to loop through the list and run this command but I get some errors.
#!/bin/bash
read -p "Enter server list: " file
if [[ $file == *linux* ]]; then
for i in `cat $file`
do
echo "creating amurug on" $i
ssh $i sudo /usr/sbin/useradd -c "Arun" -d /home/amurug -e 2014-12-12 -g users -u 1470 amurug
echo "==============================================="
sleep 5
done
fi
When I run the script it does not execute the command.
creating amurug on svr102
Usage: useradd [options] LOGIN
Options:
What is wrong with my ssh crommand in my script?
Try this script:
#!/bin/bash
read -p "Enter server list: " file
if [[ "$file" == *linux* ]]; then
while read -r server
do
echo "creating amurug on" "$server"
ssh -t -t "$server" "sudo /usr/sbin/useradd -c Arun -d /home/amurug \
-e 2014-12-12 -g users -u 1470 amurug"
echo "==============================================="
sleep 5
done < "$file"
fi
As per man bash:
-t
Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.

I am trying to send mail which will redirect the content of log files in logfile.txt in same directory.But its failing

Please find my script below:-
#!/bin/bash
date=`date +%Y%m%d`
ssh root#server-ip "ls -lrth /opt/log_$date/"
ssh root#server-ip "cd /opt/log_$date/; for i in `cat *.log`;do echo $i >> /opt/log_$date/logfile.txt; done;cat /opt/log_$date/logfile.txt| mail -s \"Apache backup testing\" saranjeet.singh#*****.com"
Any help will be appreciated. Thanks
Because you use double quotes, your backticks are getting evaluated on the local host before the SSH command executes.
A much better fix in this case is to avoid them altogether, though;
ssh root#server-ip "cat /opt/log_$date/*.log |
tee /opt/log_$date/logfile.txt" |
mail -s ...

Resources