Change PING script to TELNET script? - cmd

I need to turn this ping script into a telnet script which I'd like to configure the script to telnet to the address (on my separate target list *.txt file) & either:
connect/disconnect - write success results to file
or
fail - write fail results to file,
& go to next record, then end...
HELP? :)
#echo off
cls
echo Ping test in progress...
for /F %%i in (iplist.txt) do ping -n 3 -a %%i >> result.txt
echo .
echo .
echo Result is ready.

You cannot simply replace ping with telnet. For one thing, the telnet shipped with Windows isn't scriptable in the first place, so you'd have to jump through some hoops to make it work in a script. You'd be better off using a telnet that's actually scriptable, like plink from the PuTTY suite. Also telnet clients can talk to arbitrary services, so you need to specify where you want to connect to (a telnet server uses a different protocol than, say, a web server or a mail server).

Related

Using ftp.exe to create some kind of log [duplicate]

I am writing a batch command to send data via FTP. Before sending the actual data I need to find if the FTP server is active/running. How do I check that in batch command?
The server responds with "220 server ready" message when it is connected.
Do something like this:
YourFTPCommand | find /i /v "220 server ready" && goto :ServerNotReady
Explanation:
Pipe the output of your FTP command to FIND
Do a case insensitive (/i) search for output that does not contain (/v) the string "220 server ready"
Go to someplace if such a string is found (&&)
I do not think there's a reliable way to do this with Windows ftp.exe.
It blindly keeps running the commands, no matter if connection or previous commands succeeded.
It won't even report the result via exit code.
All you can do is to parse the ftp.exe output.
You should better use a 3rd-party FTP client.
For example with WinSCP scripting you can use:
#echo off
winscp.com /log=ftp.log /command ^
"open ftp://user:password#example.com/" ^
"put c:\local\path\file.txt" ^
"exit"
If connection (the open command) fails, WinSCP won't execute the following commands (the put).
See also Converting Windows FTP script to WinSCP FTP script.
(I'm the author of WinSCP)

run shell script on multiple remote machines

WARNING: newbie with bash shell scripting.
I've created the script to connect to multiple remote machines, one by one, check if a certain file has certain text already in it, if it does, move to the next machine and make the same check, if not, append the text to the file, then move to the next machine.
Currently, the script connects to the first remote machine but then does nothing when it connects. If I type exit to close the remote machine's connection, it then continues running the script, which does me no good because I'm not connected to the remote machine any longer.
on a sidenote, I'm not even sure if the rest of the code is correct, so please let me know if there are any glaring mistakes. This is actually my first attempt at writing a shell script from scratch.
#!/bin/bash
REMOTE_IDS=( root#CENSOREDIPADDRESS1
root#CENSOREDIPADDRESS2
root#CENSOREDIPADDRESS3
)
for REMOTE in "{$REMOTE_IDS[#]}"
do
ssh -oStrictHostKeyChecking=no $REMOTE_IDS
if grep LogDNAFormat "/etc/syslog-ng/syslog-ng.conf"
then
echo $REMOTE
echo "syslog-ng already modified. Skipping."
exit
echo -
else
echo $REMOTE
echo "Modifiying..."
echo "\n" >> syslog-ng.conf
echo "### START syslog-ng LogDNA Logging Directives ###" >> syslog-ng.conf
echo "template LogDNAFormat { template(\"<key:CENSOREDKEY> <${PRI}>1 ${ISODATE} ${HOST} ${PROGRAM} ${PID} ${MSGID} - $MSG\n\");" >> syslog-ng.conf
echo "template_escape(no);" >> syslog-ng.conf
echo "};" >> syslog-ng.conf
echo "destination d_logdna {" >> syslog-ng.conf
echo "udp(\"syslog-a.logdna.com\" port(CENSOREDPORT)" >> syslog-ng.conf
echo "template(LogDNAFormat));" >> syslog-ng.conf
echo "};" >> syslog-ng.conf
echo "log {" >> syslog-ng.conf
echo "source(s_src);" >> syslog-ng.conf
echo "destination(d_logdna);" >> syslog-ng.conf
echo "};" >> syslog-ng.conf
echo "### END syslog-ng LogDNA logging directives ###" >> syslog-ng.conf
killall -s 9 syslog-ng
sleep 5
/etc/init.d/syslog start
echo -
fi
done
Great question: Automating procedures via ssh is a laudable goal.
Let's start off with the first error in your code:
ssh -oStrictHostKeyChecking=no $REMOTE_IDS
should be:
ssh -oStrictHostKeyChecking=no $REMOTE
But that won't do everything either. If you want to ssh to run a set of commands, you can, but you'll need to pass those commands in a string as an argment to ssh.
ssh -oStrictHostKeyChecking=no $REMOTE 'Lots of code goes here - newlines ok'
For that to work, you'll need to have passwordless ssh configured ( or you'll be prompted for credentials ). This is covered in steps 1) and 2) in Alexei Grochev's post. One option for passwordless logins is to put public keys on the hosts you want to manage and, if necessary, change the IdentityFile in your local ~/.ssh/config ( you may not need to do this if you are using a default public / private key pair ) .
You've got to be careful with ssh stealing your stdin ( I don't think you'll have a problem in your case ). In the cases that you suspect that the ssh command is reading all your stdin input, you'll need to supply the -n parameter to ssh ( again, I think your code does not suffer from this problem, but I didn't look to carefully ).
I agree with tadman's comment, that this is a good application for Ansible. However, I wouldn't learn Ansible for this task alone. If you intend on doing a lot of remote automation, Ansible would be well worth your time learning and applying to this problem.
What I would suggest is pssh and pscp. These tools are awesome and take care of the "for" loop for you. They also perform the ssh calls in parallel and collect the results.
Here are the steps I would recommend:
1) Install pssh (pscp comes along for the ride).
2) Write your bash program as a separate file. It's so much easier to debug and update , etc. if your program isn't in a bunch of echo statements. Those hurt. Even my original suggestion of ssh user#host 'long string of commands' is difficult to debug. Just create a program file that runs on the remote hosts and debug it on the remote host ( as you can ) .
3) Now go back to your control host ( with that bash program ). Push it to all of the hosts under management with pscp. The syntax is as follows:
# Your bash program is at <local-file-path>
chmod +x <local-file-path>
pscp -h<hosts-file> -l root <local-file-path> <remote-file-path>
The -h option specifies a lists of hosts. So the would look like this:
CENSOREDIPADDRESS1
CENSOREDIPADDRESS2
CENSOREDIPADDRESS3
Incidentally, if you did not set up your public/private keys, you can specify the -A parameter and pscp and pssh will ask you for the root user's password. This isn't great for automation, but if you are doing a one time task it is a lot easier than setting your public/private keys.
4) Now execute that program on the remote hosts:
pssh -h<hosts-file> -i <remote-file-path>
The -i parameter tells pssh to wait for the program to execute on all hosts and return the stdout and stderr results in line.
In summary, pssh/pscp are GREAT for small tasks like this. For larger tasks, consider Ansible ( it basically works by sending python scripts over ssh and executing them remotely ). Puppet/Chef are way overkill for this, but they are fantastic tools for keeping your data center in the state that you want it in.
you can do this with puppet/chef.
but this can also be done with bash if you have patience. I don't want to give actual code because I think its best to understand logic first.
however since you asked, here is the flow you should follow:
make sure you have keys setup for all machines
create config with all the servers
put all servers into an array
create a loop to call each box and run your script (before you will have to scp the script to the home dir on the box so make sure it good to run)
you can also do what you want better imho and that's how I've done it before.
1) make a script to read your file and put it on cron to run every minute or whatever time is best, say echo out #size of file to a log file
2) all servers will have those scripts running so now you just run your script to fetch the data across all servers (iterate through your array of servers in your config file)
^^ that right there can also be done with php where you have an instance of a webserver reading the file. you can also create a web server with bash...since its only for 1 task its not terribly insane.
have fun.

