Thursday, December 7, 2017

Copying files and installing them on Redhat linux from windows

Continuing from my previous post. We not just have to configure networking on all these redhat linux on VxRack but we also had to copy a lot of packages (nearly 200mb) and install them too. I wish there was ansible but hey where is the fun in that then?.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
$pscp = "C:\pscp.exe"
$plink = "C:\plink.exe"
$target = "192.137.12.101"
$user = 'root'
$pass = 'ambig'
$sourceDir = "C:\tesfolder\"
$targetDir = "/tmp/temp/"

write-host "connecting to $target"
echo y | C:\plink.exe -ssh $user@$target -pw $pass "exit" #store ssh keys
write-host "$targetDir is being created on $target to copy files"
# create directory if doesnt exist
echo y | C:\plink.exe -ssh $user@$target -pw $pass "mkdir -p $targetDir"
# empty that directory if it has files from previous failed operation
$emptyTargetDir = "$targetDir"+"/*"
Write-Host "copying files now"
echo y | C:\plink.exe -ssh $user@$target -pw $pass "rm -rf $emptyTargetDir"
$fullpath = "$user@$target"+":"+"$targetDir"
echo $pass | C:\pscp.exe -r -p $sourceDir $fullpath

line 1-8 are self explantory.
10. it connects and stores the ssh key for the linux machine. in this case redhat
13. creates a $targetDir a if it doesnt exist
17. empties that directory if there are residues from any previous failed operations
19. the stuff that we wanted to do. copy a directory recursively to the destination.
You can also modify the line 13 and use at the end to run the a command to install all files inside the destination directory and may be delete it. I will let you figure that out for yourself. I wanna make your day a bit interesting.
So here is the final version


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
while($true)
{
$pscp = "C:\pscp.exe"
$plink = "C:\plink.exe"
$target = Read-Host "target IP?"
$user = 'root'
$pass = 'Amb!g1'
# the last directory name where the files are is files hence on line 22 the directory name is files
$sourceDir = "C:\Users\Administrator\Desktop\vce\files\files"
$targetDir = "/tmp/"

write-host "connecting to $target"
echo y | C:\plink.exe -ssh $user@$target -pw $pass "exit" #store ssh keys
write-host "$targetDir is being created on $target to copy files"
# empty that directory if it has files from previous failed operation
$emptyTargetDir = "$targetDir"+"/*"
Write-Host "copying files now"
echo y | C:\plink.exe -ssh $user@$target -pw $pass "rm -rf $emptyTargetDir"
$fullpath = "$user@$target"+":"+"$targetDir"
echo $pass | C:\pscp.exe -r -p $sourceDir $fullpath
#install files
C:\plink.exe -ssh $user@$target -pw $pass "rpm -Uivh /tmp/files/*.rpm"
}
So now this altered bit of code copies an entire directory as mentioned in the line 9 to the /tmp directory of the linux and then installs all the files located in /tmp/files whose extention is .rpm. So keep the directory name as files and give thaat path in the line 9 and you dont have to change a bit, unless the files to install are not .rpm but something else like .deb, .bin etc.,