Telnet - Connect and send message in one line - bash

I'm looking for a one-line command that will connect to a telnet server (no authentication needed, it will accept any connection) and send text to the server after connection.
If this is not possible, is it possible to write a script that will do the same thing?

With bash: replace "servername" by your telnet server:
echo "Hello" > /dev/tcp/servername/23

Sounds like you can use Netcat on the telnet port.
An example would go like so:
echo -n 'some string to telnet' | nc <server> 23

Related

How to get messages sent to http://localhost:7777/ through a Linux command line?

I would like to check messages sent to my 7777 port, namely, http://localhost:7777/. I am on Linux and want to do it through command line. Which command line should I use?
How about use the tool netcat? Try nc localhost 7777. Does it work?
Simple command :- nc -vz www.google.com 7777
nc -vz (website) (port number)

Set source port when sending udp message with echo?

I'm using
echo "This is my data" > /dev/udp/192.168.0.92/7891
in a bash script to test udp communication with a device. The device then returns the message. I've seen via wireshark that my source port is always changing.
Anyway, I want to set the source port, can I do that?
Use netcat nc and its -p option to set the source port.
As said in the netcat man page
-p source_port
Specify the source port nc should use, subject to privilege restrictions and availability.
Then use it like this:
echo "This is my data" | nc -u -p 50000 192.168.0.92 7291

Expect: How can I open a telnet session from an ssh session?

I want to write a script that automatically connects me via SSH to a given IP, and after that opens a telnet session from where it just connected.
My expect code till now:
# $1 = ssh root#111.111.111.111
# $2 = password
# $3 = telnet 123.123.123.123 10023
(expect -c "
set timeout 20
spawn $1
expect {
\"Password:\" { send \"$2\r\" }
timeout { send_error \"\nTimeout!\n\"; exit 1; }
}
spawn $3
interact
" )
My problem is that I cannot spawn the telnet in the ssh session, the script is just "telnetting" from my home directory. Maybe there is a way with session ids, but I could not find helpful information.
Would be nice if someone of you could suggest some solution or workaround,
thanks in advance and please excuse my bad English skills :)
Edit:
What helped with my problem, was:
(expect -c "
set timeout 20
spawn ssh root#server telnet server2
expect {
\"Password:\" { send \"$2\r\" }
timeout { send_error \"\nTimeout!\n\"; exit 1; }
}
interact
" )
I'd have a bit of a different approach for you, easier maybe.
As I understand it:
You want to SSH into a server of yours and from there telnet to another place
Did you consider using key based authentication with SSH ?
For this approach you would add your identity key to .ssh/authorized_keys on the remote server.
Here is an example which uses expect from command line, connects to a SSH server using a key file and from there connects to a mailserver and sends "HELO test"
Tested it on my servers, works
expect -c 'expect "\n" {eval spawn ssh -i identity_file my.sshserver.com telnet mail.anotherserver.com 25; expect "SMTP" {send "HELO test\r\n"};interact}'
you can also add a timeout option to ssh (-oConnectTimeout)
It will connect to the server and call the telnet command, so you would have an open SSH session which has telnet already connected.
The script waits for an initial ENTER from you to start.
As you also asked for a Workaround, here is one: You can use ssh port forwarding
ssh -f -N -n root#111.111.111.111 -L 10024:123.123.123.123:10023
[wait for connecting]
telnet localhost 10024
Here, ssh will open a connection and go into background. the local ssh client will listen on port 10024 and redirect all traffic to 123.123.123.123 port 10023. As long as this ssh instance is running, you can open and use telnet sessions (From the initial location).

Using pipe in Windows Command line

I'm trying to pipe a string into another command. The following snippet shows an example which does not work: It only starts Telnet, but does not "type" open 1.2.3.4 into the running telnet application.
echo open 1.2.3.4|telnet
How can I "write" open 1.2.3.4 after Telnet has been started?
(I know that there is telnet 1.2.3.4 - this is only a sample...)
Telnet in Windows is not scriptable. This free tool is.
Telnet Scripting Tool v.1.0
by Albert Yale ay#aci.qc.ca

Output from remote server at command prompt

I can connect to remote redis using the telnet command and get the value of "mytest" key. The following is working as expected.
[root#server shantanu]# telnet 10.10.10.100 6379
Trying 10.10.10.100...
Connected to 10.10.10.100 (10.10.10.100).
Escape character is '^]'.
get mytest
$14
this is first
But how do I use it in shell script?
I am used to connect to mysql using the following:
msyql -h10.10.10.100 -uroot -proot#123 -e"show databases"
Is a simialar syntax available for redis?
You can alternatively use redis-cli, included in redis
$ ./src/redis-cli --raw GET key
test
I don't know telnet, but with ssh you can:
ssh user#server "command arg1 arg2 ..."
for example
ssh user#server "ls -ltr | tail"
I would use a tool like wget, which is designed to get content from websites, and is very configurable and automaetable. You might even be able to get away with
export myTestKey=`echo "get mytest" | telnet 10.10.10.100 6379`
If the conversation needs to be more complex than that, I would use telnet in combination with expect, which is designed for trigger and response conversations.

Resources