Solved

Copy file across entire network

Posted on 2011-02-16
15
377 Views
Last Modified: 2012-05-11
I would like a script that will get the curent gateway and attempt to copy a file to every machine on that gateway

I will have full admin permissions on the gateway

So if the script determines my gateway was 10.0.10.40

It will try to copy the file to 10.0.10.1 -10.0.10.254

0
Comment
Question by:jfgray
  • 9
  • 5
15 Comments
 
LVL 34

Expert Comment

by:Paul MacDonald
ID: 34908580
What if it's not a class 'C' network?  

Does having "full admin permissions on the gateway" mean you'll have permissions on each machine on the network?
0
 

Author Comment

by:jfgray
ID: 34908608
I meant to say determin my IP address. We are going to move a big file  over our wan to one pc. after the file reaches the PC we would like it to check the C$ share on all computer on the local lan. if it finds a pc the copy the file  

0
 

Author Comment

by:jfgray
ID: 34908943
ok so file is coppied over the way to 1 pc.


The pc's ip address is 192.168.1.40

I would like it to attempt to copy to

192.168.1.1\c$\location
192.168.1.2\c$\location
192.168.1.3\c$\location
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

Author Comment

by:jfgray
ID: 34910383

Curent IP=IP$


I would need to

 \\IP$+1\c$\temp    = \\192.168.1.1\c$\Temp
 \\IP$+2\c$\temp    = \\192.168.1.2\c$\Temp
 \\IP$+3\c$\temp    = \\192.168.1.3\c$\Temp
 \\IP$+4\c$\temp    = \\192.168.1.4\c$\Temp

I would like to do this for the entire  254 IP if no pc is found then move to the next with out erroring
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34913096
Hi, try something like this.

Regards,

Rob.
Set objNetwork = CreateObject("WScript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")

strSourceFile = "C:\Temp\MyFile.txt"
strTargetFolder = "C:\Location\"

strMyIP = ResolveIP(objNetwork.ComputerName)
If InStr(strMyIP, ".") > 0 Then
	If Right(strTargetFolder, 1) <> "\" Then strTargetFolder = strTargetFolder & "\"
	strSubnet = Left(strMyIP, InStrRev(strMyIP, "."))
	For i = 1 To 254
		strDestination = "\\" & strSubnet & i & "\" & Replace(strTargetFolder, ":", "$")
		If Ping(strSubnet & i) = True Then
			If objFSO.FolderExists(strDestination) = True Then
				objFSO.CopyFile strSourceFile, strDestination, True
			End If
		End If
	Next
Else
	MsgBox "Unable to determine IP Address."
End If

Function ResolveIP(computerName)
	Set objShell = CreateObject("WScript.Shell")
	Set objExec = objShell.Exec("ping " & computerName & " -n 1")
	strOutput = objExec.StdOut.ReadAll
	Set RegEx = New RegExp
	RegEx.Pattern = "\[(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\]"
	RegEx.Global = True
	If RegEx.Test(strOutput) Then
		ResolveIP = RegEx.Execute(strOutput)(0).Submatches(0)
	Else
		ResolveIP = "IP Address could not be resolved"
	End If
End Function

Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function

Open in new window

0
 

Author Comment

by:jfgray
ID: 34917460
It keeps stopping at

MsgBox "Unable to determine IP Address."

0
 

Author Comment

by:jfgray
ID: 34917717
Here is a script that gets my ip could we some how use this to determin the IP. In this script ?

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set IPConfigSet = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
 
For Each IPConfig in IPConfigSet
    If Not IsNull(IPConfig.IPAddress) Then
        For i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
            WScript.Echo IPConfig.IPAddress(i)
        Next
    End If
Next
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34921805
Ok, try this.

Regards,

Rob.
Set objNetwork = CreateObject("WScript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")

strSourceFile = "C:\Temp\MyFile.txt"
strTargetFolder = "C:\Location\"

strMyIP = GetMyIP
If InStr(strMyIP, ".") > 0 Then
	If Right(strTargetFolder, 1) <> "\" Then strTargetFolder = strTargetFolder & "\"
	strSubnet = Left(strMyIP, InStrRev(strMyIP, "."))
	For i = 1 To 254
		strDestination = "\\" & strSubnet & i & "\" & Replace(strTargetFolder, ":", "$")
		If Ping(strSubnet & i) = True Then
			If objFSO.FolderExists(strDestination) = True Then
				objFSO.CopyFile strSourceFile, strDestination, True
			End If
		End If
	Next
Else
	MsgBox "Unable to determine IP Address."
End If

