Sending mail using shell command in Ruby - ruby

I'm trying to send an email using shell command in my ruby script.
I use command
%x{echo "sometext" | mail -s "Account report #{file_tmp}" -a /home/linux/reports/#{file} #{address[0]}}
and I get
Send options without primary recipient specified. Usage: mail
-eiIUdEFntBDNHRVv~ -T FILE -u USER -h hops -r address -s SUBJECT -a FILE -q FILE -f FILE -A ACCOUNT -b USERS -c USERS -S OPTION users sh:
line 2: send_report#gmail.com: command not found
Why an email address is taken as second line of command and how to fix it?

Try stripping newlines on the file variable:
%x{echo "sometext" | mail -s "Account report #{file_tmp}" -a /home/linux/reports/#{file.strip} #{address[0]}}

Related

I want to delete file from server by bash script using ftp

I want to delete file by bash script using ftp
I use below code
$file = xyz/ab/file.txt
curl -v -u $user:$pass ftp://server.com/$file -Q "DELE $file"
but it's give these error
*Entry path is '/'
DELE xyz/ab/file.txt
* ftp_perform ends with SECONDARY: 0
< 550 Could not delete xyz/ab/file.txt: No such file or directory
* QUOT command failed with 550
How can I delete file with single line bash script command
How to delete file from ftp server with curl:
user="foo"
pass="bar"
dir="xyz/ab/" # with trailing slash
file="file.txt"
curl -v -u "$user:$pass" "ftp://server.com/$dir" -Q "-DELE $file"
or
curl -v -u "$user:$pass" 'ftp://server.com' -Q "-DELE /$dir$file"
or without leading /
curl -v -u "$user:$pass" 'ftp://server.com' -Q "-DELE $dir$file"
You could use the ftp command if you want to add additional commands.
user=foo
user=bar
ftp -n 127.0.0.1 <<EOF
quote USER $user
quote PASS $pass
delete xyz/ab/file.txt
exit
EOF
Deleting must be available on the ftp server, however. If I remember correctly, for vsftpd you must set anon_other_write_enable=YES in /etc/vsftpd.conf

bash script read server and not read password

I use curl to open with username and password router
curl http://192.168.1.1 --user admin:admin |grep -i "stats"
But when I made this code to use curl from bash script I have problem to read server and password from file `LINKS_FILE="server"
PASS="passwd"
for link in `cat "$LINKS_FILE"`
do
for pass in `cat "$PASS"`
do
res=$(curl -m 1 "http://${link}:8080" --user admin:${pass} )
if echo $res | grep -i "stats"; then `

How to save in a file bash and psql history?

I need help for the question on topic : i'm new to centos 6 environment and i need to export in a file the 2 histories so i can send it through e-mail.Thanks.
By default bash typed commands are saved in ~/.bash_history and psql saves it in ~/.psql_history, so you could send those files with this command:
$ mail -s 'My history files' -a ~/.bash_history -a ~/.psql_history myemail#gmail.com
Note that not all mail commands support the -a flag to send attachments, heirloom-mailx in ubuntu does.

Shell script to run a command and send the commands output as a report in email

I am trying to write a shell script which will run a command to ssh into multiple machines and store the output in a variable and send it as a report via email.Here is what I have in the script as of now:
#!/bin/bash
DcEmitterConn='yinst ssh -H test.out "netstat -a | grep ES | grep 25019 | wc"'
SUBJECT="DC-Connections"
EMAIL="abc#abc.com"
EMAILMESSAGE="report.out"
echo $DcEmitterConn> $EMAILMESSAGE
#send email using /bin/mail
/bin/mail -s "$SUBJECT" "$EMAIL"< $EMAILMESSAGE
After executing the above command in the script it would ask me for a password and then would print the requested output. The problem i am facing in the above script is that I am not able to store the command output in the variable and print it in the email body. Can someone please let me know if I am missing something.
the output would look something like this:
abc#dh1.data.ac2.abc.com's password: (yinst-pw)
40 240 3560
abc#dh2.data.ac2.abc.com's password: (supplied by yinst-pw)
50 300 4450
Thanks in advance!
You should put double quotes around $DcEmitterConn on line 8. And you can avoid the temporary file:
SUBJECT="DC-Connections"
EMAIL="abc#abc.com"
yinst ssh -H test.out "netstat -a | grep ES | grep 25019 | wc" | /bin/mail -s "$SUBJECT" "$EMAIL"

how to pass password to a command in bash

I want to write a bash script that will execute one command in the script, and the command need read some thing as password. So how can I pass the password to the command in the script?
$ota_gen -k $ota_key -i $1 -p $ota_tools $2 $ota_out_file
ota_key is a private key that need to be visited with a password, so how can I do it?
thank you.
ps: thanks hlovdal for help.
expect maybe what can help. But I don't know if it can interact with bash script, such as how to pass parameters from script to expect.
A quite common tool for feeding programs with proper input (like for instance passwords) non-interactively is the tool expect. The following example is given on the wikipedia page:
# Assume $remote_server, $my_user_id, $my_password, and $my_command were read in earlier
# in the script.
# Open a telnet session to a remote server, and wait for a username prompt.
spawn telnet $remote_server
expect "username:"
# Send the username, and then wait for a password prompt.
send "$my_user_id\r"
expect "password:"
# Send the password, and then wait for a shell prompt.
send "$my_password\r"
expect "%"
# Send the prebuilt command, and then wait for another shell prompt.
send "$my_command\r"
expect "%"
# Capture the results of the command into a variable. This can be displayed, or written to disk.
set results $expect_out(buffer)
# Exit the telnet session, and wait for a special end-of-file character.
send "exit\r"
expect eof
OK, I google and get the answer of how to interact with expect in bash script.
I have added lines bellow in my script.Ant it tack effect.
th
EXEC=$(expect -c "
spawn $ota_gen -k $ota_key -i $1 -p $ota_tools $2 $ota_out_file
expect \"Enter password for .... key>\"
send \"$PASSWD\r\"
interact
")
echo $EXEC
If you are passing sensitive information around and use it regularly you are probably best encrypting it.
Putting something like
#create key as follows - will prompt for password
#echo -n 'secret you want encrypted' | openssl enc -aes-256-cbc -a -salt -pbkdf2|base64
export MY_SECRET='VTJGc2RHVmtYMTlzVnBGWXNYUitLWlpYT3BWdStaQXJXeUVwc1JORnFsNWswZXJKT1dkRWpsWkxLWVFnK1hONQo='
Into your .bashrc will give you an encrypted environment variable that you can access where ever you need a secret, and you will be prompted for you passphrase/password that you used when creating the environment variable.
In the example above it is 'secret'
You access it is a command as follows
`echo $MY_SECRET|base64 --decode|openssl enc -aes-256-cbc -a -d -salt -pbkdf2 `
e.g.
xfreerpd /parameters.... /p:`echo $MY_SECRET|base64 --decode|openssl enc -aes-256-cbc -a -d -salt -pbkdf2`
For your query where $ota_key is the secret
$ota_gen -k $ota_key -i $1 -p $ota_tools $2 $ota_out_file
You can create the variable as follows
ota_key=`echo -n 'secret you want encrypted' | openssl enc -aes-256-cbc -a -salt -pbkdf2|base64`
Then use it as follows
$ota_gen -k `echo $ota_key|base64 --decode|openssl enc -aes-256-cbc -a -d -salt -pbkdf2 ` -i $1 -p $ota_tools $2 $ota_out_file
openssh will prompt you for a password to encrypt and decrypt each time, you can supply one as part of the command, but then you are just hiding things from the history etc. Have a look at https://www.tecmint.com/generate-encrypt-decrypt-random-passwords-in-linux/ for some info on using openssh for this. https://www.serverlab.ca/tutorials/linux/administration-linux/how-to-base64-encode-and-decode-from-command-line/ for base64 and How to assign an output to a shellscript variable? for different options on command substitution I have used back-tick ` above
PS Adding a function like
get-key()
{
echo -n "$1"|base64 --decode|openssl enc -aes-256-cbc -a -d -salt -pbkdf2
}
To your bashrc gives you quick access to the secret if you need it

Resources