How to capture the output of telnet command in a variable in Shell script - linux

I need to run the telnet command on a remote server using shell script and have to capture the output. When i execute the below, it is not getting completed but instead getting hung. Can someone please advise how to terminate or timeout the telnet command using shell script once it is executed.
telnet_output=`telnet $server $port`
echo "Output is $telnet_output"
I tried writing it to a file as well. But this is also getting hung when executed in remote server.
opfile="telop.log"
telnet_output=`telnet $server $port | tee $opfile`
op=`cat $opfile`
echo "$op"

Try this :
telnet_output="$({ sleep 1; echo $'\e'; } | telnet $server $port 2>&1)"
printf "Output is\n%s\n" "$telnet_output"
echo $'\e' sends an escape character to telnet to terminate it.

Related

How to connect input/output to SSH session

What is a good way to be able to directly send to STDIN and receive from STDOUT of a process? I'm specifically interested in SSH, as I want to do the following:
[ssh into a remote server]
[run remote commands]
[run local commands]
[run remote commands]
etc...
For example, let's say I have a local script "localScript" that will output the next command I want to run remotely, depending on the output of "remoteScript". I could do something like:
output=$(ssh myServer "./remoteScript")
nextCommand=$(./localScript $output)
ssh myServer "$nextCommand"
But it would be nice to do this without closing/reopening the SSH connection at every step.
You can redirect SSH input and output to FIFO-s and then use these for two-way communication.
For example local.sh:
#!/bin/sh
SSH_SERVER="myServer"
# Redirect SSH input and output to temporary named pipes (FIFOs)
SSH_IN=$(mktemp -u)
SSH_OUT=$(mktemp -u)
mkfifo "$SSH_IN" "$SSH_OUT"
ssh "$SSH_SERVER" "./remote.sh" < "$SSH_IN" > "$SSH_OUT" &
# Open the FIFO-s and clean up the files
exec 3>"$SSH_IN"
exec 4<"$SSH_OUT"
rm -f "$SSH_IN" "$SSH_OUT"
# Read and write
counter=0
echo "PING${counter}" >&3
cat <&4 | while read line; do
echo "Remote responded: $line"
sleep 1
counter=$((counter+1))
echo "PING${counter}" >&3
done
And simple remote.sh:
#!/bin/sh
while read line; do
echo "$line PONG"
done
The method you are using works, but I don't think you can reuse the same connection everytime. You can, however, do this using screen, tmux or nohup, but that would greatly increase the complexity of your script because you will now have to emulate keypresses/shortcuts. I'm not even sure if you can if you do directly in bash. If you want to emulate keypresses, you will have to run the script in a new x-terminal and use xdotool to emulate the keypresses.
Another method is to delegate the whole script to the SSH server by just running the script on the remote server itself:
ssh root#MachineB 'bash -s' < local_script.sh

Stop exec and telnet from showing login outputs

I have script that access module locally using the code
exec 3<> /dev/tcp/127.0.0.1/5037 ; echo -e "my command here" >&3 ; cat <&3
In the telnet session, I got the lines
Remote connection from 127.0.0.1:51698 to 127.0.0.1:5037
Closing connection to 127.0.0.1:51698
These outputs appears also with telnet sessions (without script)
How can I stop them as the script is running multiple timer per min and is spamming the console?
You can redirect it in a .txt file.
This might help you:
your_command > log.txt 2>&1
This will leave your console clean while all the logs will be saved on log.txt

Script to check connection every 5 minutes and write result to file (without ping) in LINUX

I need to check my connection to a spesific port every 5 minutes, currently i can't use ping command, so i need other alternative to do this.I want to execute this command in shell script
Can someone help me to show some example for this case?
port=80
ip=8.8.8.8
checkIntervalSecs=5
timeoutSecs=1
while true ; do
if $(nc -z -v -w$timeoutSecs $ip $port &>/dev/null); then
echo "Server is up!"
else
echo "Server is down!"
fi
sleep $checkIntervalSecs
done
This runs until you kill it. For an explanation of the nc command, it is basically taken from SO question #IporSircer suggested.

How can I check to see if an SSH server is listening on a host without actually attempting a login

I am trying to make a bash script which checks to see if a host exists and then attempts to ssh into it if an SSH server is listening on the host. The command would default to telnet if an SSH server is not listening.
What would be the best way to do this? I was thinking about using something like ssh-keyscan to just grab the public key from the ssh server, but ssh-keyscan is not on this jumpserver. Nmap is not on this server either. I'm not able to copy those binaries onto the jump server, nor am I able to compile/build anything on the jumpserver.
What would be the best way to go about checking for an SSH server? I think expect might work, though I would like to avoid using that if possible.
Just check your ability to connect to it, if your bash has the necessary (/dev/tcp) extension; this requires no external commands whatsoever:
if (exec 2>/dev/null 4>/dev/tcp/"$hostname"/22); then
echo "port is open"
else
echo "unable to connect"
fi
Note that your script will need to start with #!/bin/bash, not #!/bin/sh, for this to work.
You can write a shell script and use telnet command to find remote port status
[root#box ~]# telnet remote.example.com 22
Trying 192.168.100.1...
Connected to remote.example.com.
Escape character is '^]'.
SSH-2.0-OpenSSH_5.3
Sample script:
TELNET=`echo "quit" | telnet $SERVER $PORT | grep "Escape character is"`
if [ "$?" -ne 0 ]; then
echo "Connection to $SERVER on port $PORT failed"
exit 1
else
echo "Connection to $SERVER on port $PORT succeeded"
exit 0
fi
I love oneliners :)
if nc "server" "port" </dev/null >/dev/null 2>&1;then echo yeah;else echo no;fi
works on my router and on my rpi

How to retrieve bash shell return code in Windows Server with ssh?

We have a remote bash shell script on a Linux Server.
We have a local Windows Server 2008 box to use ssh to execute the remote shell script.
We cant seem to get the remote return code.
we tried
ssh remote "./remote_shell.sh test" <-- returns 1
echo %errorlevel%
How do we do it right ?
Thanks
If it's really bash; then the return code is $?
ssh remote "./remote_shell.sh test"
echo $?
ssh remote "./remote_shell.sh test; echo $?"
The echo command will print the exit status of the preceding command. It would be necessary to parse the number from the ssh output. You could make that a little easier by tagging the value:
ssh remote "./remote_shell.sh test; echo exit value was: $?"

Resources