0

I need to send mail to a particular user from host computer via shell script using command line argument.Mail should go to that particular user only.

Script execution will be like ./tesh.sh user emailid


cat test.sh
    #!/bin/sh
    export user=$1
    export email=$2
    list=`echo "$(cat ip.txt)"`
    script=$(cat remote_cmds.sh)
    for ip in ${list[@]} ; do
    echo "***";
    echo "IP: $ip"
        ssh -o StrictHostKeyChecking=no -l ${user} ${ip} ${script}
    done

   # cat remote_cmds.sh
    source pathto/test.sh
    echo "";
    echo "****Hostname****";
    echo "`hostname`";
    echo "";
    echo "****Disk Allocated****";
    echo "`df -h`";
    echo "";
    echo "****Ram Allocated****";
    echo testmail | mailx -s "Testmail" $email
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
user6690412
  • 41
  • 1
  • 2
  • 7
  • 2
    Not sure if I understand, please do mention what is your question here? – RavinderSingh13 May 18 '18 at 06:59
  • Sorry for the bad edit.I have edited my question.Hope it is clear now – user6690412 May 18 '18 at 07:11
  • kindly do mention in question(apart from code too) that what are you trying to achieve? and what did you try so far to achieve that? – RavinderSingh13 May 18 '18 at 07:13
  • from `./tesh.sh user emailid` seems that email is second parameter, just store parameter in a variable as `user=$1`; `email=$2`; and use it in the mailx command `"$email"` (or `"${email}"` in case of ambiguity), note that double quotes arround parameter are striongly advised to avoid argument be split or file glob matching – Nahuel Fouilleul May 18 '18 at 07:15
  • I get this error now Send options without primary recipient specified. Usage: mailx -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 – user6690412 May 18 '18 at 07:33
  • **Moderator Note**: Please do not vandalize your posts. Once you post a question, they belong to the site and its users. Even if it is no longer useful to you, it might be helpful to someone in the future. The answerers would have also put an effort in writing their answer, which would no longer be useful if you have removed the content from the post. Also, note that by posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the CC BY-SA 3.0 license). By SE policy, any vandalism will be reverted. – Bhargav Rao May 18 '18 at 11:35

1 Answers1

0

If I understand you correctly you have N computers with ip addresses saved in ip.txt. Each of these computers has a user named 'user' to which you can login via ssh. Each of these computers has a user named 'emailid' to which you want to send mail. You want to use ssh session to connect to these computers and run mail client on these computers. You want to print some information about hostname, disc space and some other infos from the remote computers on your computer and send simple mail with subject 'Testmail' and content 'testmail' to the local user 'emaillid' on these remote N computers.

These scripts are not good. Doing script=$(cat remote_cmds.sh) and then calling ssh ... ${scripts} is so dangerous... The line list=echo "$(cat ip.txt)" can be just shortened to list=$(cat ip.txt). list variable is not an array, so doing ${list[@]} is the same as $list. remote_cmds.sh has the line source pathto/test.sh, so that means that each of N computers has a test.sh file in the same path?

mailx is giving you arror, cause you are executing (literally) mailx -s Testmail $email and email address can't have $ characters (the email variable is not expanded).
Try smth like this:

test.sh

#!/bin/sh
# export is not needed anywhere here
user=$1
email=$2
# loop through the lines in ip.txt
cat ip.txt | while read ip; do
    echo "***";
    echo "IP: $ip"

    # https://unix.stackexchange.com/questions/87405/how-can-i-execute-local-script-on-remote-machine-and-include-arguments
    # login on computer with ip $ip on user $user and execute command on 
    # remote host 'bash -s', which will read commands from standard input,
    # append our remote_cmds.sh script to stdin and pass "$email" as first
    # argument to this script
    ssh -o StrictHostKeyChecking=no -l "$user" "$ip" bash -c -- < remote_cmds.sh "$email"

done
# this loop is bad and inefficient, but it's safe and simple to write
# see https://stackoverflow.com/questions/1521462/looping-through-the-content-of-a-file-in-bash

remote_cmds.sh

#!/bin/sh
# these lines will be executed on N computers

# email is passed as first argument to this script
email=$1

echo "";
echo "****Hostname****";
hostname
echo "";
echo "****Disk Allocated****";
df -h
echo "";
echo "****Ram Allocated****";
free -h  # ;)
echo testmail | mailx -s "Testmail" "$email"
KamilCuk
  • 120,984
  • 8
  • 59
  • 111