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

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

Related

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

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.

Shell script to find out if a port is being listened to using netstat?

I have a server where a certain port (9999) is being listened to by a PHP socket server. What happens is that devices can connect to the socket and send messages. The code works fine right now, however, I noticed that the socket would sometimes close or die off, and I need to be able to put it back up online automatically without me having to log in and run it again.
What I'm thinking of is writing a Shell script that would check via netstat if there's a process running on port 9999, and if there's none, the script would trigger the PHP socket server to go online again. This Shell script would then be called by Cron every 1 or 2 minutes to check if the PHP socket is running.
I have bare minimum knowledge about Shell scripting, and so far, this was the only other thing I wrote in Shell:
#!/bin/sh
if pidof "my process name here" >/dev/null; then
echo "Process already running"
else
echo "Process NOT running!"
sh /fasterthancron.sh
fi
I think I should be able to reuse this code to some degree but I'm not sure what to replace the if condition with.
I have the idea that I'm supposed to use netstat -tulpn to figure out what processes are running, but I'm not sure how to filter through that list to find if a specific process is running on port 9999.
If you use netstat -tlpn (or its replacement ss -tpln), you can grep for 9999 and look for processes listening on it under "Local Address".
ss -tpln | awk '{ print $4 }' | grep ':9999'
Alternatively, if you can, use netcat or telnet instead e.g. nc -v localhost 9999.
if echo -n "\cD" | telnet ${host} ${port} 2>/dev/null; then
...
fi
I wrote something similar a while back: docker-wait
This was forked from aanand's docker-wait
You can use famous netstat -tupln with a simple if/else logic to do this.
if [ -z "$(sudo netstat -tupln | grep 9999)" ];
then
echo notinuse;
else
echo inuse;
fi

Linux shell: why is "open" required in this telnet command

I tried to process telnet output in bash and i stumbled upon this syntax to send telnet commands to a server
( echo open $host $port
sleep 1
echo $cmd1
sleep 1
) | telnet
What i would like to know is why the "open" command is required and why
( echo $host $port
...
) | telnet
results in a "?Invalid command" error.
...because a hostname is not a valid command name? There is a big difference between
$ telnet host port
and
$ telnet
telnet> host port
Where the latter is what your echo command is effectively doing.
The one-liner automatically runs an open command, so it is basically equivalent to this:
$ telnet
telnet> open host port
But I'm not at all sure why you wouldn't just run telnet host port in the first place.

Checking remote port status in bash script [duplicate]

This question already has answers here:
Test if remote TCP port is open from a shell script
(17 answers)
Closed 6 years ago.
I need check port on the remote server in bash script before script will continue.
I search here and on the internet, but I can´t find answer which works for me.
I´m using RHEL 7.2 virtual machine so I don´t have -z parameter in nc command or /dev/tcp/ thing.
Also nc remote.host.com 1284 < /dev/null don´t work, because every time I get exit code 1.
Basically I need something like that:
/bin/someting host port
if [ $? -eq 0 ]; then
echo "Great, remote port is ready."
else
exit 1
fi
How about nmap?
SERVER=google.com
PORT=443
state=`nmap -p $PORT $SERVER | grep "$PORT" | grep open`
if [ -z "$state" ]; then
echo "Connection to $SERVER on port $PORT has failed"
else
echo "Connection to $SERVER on port $PORT was successful"
exit 1
fi
Please note You have to install nmap.
yum install nmap #Centos/RHEL
apt-get install nmap #Debian/Ubuntu
Our you can compile and install from source.
You can do this with Bash itself, using it's built-in /dev/tcp device file.
The following will throw a connection refused message if a port is closed.
: </dev/tcp/remote.host.com/1284
Can be scripted like this:
(: </dev/tcp/remote.host.com/1284) &>/dev/null && echo "OPEN" || echo "CLOSED"
Details of /dev/tcp in bash reference manual: https://www.gnu.org/software/bash/manual/html_node/Redirections.html

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"

Resources