Telegram-cli : Script not sending message - linux

I am making a simple bash script with Telegram-cli, in which I send 2 parameters (destination and message), and it should send a Telegram message to the destination (Name_FamilyName).
The script looks as follows:
#!/bin/bash
destination=$1;
message=$2;
(echo "msg $destination $message"; echo "safe_quit") | bin/telegram-cli -k tg-server.pub -W
With that, in theory, messages should be sent. I change the permissions of the script, and I call it in the next way:
./script_send_message.sh Max_Musterman "Hola qute tal estas"
And that is the output I get:
Telegram-cli version 1.2.0, Copyright (C) 2013-2015 Vitaly Valtman
Telegram-cli comes with ABSOLUTELY NO WARRANTY; for details type `show_license'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show_license' for details.
Telegram-cli uses libtgl version 1.2.0
I: config dir=[/home/machine/.telegram-cli]
> msg Max_Musterman Hola qute tal estas
> safe_quit
User Max_Musterman updated username
User Max_Musterman online (was online [2015/04/09 06:56:04])
User Test Phone offline (was online [2015/04/09 06:51:42])
> > All done. Exit
halt
No message has been sent at all. Insted, if I send exactly the same message from the console, it works fine. Here is what I do:
bin/telegram-cli -k server.pub -W
Telegram-cli version 1.2.0, Copyright (C) 2013-2015 Vitaly Valtman
Telegram-cli comes with ABSOLUTELY NO WARRANTY; for details type `show_license'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show_license' for details.
Telegram-cli uses libtgl version 1.2.0
I: config dir=[/home/machine/.telegram-cli]
User Max_Musterman updated username
User Max_Musterman online (was online [2015/04/09 06:59:46])
User Max_Musterman offline (was online [2015/04/09 06:51:42])
> msg Max_Musterman Hola qute tal estas
[06:57] Max_Musterman <<< Hola qute tal estas
User Max_Musterman marked read 1 outbox and 0 inbox messages
User Max_Musterman offline (was online [2015/04/09 06:57:29])
>
I am running out of ideas. I start to think that, somehow, the contact list is not loaded when the command msg is sent in the script, so it sends nothing(in the console, if you send a message to a made up user, it won't do anything like in the script).
Has anyone experienced something similar? Any solution? Thank you for your help.

You have other option:
instead of piping commands to telegram-cli you can use "-e" option and the user_id, this way:
telegram-cli -RD -e "msg user#nnnnnnn Hola caracola"
where nnnnn is the user_id. You can find it via the "user_info ....." command.
This way you don't need to sleep, just make the telegram-cli do all the work, and without using the -W command telegram doesn't need to get all your contacts.

Looks like when you run telegram_cli from the script, it needs a bit of time to be able to send any message (until it shows the list of Users at least). If you send a message before the user list is loaded, you won't be able to send anything. So a quick fix (or we may call it a naughty hack) is to tell the script to wait 3 seconds for sending the message:
#!/bin/bash
destination=$1;
message=$2;
(sleep 3;echo "msg $destination $message"; echo "safe_quit") | bin/telegram-cli -k tg-server.pub -W
Maybe you have to change the sleep3 to sleep 5 or something like that, but it should be able to send the messages after that.

Related

Linux shell emailing - set sender address using "mail" command

I am using the "mail" command in Linux shell to send an email when programmatically prompted to do so. I am using
mail -s 'subject' recipient#theirhost.com <<< 'Email body'
to send it at present, but this does not include a sender address. Instead, the system uses the account name as the sender address. I've seen the claim that "-aFrom:myname#myhost.com" should work, but -a is being interpreted as an attachment attempt, not a header change. This results in the error: "From:Mynamemyname#myhost.com: No such file or directory". I've also seen a claim that this works:
-r "from#fromserver.com"
But the OS doesn't even know what that means, in my case.
Is there a way to make this work without additional software? I don't need a from name, but a from address would be very helpful.
Note: let's consider ssmtp, mutt, and other such add-on software unavailable. Attempting to install and use such software would be problematic.
Thank you!
Edit 1: I'm not certain how to check what version of "mail" I have. With some difficulty, I got it to spit out the usage, which is as follows.
-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
Never mind, I got it. "-r" can actually work.
I successfully used:
echo "This is the message body" | mail -s "This is the Subject" -r "FromName<fromAddress#example.com>" recipientName#example.com
It looks like karakfa was onto something also, as I do have mailx available to me also, but it looks like I won't be needing that for this use case.
Thank you!

How to log the live output of a running process

I want to run a game server inside my Ubuntu machine. I want to run it in the background and write the live output of that process inside a log file. I tried using nohup and running the game server using "&" at the end but I couldn't make it work the way I wanted.
Then I started reading about named pipes and actually gave it a go. I made a simple script that in theory should work. But, of course I am missing something.
First, I made a pipe using the mkfifo command.
mkfifo testpipe
Then I created a small script:
#!/bin/bash
./mta-server64 > pipe &
pid=$!
echo $pid // so I know the pid of the process
cat < pipe > log.txt &
(Note: I wrote this code from memory.)
The code works only when there is an error and the process stops. It actually records the game console error. But when the game server is running I get no output in the log file.
I want to read the output (stdout and stderr if I am not mistaken) of a process running in background and record it those inside a log file.
I also thought about using screen as it logs everything inside a file but I would prefer not using it if there is a better solution.
EDIT:
First of all: thank you for the interest you had in helping me. In the same way, I have to apologize for only giving scarce details about what I intend to do with this small project and for my limited understanding of stdout and stderr.
Let's go to the first base.
I want to run a game server named Multi Theft Auto (https://multitheftauto.com/). This is GTA San Andreas but multiplayer.
I can easily run this game server in my Ubuntu server by calling the executable ./mta-server-64. After calling it the game server console appears:
[|] MTA: San Andreas :: 0/32 players :: 196 resources :: 125 fps (25)
MTA:BLUE Server for MTA:SA
==================================================================
= Multi Theft Auto: San Andreas v1.5.6 [64 bit]
==================================================================
= Server name : Default MTA Server
= Server IP address: auto
= Server port : 22884
=
= Log file : /root/mta/mods/deathmatch/logs/server.log
= Maximum players : 32
= HTTP port : 22564
= Voice Chat : Disabled
= Bandwidth saving : Medium
==================================================================
[09:49:07] Resource 'mapmanager' requests some acl rights. Use the command 'aclrequest list mapmanager'
[09:49:07] Resources: 196 loaded, 0 failed
[09:49:07] Starting resources...
[09:49:07] Server minclientversion is now 1.5.6-9.16588.0
[09:49:07] INFO: MAPMANAGER: Some important ACL permissions are missing. To ensure the correct functioning of Mapmanager, please write: aclrequest allow mapmanager all
[09:49:07] Gamemode 'play' started.
[09:49:07] Authorized serial account protection is enabled for the ACL group(s): `Admin` See http://mtasa.com/authserial
[09:49:07] WARNING: <owner_email_address> not set
[09:49:07] Server started and is ready to accept connections!
[09:49:07] To stop the server, type 'shutdown' or press Ctrl-C
[09:49:07] Type 'help' for a list of commands.
[09:49:07] Querying MTA master server... success! (Auto detected IP:xxx.xxx.xxx.xxx)
I am using the following script to run the process in the background and (try to) get the live output from:
#!/bin/bash
newport=$(shuf -i 22003-22900 -n 1)
newip=$(shuf -i 22003-22900 -n 1)
rm -rf ~/server/*
cp -r /home/user*/ftp/server/mtaserver/serverfiles/* ~/server
sed -i "s/<httpport>[0-9][0-9][0-9][0-9][0-9]<\/httpport>/<httpport>$newport<\/httpport>/g" ~/server/mods/deathmatch/mtaserver.conf
sed -i "s/<serverport>[0-9][0-9][0-9][0-9][0-9]<\/serverport>/<serverport>$newip<\/serverport>/g" ~/server/mods/deathmatch/mtaserver.conf
~/server/mta-server64 2>&1 | tee -a outfile &
mta_pid=$!
echo $mta_pid
sleep 6
pkill $mta_pid
(Note: Because of some technical problems I had to add the first few lines of script which automatically replace the game files with new ones and also replace the existing ports with random ones.)
This script starts the server and tries to log the output of the process. The process is automatically killed after few seconds so there is only one instance of the game server at any given time.
THE ISSUE:
This script only logs the output if there is an error. I still cannot get the live output of the process when it is still running. Maybe this is an issue with the game server but truly believe there should be a way to make it work the way I intend.
I believe you want to use tee command to split the pipe output to log file.
I suggest you read this article and these answers 1 2.
Usually this is enough nohup somecommand > somecommand.log 2>&1 & then, tail -F somecommand.log to follow the logs.
After 2 days I finally figured out a way to make it work (the way I intended to work, without taking in consideration any major security/performance risks).
Reading the comments made me realize I was attacking the wrong point. The stdout of the game server is buffered, thus making it impossible to log it into a log file using the methods I tried when I posted my question At least this is what I came to understand).
I did some research on how to run the application without having the stdout buffered: https://serverfault.com/questions/294218/is-there-a-way-to-redirect-output-to-a-file-without-buffering-on-unix-linux
My code now:
stdbuf -o0 ~/server/mta-server64 >> pipe &
cat < pipe | tee -a outfile &
After creating the named pipe it executes the game server inside that pipe and then appends the stdout into the log file.
The stdbug -o0 command disables the stdout buffering (as noted in the link above).
This works for me and I cannot guarantee it will work for anybody else. I am still not aware if disabling the buffering is a safe approach to my issue but for now it is what I need.

unable to send mail using Mutt

Recently we have migrated Unix OS to new flavor. Since then we couldn't able to send mails. I think we are missing some configuration, Could someone help in resolving the issue.
Command: echo "" | mutt -s "subject" -i /tmp/test.txt -a /tmp/test.txt xxx#gmail.com
Error: Can't stat xxx#gmail.com: No such file or directory
xxx#gmail.com: unable to attach file.
Current OS: Oracle Linux Server release 7.1
Mutt 1.5.21 (2010-09-15)
Please let me know what would be the issue.
Take a look at the man page. It states, that -a takes a list of arguments, which need to be terminated by --.

Sending SNMP2 trap message from Linux command lne

Folks,
I need to use this command (snmptrap) from Linux command line to send my custom message to a trap listener. I need to send the same message in both v1 and v2c depending on user settings.
Here is what I found.
For v1:
snmptrap -v 1 -c Tas hostname 1.3.6.1.4.1.2.3 "" 6 3 1234 s s "This is a test"
The above command works, I see the following message in my listener.
Display of SNMPv1 trap:
community: Tas
enterprise oid: 1.3.6.1.4.1.2.3
..... ibmAgents
agentAddress: hostname
generic-trap: enterpriseSpecific ('00000006'h)
specific-trap: 3 ('00000003'h)
time-stamp: 1234 - 12.34 seconds
varBind oid: OBJECT_IDENTIFIER 1.3.6.1.6
name: snmpV2
value: OCTET_STRING This is a test <- Here is my message.
My listener is a basically an AIX system, running the following command.
clsnmp -c public TRAP
For v2c: I am issuing this command.
snmptrap -v 2c -c "Tas" hostname '1234' 1.3.6.1.4.1.2.3 s s "This is a Test"
The command does not give me any error. But on the listener I see this.
Display of SNMPv2 trap with SNMPV2C security
community: Tas
The message "This is a Test" is not shown.
I am not familiar with snmptrap command - know very little about the protocol also. I have googled the command. My questions...
Is there anything wrong with the v2c command?
Is there anything wrong with my listener command? Let me know if I should something else. I can set up a Linux listener too.
Any suggestion is most welcome. I want to keep it simple - sending a custom message over snmp to a host, v1 and v2c and set up a listener to verify that my commands are working.
Thanks a lot
Tas
The command should look like this:
snmptrap -v 2c -c "Tas" 127.0.0.1 0 1.3.6.1.4.1.2.3 1.3.6.1.6.1.4.1.2.3.1.1.1.1.1 s "This is a Test"
Please note that varbind (OCTET STRING in this case) which is part of your SNMP trap PDU should also have an OID assigned. I've just used random OID for this varbind: 1.3.6.1.6.1.4.1.2.3.1.1.1.1.1
Please also note that you should use proper OIDs based on trap definitions from MIB file(s).
I'd suggest using enterprise class SNMP tools to send out SNMP traps like NetDecision Trap Simulator. This tool sends out traps in full accordance with SNMP standards and based on MIB file definitions:

is it possible to have fetchmail trigger a script when it finds an unread email

I have a GMail account that is set to only receive an email with a certain subject.
I need some linux commands to trigger when it finds unread versions of this email.
The commands are just application calls so I could record my desktop while it runs.
xrandr --size 1360x768
timeout 2h recordmydesktop
xrandr --size 1366x768
I already created a filter on gmail for the email itself, but now I have no idea what to do next. I was told to set up fetchmail that would fetch unread emails in the folder I set-up.(and mark the newly fetched email as read on gmail so it wouldn't read the same mail over and over). This is the script I got, but i'm not sure if ths script does what I need it to.
poll imap.gmail.com protocol IMAP
user "l**********#gmail.com"
password '*****'
folder 'Pic*******'
fetchmail -c
keep
ssl
Next is that I was told to set up a procmail script to trigger my linux commands whenever fetchmail says that it found an unread version of the email. but I'm not exactly sure how to do it. I also need to set up cronjob to have this script trigger every few minutes.
fetchmail is good at downloading messages. procmail is good at filtering messages and executing arbitrary linux commands based on the mail received.
To set up fetchmail to run every 10 minutes as a cronjob and pass the mail it receives to procmail, run crontab -e and add the following line:
*/10 * * * * /usr/bin/fetchmail -N -d0 -f $HOME/.fetchmailrc -m "/usr/bin/procmail $HOME/.procmailrc"
You will need to create a ~/.procmailrc file to do the filtering and executing.
You have not said what commands you want to run. As an example, just for the purpose of giving you ideas, the following stanza from a ~/.procmailrc file selects messages from somebody#example.com with the subject heading New File. The body of the message is then uudecoded and the output from the uudecoder is then untarred:
:0 w
* ^From:.*somebody#example.com
* ^Subject: New File
| uudecode -o /dev/stdout | tar -xzC /var/tmp/
The requirement to run X commands somewhat complicates matters. Cron is not really suitable -- even if your computer is only turned on when you are logged in, the general X architecture is based on privilege separation -- commands running in your current X session have access to the GUI, others do not.
Running Fetchmail and Procmail from within your X session is kind of awkward as well, but if you have a dedicated email account for this task, I don't suppose you mind accidentally losing an email message occasionally.
Thus, instead of a Cron job, I would suggest a simple background script running from your .xsession or similar.
#!/bin/sh
while true; do
fetchmail -N -d0 -f $HOME/.fetchmailrc -m "/usr/bin/procmail $HOME/.procmailrc"
sleep 600
done
The Procmail recipe could look something like
:0
* ^Subject: whatever
| xrandr --size 1360x768 ;\
timeout 2h recordmydesktop; \
xrandr --size 1366x768

Resources