From the following nc command,i would like to redirect source-server.fqdn name to the output file. By default nc command is not returning the source-server.fqdn in the output.
nc -zv -s source-server.fqdn dest-server.fqdn 1234 >> file.txt 2>&1
nc command does not provide the source server's fully qualified domain name (FQDN) in its output.-s option in nc is used to specify the source IP address, not FQDN.
if you want to include the source server's FQDN in the output, you would need to use a different command. for example, you could use nslookup to get the FQDN of the source server and then use echo to include it in the output. Here's an example:
echo "Source server FQDN: $(nslookup source-server-ip | grep name | awk '{print $4}')" | tee -a file.txt
nc -zv -s source-server-ip dest-server.fqdn 1234 >> file.txt 2>&1
here, nslookup source-server-ip | grep name | awk '{print $4}' is used to get the FQDN of the source server. tee -a file.txt command is used to append the output to the file.
echo source-server.fqdn >file.txtbefore running yournccommand?{ echo source-server.fqdn; nc -zv -s ...; } >>file.txt