Writing pings to files batch

Im looking for the best way to write two instances of cmd ping commands to 2 seperate text files using a batch file.
Here is what I have but its not writing to the file im wondering why.
start cmd /k ping 8.8.8.8 -t >> c:\troubleshoot_connection_google.txt
start cmd /k ping 192.x.x.x -t >> c:\troubleshoot_connection_gateway.txt
Thanks for the help.
The problem with your commands, appart from the point that the root folder of the drive is usually not writteable, is when the redirection to the output file is done.
As you have it written, what is being redirected to the file is the output of the start command, but if you need to redirect the output of the ping command, the redirection must be part of the started command
start "" "cmd /c ping 8.8.8.8 -t >> c:\troubleshoot_connection_google.txt"

Telnet inside a shell script

How can I run telnet inside a shell script and execute commands on the remote server?
I do not have expect installed on my solaris machine because of security reasons.
I also do not have the perl net::telnet module installed.
So with out using expect and perl how can I do it?
I tried the below thing but its not working.
#!/usr/bin/sh
telnet 172.16.69.116 <<!
user
password
ls
exit
!
When I execute it, this is what I am getting:
> cat tel.sh
telnet 172.16.69.116 <<EOF
xxxxxx
xxxxxxxxx
ls
exit
EOF
> tel.sh
Trying 172.16.69.116...
Connected to 172.16.69.116.
Escape character is '^]'.
Connection to 172.16.69.116 closed by foreign host.
>
Some of your commands might be discarded. You can achieve finer control with ordinary script constructs and then send required commands through a pipe with echo. Group the list of commands to make one "session":-
{
sleep 5
echo user
sleep 3
echo password
sleep 3
echo ls
sleep 5
echo exit
} | telnet 172.16.65.209
I had the same issue...however, at least in my environment it turned out being the SSL Certificate on the destination server was corrupted in some way and the server team took care of the issue.
Now, what I'm trying to do is to figure out how to get a script to run the exact same thing you're doing above except I want it to dump out the exact same scenario above into a file and then when it encounters a server in which it actually connects, I want it to provide the escape character (^]) and go on to the next server.

Checking whether a server is up or not using batch file?

I need to check whether a server is up or not?
If down then i need to send an email
And this task should be repeated in every 30mins.
I have to do this using batch file.
This batch file will get you most of the way there. You'll have to use blat or something similar or Windows script to send the email. Use the task scheduler to call the batch file every 30 minutes.
checkserver.bat:
#echo off
ping -n 1 %1 > NUL
IF ERRORLEVEL 0 (echo "Up -- Don't send email.") ELSE echo "Down -- Send email."
Call it like so:
C:\>checkserver 127.0.0.1
"Up -- Don't send email."
C:\>checkserver 128.0.0.1
"Down -- Send email."
You can try to access one of its filesystem shares or ping it if you know its IP address. That would be the easiest way and both of them doable from CMD.
To check whether server is up, you can use the ping command. To send email, you can download email tools like blat etc. To repeat every 30mins, set it up using task scheduler.
You can check whether the machine replies to ping. But because ping on Windows doesn't return a useful %errorlevel%, you need to pipe it's output through find.
Example:
ping -4 -n 2 YourIPorHostname | find "TTL=" >NUL && echo OK
or
ping -4 -n 2 YourIPorHostname | find "TTL=" >NUL || echo No reply
To send an email, you need some additional program.
(if you also have a Unix machine on your network, the whole thing would be much easier from there, since the "crontab" scheduler automatically sends emails)

Resources