Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Execute program on 2 boxes at the same time

Status
Not open for further replies.

jr8rdt

IS-IT--Management
Feb 9, 2006
59
US
I need to create a script to telnet to 2 or more boxes and then execute a program at the same time. I am talking about the opposite of sequential.

anybody know to do this?

thanks

 
You just need to fork separate processes. If you're doing identical operations on each host, then something like the following should work:
Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]Net::Telnet[/green] [red]([/red][red])[/red][red];[/red]

[black][b]use[/b][/black] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]

[gray][i]# List of hosts for which to run command.[/i][/gray]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]@hosts[/blue] = [red]qw([/red][purple][/purple]
[purple]	192.168.0.2[/purple]
[purple]	192.168.0.3[/purple]
[purple][/purple][red])[/red][red];[/red]

[black][b]my[/b][/black] [blue]@childs[/blue][red];[/red]

[olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$host[/blue] [red]([/red][blue]@hosts[/blue][red])[/red] [red]{[/red]
	[black][b]my[/b][/black] [blue]$pid[/blue] = [url=http://perldoc.perl.org/functions/fork.html][black][b]fork[/b][/black][/url][red]([/red][red])[/red][red];[/red]

	[olive][b]if[/b][/olive] [red]([/red][blue]$pid[/blue][red])[/red] [red]{[/red]
		[gray][i]# Parent[/i][/gray]
		[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]Child [blue]$host[/blue] running under [blue]$pid[/blue], parent [blue]$$\[/blue]n[/purple][red]"[/red][red];[/red]
		[url=http://perldoc.perl.org/functions/push.html][black][b]push[/b][/black][/url] [blue]@childs[/blue], [blue]$pid[/blue][red];[/red]

	[red]}[/red] [olive][b]elsif[/b][/olive] [red]([/red][blue]$pid[/blue] == [fuchsia]0[/fuchsia][red])[/red] [red]{[/red]
		
		[gray][i]# Child[/i][/gray]
		[black][b]print[/b][/black] [red]"[/red][purple][blue]$host[/blue]: Connecting[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
		[black][b]my[/b][/black] [blue]$obj[/blue] = new [maroon]Net::Telnet[/maroon][red]([/red][blue]$host[/blue][red])[/red][red];[/red]
		
		[black][b]print[/b][/black] [red]"[/red][purple][blue]$host[/blue]: More stuff here[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
		[url=http://perldoc.perl.org/functions/sleep.html][black][b]sleep[/b][/black][/url][red]([/red][fuchsia]1[/fuchsia] + [url=http://perldoc.perl.org/functions/rand.html][black][b]rand[/b][/black][/url] [fuchsia]6[/fuchsia][red])[/red][red];[/red]

		[black][b]print[/b][/black] [red]"[/red][purple][blue]$host[/blue]: All Done[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
		[url=http://perldoc.perl.org/functions/exit.html][black][b]exit[/b][/black][/url] [fuchsia]0[/fuchsia][red];[/red]

	[red]}[/red] [olive][b]else[/b][/olive] [red]{[/red]
		[url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]couldnt fork: [blue]$![/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
	[red]}[/red]
[red]}[/red]

[gray][i]# Wait for all children to Finish[/i][/gray]
[olive][b]foreach[/b][/olive] [red]([/red][blue]@childs[/blue][red])[/red] [red]{[/red]
	[black][b]my[/b][/black] [blue]$tmp[/blue] = [url=http://perldoc.perl.org/functions/waitpid.html][black][b]waitpid[/b][/black][/url][red]([/red][blue]$_[/blue], [fuchsia]0[/fuchsia][red])[/red][red];[/red]
	[black][b]print[/b][/black] [red]"[/red][purple]done with pid [blue]$tmp[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

[red]}[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.10.0) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[li]warnings - Perl pragma to control optional warnings[/li]
[/ul]
Other Modules used :
[ul]
[li]Net::Telnet[/li]
[/ul]
[/tt]
- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top