[SOLVED] piping email to a bash script for processing
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi all
I want to turn the following command into a bash script:
cat file | ssh user@system 'script'
ssh is setup so that I can do this without a password for this specific command.
However I'm not getting far.
The only way I can find to get a bash script to read a file is using 'read', which is one line at a time, and this doesn't work here.
Is there any way I can get all of the input file and pipe it into an ssh command?
I feel like I'm missing something fairly basic but I've been at this for a few hours now and I'm not getting too far.
You could try making the 'script' a forced command; I used that in
a previous employment for a certain application - not w/ emails,
but with lengthy textual output; and not into bash, but into perl.
Shouldn't make a difference for the use-case, though.
However I'm not getting far.
The only way I can find to get a bash script to read a file is using 'read', which is one line at a time, and this doesn't work here.
Can you post a short version of the script? In what way does the script "read" the file data? Are you wanting to pass the data as a filename to something else, e.g.
Code:
#!/bin/bash
./myprogram-needs-a-file <(cat)
I seem to recall a circumstance where read wouldn't work directly so I had to cat | read.
Kevin Barry
Tinkster - thanks for the input - I think Perl is the answer, it's fairly easy just using Net::SSH but I thought that bash should be easy too.
KenJackson - that's a neat idea thanks. the problem is that postfix executes milters (mail filters) directly piping the mail to the script, and it's going to be much more robust if I don't have to break it up in this way. I think it would be better to revert to perl.
ta0kira - hopefully the above answers your question. Postfix pipes the mail into the script directly. If the script was local, that would be fine, but the script is remote, so efectively postfix will
cat mail_file | localscript.
Inside "localscript" I want something that reads mail_file and pipes that to "ssh user@remote_system milter_script"
I hope that makes sense.
I have to say I don't really have any example to post yet. i found read fairly quickly but then couldn't see how to read all of the mail into something that I could pipe to ssh.
ta0kira - hopefully the above answers your question. Postfix pipes the mail into the script directly. If the script was local, that would be fine, but the script is remote, so efectively postfix will
cat mail_file | localscript.
Inside "localscript" I want something that reads mail_file and pipes that to "ssh user@remote_system milter_script"
I hope that makes sense.
So having a stand-alone line ssh user@remote_system milter_script doesn't work? You shouldn't need to pipe to ssh if the data is already being piped to the script. Are you trying to protect standard input from being read by something besides ssh?
Code:
#!/bin/bash
exec 3<&0 < /dev/null
read stuff #<-- nothing is read here
ssh user@remote_system milter_script <&3#<-- only ssh can read standard input
Tinkster - thanks for the input - I think Perl is the answer, it's fairly easy just using Net::SSH but I thought that bash should be easy too.
I didn't think of perl as "the answer" up there, but of the forced
command. Have a read about them: they work fairly well. Of course
I have to admit I never tried bash on the receiving end.
KenJackson - that's a neat idea thanks. the problem is that postfix executes milters (mail filters) directly piping the mail to the script, and it's going to be much more robust if I don't have to break it up in this way. I think it would be better to revert to perl.
Do what you must.
But I changed my experiment script on the remote server to this:
Code:
#!/bin/sh
DATA="$(< /dev/stdin)"
# Now process the text, e.g.:
echo "$DATA" | grep example
But I changed my experiment script on the remote server to this:
Code:
#!/bin/sh
DATA="$(< /dev/stdin)"
# Now process the text, e.g.:
echo "$DATA" | grep example
This has a slight functional difference in that any null characters will be discarded. I don't know enough about the problem to know if that's an issue (i.e. if binary data will ever be sent), however.
Kevin Barry
I didn't think of perl as "the answer" up there, but of the forced
command. Have a read about them: they work fairly well. Of course
I have to admit I never tried bash on the receiving end.
Cheers,
Tink
And with these words:
Code:
on machine 2
user@machine2$ echo command="/home/p652900/test",no-port-forwarding,no-X11-forwarding ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAoGgEV84faPDa9hsUmGUi2ZixGqAe03jQQ0Fkaz1llgZ9RCex/EJZ/Lny771t+oXyuYxaOtOIlya8oZDfBvaaYhWqxUPq81eF9PL43+ytI/C6m9nNhv4f4Tw12usd/Xi7qPUmaAT9fuWQCYHQniOoCLgY6fDcPTb97EDEIl9rIk4C8U+mij/ywq+UhgFLCmOtQM0RBw+q2bPoyjRm53RifAys1snjWlvGKxATGntI2TFE+sk933Id9MtOz/bB2yULWnRa9MPFXmYvVsJ1Hx0e/Ds4iSUy8k+K0HnQOvneAEGZG98bOzCmOnCvchOk8wqtwzSfPOkrMQ3QzvaqcmKOLQ== user@machine1 >> ~/.ssh/authorized_keys
vim /home/user/test
#!/bin/bash
count=1
while read line
do
echo $count $line
count=$(($count + 1))
done
on machine 1
cat /etc/shells | ssh -t user@machine2
1 /bin/bash
2 /bin/ash
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.