4

Id like to plug the following "curl" command into a bash script and run it against a hostfile of ip addresses then display the output as success or failure in an output file.

curl -v telnet 10.10.10.10:22

Is this possible?

3 Answers 3

5

I don't yet have the reputation necessary to comment on the post that claims curl isn't for ssh or telnet. That is not accurate. Curl handles a multitude of protocols, including telnet, ssh, scp, sftp, ftps, and more.

This is the correct syntax for curl:

curl -v telnet://127.0.0.1:22
4

Using the bash builtin to check for open ports may work as well:

#!/bin/bash

host_file=/path/to/file.txt
out_file=/path/to/out.txt

while read -r ip; do
    if timeout 5 bash -c "cat < /dev/null >/dev/tcp/${ip}/22"; then
        echo -e "${ip}\tSuccess"
    else
        echo -e "${ip}\tFailure" 
    fi >> "$out_file"
done < "$host_file"
2
  • I didn't know this one. Is it documented anywhere? Does it also work with IPv6?
    – user147505
    Commented Jun 21, 2018 at 19:55
  • 1
    @Tomasz: It's documented in the bash reference manual 3.6 Redirections. I'm unsure if it works with ipv6.
    – jesse_b
    Commented Jun 21, 2018 at 20:05
3

curl is generally used for HTTP / HTTPS / FTP; not so much for SSH or Telnet.

I'd just use netcat:

testport=22 # 22 for ssh; 23 for telnet; 80 for HTTP; etc.
while read ip; do
    if nc -w2 -z $ip $testport; then
        echo $ip up
    else
        echo $ip down
    fi >> testresults.txt
done < hostlist.txt
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .