LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-27-2009, 03:33 PM   #1
madpear
LQ Newbie
 
Registered: Oct 2009
Posts: 7

Rep: Reputation: 0
Creating a script to run netstat on remote servers and return results


I would like to write a script that can run a "netstat -aa | grep [subnet]" command on a known list of servers identified by their IPs, then return the output back to the machine that is calling the script. My first goal is to get this working for all the remote servers running Linux, though eventually I hope to make it work with Windows Servers also. I have been able to find a few different strategies for this type of thing, but I am not certain which will work best.

Please let me know of what you think may be the best solution for this script.
 
Old 10-27-2009, 07:38 PM   #2
TBKDan
Member
 
Registered: Dec 2005
Location: NY, USA
Distribution: Ubuntu
Posts: 44

Rep: Reputation: 16
I would use SSH to connect and execute the commands on the Linux servers and something like winexe (part of the samba project - see here) to run the commands on the Windows servers.

Linux - put all the linux servers in a file called linux_list and set up key-based SSH authentication.
Code:
for SERVER in `cat linux_list`
do
ssh user@${SERVER} "netstat -aa" >> ${SERVER}.txt
done
Windows - create a user that can access each of the systems remotely and turn on administrative shares if not already enabled. Put all servers in windows_list
Code:
for SERVER in `cat windows_list`
do
winexe -U DOMAIN\\user //${SERVER} "netstat -aa" >> ${SERVER}.txt
done
 
Old 10-28-2009, 10:01 AM   #3
madpear
LQ Newbie
 
Registered: Oct 2009
Posts: 7

Original Poster
Rep: Reputation: 0
An important feature I failed to mention, the script should be written for Windows. I've read something about being able to use Putty for this purpose (plink?), but I can't seem to find a place that can state the basics of sending commands via this and receiving back their output.
 
Old 10-29-2009, 09:44 AM   #4
TBKDan
Member
 
Registered: Dec 2005
Location: NY, USA
Distribution: Ubuntu
Posts: 44

Rep: Reputation: 16
Quote:
Originally Posted by madpear View Post
An important feature I failed to mention, the script should be written for Windows. I've read something about being able to use Putty for this purpose (plink?), but I can't seem to find a place that can state the basics of sending commands via this and receiving back their output.
Well that changes things a bit... you'll need to get plink and psexec (from the pstools package) and modify the paths below.

Linux
Code:
for /F %S IN (linux_list.txt) DO "c:\program files (x86)\putty\plink.exe" user@%S "netstat -aa" >> %S.txt
Windows
Code:
for /F %S IN (windows_list.txt) DO "c:\pstools\psexec.exe" \\%S %windir%\system32\netstat.exe -aa >> %S.txt
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to call Shell Script on a remote server with remote servers env variables need Linux - Server 1 10-14-2009 08:37 PM
Using PHP, run bash script and see results on Server's Monitor xmrkite Linux - Software 6 10-17-2006 01:28 PM
Question about netstat results JaseP Linux - Security 4 11-18-2004 12:09 PM
Netstat results. Imyrryr Linux - Networking 2 07-02-2004 10:00 PM
Weird results with netstat nekromancer Linux - Networking 3 02-17-2004 01:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration