Friday, December 8, 2017

Configuring network on Redhat linux servers with windows powershell

Okay we had a lot of redhat servers on which we had to alter some files. My earlier resolutions always has been push one command at a time to ssh based servers (esxi or linux) but why not use pscp to upload a shell script with all the commands in it and run one plink command to execute and move on to next server. Finally we were able to do that on our VxRack.

1
2
3
4
5
6
7
$scriptfile = "$PSScriptRoot\ifcfg_p9p9_ip_update.sh"
$targetDir = "/tmp/temp/"
$targetFile = "/tmp/temp/ifcfg_p9p9_ip_update.sh"
$fileToChange = "/etc/sysconfig/network-scripts/ifcfg-p9p9"
$target = "192.16.16.16"
$user = 'root'
$pass = 'ambig'

So there are the variables of this powershell script.
1. the location of the shell script on my local machine
2. the directory on the linux machine where the script will get uploaded
3. the shell script's full path once it gets uploaded to the target
4. shell script edits this file
5. the target linux server
6,7 credentials for linux server


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#store keys
echo y | C:\plink.exe -ssh $user@$target -pw $pass "exit" #store ssh keys
C:\plink.exe -ssh $user@$target -pw $pass "mkdir -p $targetDir"
#copy script file to server
$fullpath = "$user@$target"+":"+"$targetDir"
echo $pass | C:\pscp.exe -r -p $scriptfile $fullpath
#make script file executible on redhat
C:\plink.exe -ssh $user@$target -pw $pass "chmod +x $targetFile" #store ssh keys
# execute script on redhaat
C:\plink.exe -ssh $user@$target -pw $pass "$targetFile"
#C:\plink.exe -ssh $user@$target -pw $pass "rm -rf $targetFile"
C:\plink.exe -ssh $user@$target -pw $pass "cat $fileToChange"

2. when you connect to linux server first you have to store the ssh keys to your local machine.
3. create the directory on the target machine to copy the shell script
6. pscp needs the password to be entered when you upload files. here i am copying the shell script to the remote linux machine
8. send a command via plink to linux machine to make that shell script executible
10. run the shell script
12. print out the contents of the newly edited file to make sure it has been edited

Basically we used this method to edit/update 4 ethernet configuration files on many redhat linux servers.
Note: you should have the plink.exe pscp.exe copied to your c drive. the .sh or shell script should be in the same directory as this powershell script.