LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-22-2019, 01:19 PM   #1
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Rep: Reputation: Disabled
Open telnet and send commands on a single bash line


I am trying to execute a mail read using telnet on pop3 port , however i did not had any luck .
My objective is to connect with telnet using a line in bash , get all the stats from specific username to a file .

Code:
#!/bin/bash
user="USER someuser@someserver"
pass="PASS password"
server="mailserver"
port="110"

telnet $server $port | sleep 1 | echo $user | sleep 1 | echo $pass | sleep 1 | echo STATS | echo QUIT > file >/dev/null 2>&1
echo "Done"
exit 0
Ideas ?
 
Old 11-22-2019, 01:36 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,680

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
I know it is possible using an expect script. I also think you can pipe commands from a file into telnet as if you typed them in during a session. You should be able to do something similar with netcat.

cat commands.txt | telnet

Last edited by michaelk; 11-22-2019 at 01:42 PM.
 
Old 11-22-2019, 03:13 PM   #3
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
using cat to pipe the commands did not worked out .
i tried with nc with also the same issue , cant pipe the commands to the program in execution .

I also did a loop , but it stays hanged , i have to run it with bash -x to see what happened .

Code:
#!/bin/bash
user="USER someuser@someserver"
pass="PASS password"
server="mailserver"
port="110"
while nc $server $port
do
echo $user 
echo $pass 
echo STATS 
echo QUIT 
done > file 
echo "Done"
exit 0
I already saw somewhere on the web an example using expect , however i am trying to use it with less tools as possible .
 
Old 11-22-2019, 04:14 PM   #4
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,725

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
My pop3 server on port 110 doesn't understand the echo command.
I can get a script to hang waiting for the user and pass. A here document didn't work either.
passing the commands from a file didn't work for me either...at least I didn't get the result from the stat

Perhaps the expect script is the only way.
 
Old 11-22-2019, 04:16 PM   #5
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
my last attempt here was to wait for the logged file from telnet or nc to show the commands and then the script execute next command , but somehow i can`t insert the commands into nc or telnet .

Code:
#!/bin/bash
user="USER someuser@someserver"
pass="PASS password"
server="mailserver"
port="110"
dove () {
if [[ "$i" == "1" ]]
then
a1=$(tail -1 connect | grep "+OK Dovecot ready.")
if [[ ! -z "$a1" ]]
then
echo $user
else
sleep 0.5
dove
fi
elif [[ "$i" == "2" ]]
then
a1=$(tail -1 connect | grep "+OK")
if [[ ! -z "$a1" ]]
then
echo $pass
else
sleep 0.5
dove
fi
elif [[ "$i" == "3" ]]
then
a1=$(tail -1 connect | grep "+OK Logged in.")
if [[ ! -z "$a1" ]]
then
echo STAT
else
sleep 0.5
dove
fi
elif [[ "$i" == "4" ]]
then
a1=$(tail -1 connect | grep "+OK")
if [[ ! -z "$a1" ]]
then
echo QUIT
else
sleep 0.5
dove
fi
fi
telnet $server $port | tee connect &
for i in "seq 1 4"
do
dove
done
exit 0
Probably the problem here is that i need to insert an "ENTER" command after every instruction on telnet or nc .

I am able to put this code working but using a different method that i am trying to avoid , witch using ttyecho script , basically i have to open a another shell and then use ttyecho to write all these commands there , but if i can avoid it then would be awesome .

I will wait a few days to see if someone here knows how to do this .

Last edited by pedropt; 11-22-2019 at 04:18 PM.
 
Old 11-22-2019, 04:41 PM   #6
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,725

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Another thought. Perhaps you are "re-inventing the wheel"
There are existing command-line utilities for retrieving email. fetchmail is one. This search pulled up more information.

Just a thought.
 
1 members found this post helpful.
Old 11-22-2019, 07:10 PM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,680

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
Silly me... I would agree if you want to write your own script that expect is the only way but using an existing utility is a better choice.
 
1 members found this post helpful.
Old 11-22-2019, 09:52 PM   #8
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
Quote:
Another thought. Perhaps you are "re-inventing the wheel"
There are existing command-line utilities for retrieving email. fetchmail is one. This search pulled up more information.

Just a thought.
This is not just to grab the email , if it was just to watch my email then i would use roundcube , this script will be used to execute commands that i will send to a specific email address .
So , the script will see every x time if any email exists in some account , case exists then retrieve the email by email to local folder , then delete those emails from the account , and then after parsing the emails on local folder for who sent and witch commands were requested it will execute them on server .

Look , i need to write this script because there is nothing ever done like this , usually by default a sys admin connect to a server using a ssh and execute what he needs , i can also do that , and i do it , but when you are on an android phone somewhere with a small display and with a complex ssh login connection as i have in my server it is impossible , not even mention if your phone is not rooted and you dont have most of the tools to do this job .

so if i send "video add 1.1.1.1" as a subject , then my script will interpret that i want to add the ip 1.1.1.1 to the video streaming location in firewall , but if i write "video rem 1.1.1.1" then it will remove that ip from firewall , and this is just a sample of what will do .

I will always find a way to make it work , maybe i will use expect or tty echo , but only if no solution appear here witch i have some doubts because there is always someone here that knows how to do it , i never saw here anything that did not had a solution .

Anyway , i was looking in this code :
https://www.cyberciti.biz/tips/remov...p3-server.html

This code executes 2 scripts at same time to do the job , 1 is the telnet connection to server and the other are the commands .

UPDATE

Well , expect is out of question , it looks it needs its own interpreter to run , witch means i have to create a new script with the commands to be able to run in expect .

ttyecho won the prize .
http://www.humbug.in/2010/utility-to...minals-ttypts/

After compilation i only need to launch telnet on another tty , like
Quote:
setsid sh -c 'exec telnet $server $port > connect <> /dev/tty9 >&0 2>&1' &
and then from this script everything can be done with these commands :
Quote:
ttyecho -n tty? USER someusername
and etc ....

Last edited by pedropt; 11-22-2019 at 11:02 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
telnet slowness - linux telnet vs windows telnet vahab Linux - Networking 4 01-23-2013 02:25 PM
ssh then telnet vs telnet then telnet sanabani Linux - Networking 1 12-20-2011 07:39 PM
[SOLVED] Bash script to read line by line and execute commands Striketh Programming 4 11-06-2011 11:38 AM
Automae telnet log in and execute commands (expect and send) rajatgarg Linux - Networking 0 06-24-2004 11:37 AM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration