Solved

Copy file across entire network

Posted on 2011-02-16
15
374 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 33

Expert Comment

by:paulmacd
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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Utilizing an array to gracefully append to a list of EmailAddresses
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 this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now