*ix script to check port of client server and return output

Hello EveryOne,

I am new to *ix. some could help to write a script.
Problem :- Have to ssh to so many client and check port or filesystem usage, so thinking to automate using script.

What i Need:- when i run script on my Launchpad server, it should

Should ask and SSH to user provided Server -> Authenticate User password-> Run port check commands (netstat) or Filesystem usage (df -k) -> return output to Launchpad server server screen(where i ran that script)

if fails to do- Return a valid reason( file not found) or (server not available)

please let me know if i miss something..

Kindly help:-

Br,

You shouldn't use a password to log on to your servers from the launch pad system. Use exchanged keys instead.

You might want to design your script so that it reads from a list all the host names it has to work on. This way you can design a "template script" which provides the base logic of cycling through all the servers in the list, contacting them, writing the results in a log, etc..

You will need some logic to handle servers which are unreachable for some reason (not pingable, key not exchanged, network not reachable, ...). If the list of servers is really long you probably have always one or the other which is unreachable in the moment your run your script, so it should better be able to deal with such a condition until you don't want to do an awful lot of handiwork during its run. "Deal with it" meaning something as simple as writing the hostname in an output file "hosts.failed" for instance.

Run your tasks separately, so that you can handle all the logic on the launch pad server. It is easier to handle this locally:

if [ $(ssh -qo BatchMode user@host "/some/command") != "success" ] ; then
     ssh -qo BatchMode user@host "do_this"
fi

then to handle it remote:

ssh -qo BatchMode user@host "if [ \"$(/some/command)\" != \"success\" ] ; then ; do_this ; fi"

or, even more troublesome, first "roll out" all sorts of scripts to the remote servers and only then execute these from the central script. Chances are you introduce a lot of inconsistencies though this error-prone way, where older versions of your scripts interfere with newer versions, etc..

Ideally you can parallelize the work on remote servers by creating a special script which handles exactly one remote host and call that from a control script looping through the list of hosts. Put the calls to this script in the background and use "wait" and some counter to limit the number of background processes running in parallel. (You might want to avoid hitting the limit on processes on your launch pad box.)

I hope this helps.

bakunin

Thanks for suggestion, But

In our work environment i have hell lot of servers. In a day i get 20 to 30 tickets or requests saying a port check have been failed on so and so(6445 or 6996) node. so everytime its new server(even i dont know there names)
script should prompt.
enter username
enter node name;
enter password
enter port number to check
check port (listening or established or nothing) and show me the result.
if the port is not up or server is unreachable show fixed error(try manually human :-)))

please help... i am fed up of doing same lengthy steps to check the tickets.

Br,

I guess you can do something like below:-

echo -e "Enter host: \c"
read host

echo -e "Enter user: \c"
read user

echo -e "Enter port: \c"
read port

ssh ${user}@${host} "netstat -a | grep ${port}; some_other_cmd"

Since you do not have the ssh-keys setup, the script will ask for password and execute the command. I hope this helps.

1 Like