How to send a character through telnet from Linux shell? [duplicate] - linux

This question already has answers here:
Linux tool to send raw data to a TCP server
(3 answers)
Closed 2 years ago.
I manage a running program by sending a command-character through telnet. I usually do it step by step:
telnet localhost 12345
command-character
ctrl+]
quit
Is it possible that I send the command-character from bash directly, or if not, write a bash script to do that?

Do you need to use telnet? I would use the netcat (nc)
Anyway the related thread:
https://unix.stackexchange.com/a/160597
so for example:
echo 'c' | nc localhost 12345
for 'c' use any other character

Related

writing a basic script that pings the current network [duplicate]

This question already has answers here:
Send a ping to each IP on a subnet
(14 answers)
Closed 3 years ago.
I'm having trouble writing a simple script that pings its current network.
I want the output to look the same as a normal ping but I'm having trouble getting to that point
here's the original question from the assignment
-write a short script(using the ping command)to do a "ping" scan of your current network.
any help you can give is much appreciated
I originally tried printf but I wasn't able to even get a decent output so I gave up and started using echo
This is what I have right now
#!/bin/bash
for ping in $(ping -c 4 -v 192.168.1.10)
do echo $ping
done
my teacher said that I would run into two problems with the simple ping script and that I could find the answer in the ping man pages. however I can't seem to find anything to help.
I basically want the script to run as if I just ran a normal ping command and have it display output in the same way.
According to the question from your assignment, I would say what you need to do is invoking ping in all the IP's in your current network (192.168.1.0 in case it is a /24 mask).
What you are trying to do is a simple ping to 192.168.1.10.
If that is the case, why don't you just call ping command in your script?
#!/bin/bash
ping -c 4 -v 192.168.1.10
I think what your teacher really wants is you to find the way of making a loop over all the IP directions in your network.
#!/bin/bash
ping= ping -c 4 -v 192.168.1.10
print $ping
simply get the out put

Best way to communicate between RPi and Ubuntu Server [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
So I'm currently working on an automatic CO / Smoke detector that uses a RasPi as its controller. I want it to send out SMS alerts when a detection event happens. I would like it to work like this.
Sensor(s) > RPi > Main Server > SMS via sendmail
I am wondering how to keep a stable link between the RPi and my Server. I was thinking about using SSH and Ping along with Supervisord to keep everything working, however I also want to be able to test whether the script is working remotely with a command from the main server.
Does SSH and Ping seem like a good way of going about this? And if so, how can I ensure that the script on the RPi hasn't crashed, even if the device is responding to Pings?
Thanks
Edit: It doesn't need to be realtime communication, I was thinking of having it checked every 10s
I was suggesting you use something like this as your script on the Raspberry Pi.
#!/usr/bin/env bash
SVIP=192.168.0.9 # Server IP address
PORT=30000
################################################################################
# This function is called whenever the script exits or dies
################################################################################
function cleanup(){
echo DEBUG: EXIT code running...
# Kill the alive function we started in the background
kill -9 $alivePID
}
trap cleanup EXIT
################################################################################
# This function runs continuously in the background answering "pings"
################################################################################
function alive(){
echo DEBUG: Listening for pings on port $PORT
while :; do
echo "$RANDOM" | nc -l $PORT
echo DEBUG: Ouch.
done
}
################################################################################
# main function of script
################################################################################
# Start the ping responder in the background and note its PID so we can kill it
alive &
alivePID=$!
# Do whatever you want to do here - e.g. read temperatures
sleep 20
Start reading around 4 lines from the bottom. It starts a function called alive() in the background and gets its PID so it can be stopped on exit. The alive() function listens for "pings" using netcat and replies to each one with a random number.
If the script is stopped, it kills the alive() function and so it will no longer reply to pings.
On your server, you can then "ping" the Raspberry Pi by typing this command (replace RASPI_IP with the IP address of your Raspberry Pi):
echo "PING" | nc <RASPI_IP> 30000 ; echo $?
and you will get a random number and an exit code of 0 if the service is running on the Raspberry Pi, whereas you will get an exit code of 1 and no reply if the service on the Raspberry Pi is not running.

How to get the output of a telnet command from bash?

I'm trying to get the list of processes running on my Windows machine from Linux, but I don't get any output when I do it in a script. If I use telnet manually and use the command pslist I get the complete list of processes, but not in my script.
Here is the bash script (minus the variables):
( echo open ${host}
sleep 1
echo ${user}
sleep 3
echo ${pass}
sleep 1
echo pslist
sleep 2
) | telnet
and I simply call it with bash pslist.sh and the output is something like that:
telnet> Trying ip_address...
Connected to ip_address.
Escape character is '^]'.
Welcome to Microsoft Telnet Service
login: my_loginmy_passwordpslistConnection closed by foreign host.
What am I doing wrong ?
telnet is notoriously tricky to script. You may be able to succeed more often if you add a longer still sleep between the commands.
A better approach is to switch to a properly scriptable client, viz. netcat (aka nc). Better still would be to install an SSH server on your Windows box (perhaps for security only make it accessible from inside your network) and set it up with passwordless authentication. Then you can simply ssh user#ipaddress pslist
Terminate each echo with \r character, like this: echo -e "${user}\r"

Writing output from a socket

I have 2 machines A and B.
In machine A, I do
echo "Hello World" > /dev/tcp/{Bs_ip}/12345
In machine B, how do I write a script that runs in the background, listens on port 12345, and prints whatever it receives from port 12345 to stdout?
BTW both machines are running Red Hat Enterprise Linux AS 4.
Thanks
You can do that using netcat:
nc -l -p 123456
If you want to be able to handle multiple connections you will have to use a loop.
You can use netcact (nc) or netcat on steroids, ie socat. I gave a link to the examples section of the man page, so that you can see how powerful socat is.
socat TCP4-LISTEN:12345 -
Should do what you want

Loop exits after first iteration

cat hosts.txt | while read h; do telnet $h; done
When I run this, it telnets to first host which denies the connection but then exits instead of looping over the other hosts in the file.
Why is it exiting the loop after the first host and how can I fix it?
Thanks.
That works just fine in my bash:
$ cat hosts.txt
machine1
machine3
$ cat hosts.txt | while read h; do telnet $h; done
Trying 9.190.123.47...
telnet: Unable to connect to remote host: Connection refused
Trying 9.190.123.61...
telnet: Unable to connect to remote host: Connection refused
However, when I connect to a machine that doesn't refuse the connection, I get:
$ cat hosts.txt
machine2
machine1
machine3
$ cat hosts.txt | while read h; do telnet $h; done
Trying 9.190.123.48...
Connected to machine2.
Escape character is '^]'.
Connection closed by foreign host.
That's because I'm actually connecting successfully and all the other host names are being sent to the telnet session. These are no doubt being used to attempt a login, the remaining host names are invalid as user/password and the session is being closed because of that.
If you just want to log in interactively to each system, you can use:
for h in $(cat hosts.txt); do telnet $h 1023; done
which will not capture the rest of the host names into the first successful session.
If you want to truly automate a telnet session, you should look into a tool such as expect or using one of the remoting UNIX tools such as rsh.
telnet is interactive. what are you actually wanting to do? If you want to automate "telnet" session, you can use SSH
while read -r host
do
ssh <options> <commands> "$host"
done <"hosts.txt"
As mentioned previously telnet is an interactive program that expects input.
At a guess all hosts after the first are consumed as input by the first telnet.
It is not clear what your script is trying to do. Perhaps you need to be clearer on what you are trying to do.

Resources