Function GetMyIP
	strComputer = "."
	Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
	
	Set IPConfigSet = objWMIService.ExecQuery _
	    ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
	 
	For Each IPConfig in IPConfigSet
	    If Not IsNull(IPConfig.IPAddress) Then 
	        For i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
	            If IPConfig.IPAddress(i) <> "0.0.0.0" Then strIP = IPConfig.IPAddress(i)
	        Next
	    End If
	Next
	GetMyIP = strIP
End Function

Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function

Open in new window

0
 

Author Comment

by:jfgray
ID: 34929638
ok turns out I do have to authenticate

How does this look password is stored in a file called input.txt in the same directory

I am not sure if I am passing admin rights to the entire process any thoughts ?

On Error Resume next
Set oShell = CreateObject("Wscript.Shell")
Authenticate = "runas /user:Domain\User cmd < input.txt"
oshell.Run Authenticate, 0




Set objNetwork = CreateObject("WScript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")

strSourceFile = "C:\Temp\MyFile.txt"
strTargetFolder = "C:\Temp\"

strMyIP = ResolveIP(objNetwork.ComputerName)
If InStr(strMyIP, ".") > 0 Then
      If Right(strTargetFolder, 1) <> "\" Then strTargetFolder = strTargetFolder & "\"
      strSubnet = Left(strMyIP, InStrRev(strMyIP, "."))
      For i = 1 To 254
            strDestination = "\\" & strSubnet & i & "\" & Replace(strTargetFolder, ":", "$")
            If Ping(strSubnet & i) = True Then
                  If objFSO.FolderExists(strDestination) = True Then
                        objFSO.CopyFile strSourceFile, strDestination, True
                  End If
            End If
      Next
Else
      MsgBox "Unable to determine IP Address."
End If

Function ResolveIP(computerName)
      Set objShell = CreateObject("WScript.Shell")
      Set objExec = objShell.Exec("ping " & computerName & " -n 1")
      strOutput = objExec.StdOut.ReadAll
      Set RegEx = New RegExp
      RegEx.Pattern = "\[(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\]"
      RegEx.Global = True
      If RegEx.Test(strOutput) Then
            ResolveIP = RegEx.Execute(strOutput)(0).Submatches(0)
      Else
            ResolveIP = "IP Address could not be resolved"
      End If
End Function

Function Ping(strComputer)
      Dim objShell, boolCode
      Set objShell = CreateObject("WScript.Shell")
      boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
      If boolCode = 0 Then
            Ping = True
      Else
            Ping = False
      End If
End Function
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 34931127
Hi, try this.  It uses the specified username and password to map a connection the IPC$ share of each computer, to gain an access tunnel with those credentials.  Then the file is copied, and the connection is removed.

Regards,

Rob.
Set objNetwork = CreateObject("WScript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")

strSourceFile = "C:\Temp\MyFile.txt"
strTargetFolder = "C:\Location\"
strUser = "domain/admin"
strPassword = "password"

strMyIP = ResolveIP(objNetwork.ComputerName)
If InStr(strMyIP, ".") > 0 Then
	If Right(strTargetFolder, 1) <> "\" Then strTargetFolder = strTargetFolder & "\"
	strSubnet = Left(strMyIP, InStrRev(strMyIP, "."))
	For i = 1 To 254
		strDestination = "\\" & strSubnet & i & "\" & Replace(strTargetFolder, ":", "$")
		If Ping(strSubnet & i) = True Then
			objNetwork.MapNetworkDrive "", "\\" & strSubnet & i & "\IPC$", False, strUser, strPassword
			If objFSO.FolderExists(strDestination) = True Then
				objFSO.CopyFile strSourceFile, strDestination, True
			End If
			objNetwork.RemoveNetworkDrive "\\" & strSubnet & i & "\IPC$", True, False
		End If
	Next
Else
	MsgBox "Unable to determine IP Address."
End If

Function ResolveIP(computerName)
	Set objShell = CreateObject("WScript.Shell")
	Set objExec = objShell.Exec("ping " & computerName & " -n 1")
	strOutput = objExec.StdOut.ReadAll
	Set RegEx = New RegExp
	RegEx.Pattern = "\[(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\]"
	RegEx.Global = True
	If RegEx.Test(strOutput) Then
		ResolveIP = RegEx.Execute(strOutput)(0).Submatches(0)
	Else
		ResolveIP = "IP Address could not be resolved"
	End If
End Function

Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function

Open in new window

0
 

Author Comment

by:jfgray
ID: 34945825
You are THE MAN !
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34946636
Please accept my comment ID:34931127 as the answer, as it appears to have provided the solution.
0
 

Author Comment

by:jfgray
ID: 34946814
oops Thought I did that !
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34947005
Great, thanks for the grade.

Regards,

Rob.
0
 

Author Comment

by:jfgray
ID: 34947010
Thank you for all your help !
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question