Saturday, December 23, 2017
Thursday, December 21, 2017
Monday, December 18, 2017
Saturday, December 16, 2017
getting started with amazon AWS with Powershell
You first need windows and I got it from here
https://aws.amazon.com/powershell/
Now I do not want to show you my credentials to log in to my aws so that you can spin up your own workload on my credit card so I downloaded the csv file containing the access key ID and secret key.
1. path to the credentials
2. import the csv file contents
3. extracting the access key ID and storing it as a variable to use later
4. extracting the secret access key from the csv
5. loging in to my AWS and storing it as a profile
Now you see that when you run Get-AWSRegion I get a list of all the available regions.This is the time you want to play with it. Whenever you start automating or scripting stuff, you should and must always start only with getting information. Generating reports etc., which will safely allow you to get a hang on the eco system. You do not want to accidentally do something which you will regret for life.
https://aws.amazon.com/powershell/
Now I do not want to show you my credentials to log in to my aws so that you can spin up your own workload on my credit card so I downloaded the csv file containing the access key ID and secret key.
1 2 3 4 5 | $credCsv = 'C:\Users\<username>\Foldename\aws_accessKeys.csv' $csv = Import-Csv $credCsv $keyId = $csv.'Access key ID' $skey = $csv.'Secret access key' Set-AWSCredential -AccessKey $keyId -SecretKey $skey -StoreAs batman |
1. path to the credentials
2. import the csv file contents
3. extracting the access key ID and storing it as a variable to use later
4. extracting the secret access key from the csv
5. loging in to my AWS and storing it as a profile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | PS C:\Users\gaju> $credCsv = 'C:\Users\<username>\Foldername\aws_accessKeys.csv' $csv = Import-Csv $credCsv $keyId = $csv.'Access key ID' $skey = $csv.'Secret access key' Set-AWSCredential -AccessKey $keyId -SecretKey $skey -StoreAs batman Get-AWSRegion Region Name IsShellDefault ------ ---- -------------- ap-northeast-1 Asia Pacific (Tokyo) False ap-northeast-2 Asia Pacific (Seoul) False ap-south-1 Asia Pacific (Mumbai) False ap-southeast-1 Asia Pacific (Singapore) False ap-southeast-2 Asia Pacific (Sydney) False ca-central-1 Canada (Central) False eu-central-1 EU Central (Frankfurt) False eu-west-1 EU West (Ireland) False eu-west-2 EU West (London) False sa-east-1 South America (Sao Paulo) False us-east-1 US East (Virginia) False us-east-2 US East (Ohio) False us-west-1 US West (N. California) False us-west-2 US West (Oregon) False |
Now you see that when you run Get-AWSRegion I get a list of all the available regions.This is the time you want to play with it. Whenever you start automating or scripting stuff, you should and must always start only with getting information. Generating reports etc., which will safely allow you to get a hang on the eco system. You do not want to accidentally do something which you will regret for life.
Amazon AWS automation with python
I hate the fact that AWS GUI keeps changing (atleast now, may be not that much in future) so fast that you wonder where some stuff is which you used yesterday. It is time for me to get my hands dirty with aws via text, that is cli, python etc.,
you need the following though
you will notice that it asks for a key. Go to AWS IAM and create a new user and do not use root user. give necessary permissions and rights to this user.
click on the user
security credentials tab
generate access key
download the csv file
use the details of the csv file when you run aws configure
leave the default region name empty
default output format, set this to json (yes I like json over others, blow me :p)
Note : you cannot recover a lost key. you can only generate a new one. delete the lost access key id and key pair and keep only the one which are in use and relevant.
Run the below now to list all the aws regions.
you need the following though
- aws account (duhh...of course you should)
- python and pip installed and working
- aws cli
https://aws.amazon.com/cli/ - boto3 (pip install boto3
or if you are a not python aficionado or pythonista then you can choose your own diet her
http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html
Now do the following
aws configure
you will notice that it asks for a key. Go to AWS IAM and create a new user and do not use root user. give necessary permissions and rights to this user.
click on the user
security credentials tab
generate access key
download the csv file
use the details of the csv file when you run aws configure
leave the default region name empty
default output format, set this to json (yes I like json over others, blow me :p)
Note : you cannot recover a lost key. you can only generate a new one. delete the lost access key id and key pair and keep only the one which are in use and relevant.
Run the below now to list all the aws regions.
import boto3 client = boto3.client('ec2') regions =[region['RegionName'] for region in client.describe_regions()['Regions']] for region in regions: print (region)
Tuesday, December 12, 2017
Cloning the Dell iDRAC configuration to others
Hopefully this is the last among-st my series of iDRAC configuration automation. This takes one source machine as reference, takes the xml and pushes it to all the others. Whatever changes that you want to edit or omit, you can when the script is paused for you to update the csv files.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | $source = '10.1.211.222' $user = 'root' $pass = 'P@ssword' $filename = "$source.xml" $sourceXml = "$psscriptroot"+"$filename" if (!$sourceXml) { racadm -r $server -u $user -p $pass get -t xml -f $sourceXml } else { Read-Host "There is an existing $sourceXml sourcefile configuration, Hit enter/return to use the same" } Write-Host " A CSV file will be opened (open in excel/spreadsheet) populate the values, save & close the file, Hit Enter to proceed " -ForegroundColor Blue -BackgroundColor White $csv = "$PSScriptRoot/nameCompare.csv" get-process | Select-Object idrac_ip_address| Export-Csv -Path $csv -Encoding ASCII -NoTypeInformation Start-Process $csv Read-Host "Hit Enter/Return to proceed" Write-Host "processing your entries from the csv file...." $csv = Import-Csv $csv foreach ($line in $csv) { $server = $($line.server) racadm -r $server -u $user -p $pass set -t xml -f $sourceXml } |
automating the configuration of snmp settings on Dell iDRAC
This is one of my many posts of automating individual tasks on dell iDRACs. It was intended for our VxRacks but otherwise useful for all iDRACs.
http://topics-cdn.dell.com/pdf/idrac7-8-lifecycle-controller-v2.40.40.40_reference%20guide_en-us.pdf
http://topics-cdn.dell.com/pdf/idrac7-8-lifecycle-controller-v2.40.40.40_reference%20guide_en-us.pdf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | <# .SYNOPSIS set the snmp for dell iDRAC servers .DESCRIPTION opens up a csv file. put the ip addresses of idrac, root password, snmp address .NOTES File Name : idracSetSnmp.ps1 Author : gajendra d ambi Date : Dec 2017 Prerequisite : PowerShell v4+, Dell OpenManage DRAC Tools, includes Racadm (64bit) v8.1 or higher Copyright - None .LINK Script posted over: github.com/MrAmbiG/ #> #Start of Script Write-Host " A CSV file will be opened (open in excel/spreadsheet) populate the values, save & close the file, Hit Enter to proceed " -ForegroundColor Blue -BackgroundColor White $csv = "$PSScriptRoot/idracSetSnmp.csv" get-process | Select-Object idrac_ip_address, root_password, snmpAddress, snmpString | Export-Csv -Path $csv -Encoding ASCII -NoTypeInformation Start-Process $csv Read-Host "Hit Enter/Return to proceed" Write-Host "processing your entries from the csv file...." $csv = Import-Csv $csv foreach ($line in $csv) { $idrac_ip_address = $($line.idrac_ip_address) $root_password = $($line.root_password) $snmpAddress = $($line.snmpAddress) $snmpString = $($line.snmpString) $user = 'root' # enable snmp write-host enabling snmp agent -ForegroundColor Green racadm -r $idrac_ip_address -u $user -p $root_password --nocertwarn set iDRAC.SNMP.AgentEnable 0 write-host setting snmp TrapFormat v2 -ForegroundColor Green racadm -r $idrac_ip_address -u $user -p $root_password --nocertwarn set iDRAC.SNMP.TrapFormat 1 write-host setting snmp target $snmpAddress racadm -r $idrac_ip_address -u $user -p $root_password --nocertwarn set iDRAC.SNMP.Alert.DestAddr $snmpAddress write-host setting snmp community string $snmpString racadm -r $idrac_ip_address -u $user -p $root_password --nocertwarn set iDRAC.SNMP.AgentCommunity $snmpString write-host enabling snmp alert -ForegroundColor Green racadm -r $idrac_ip_address -u $user -p $root_password --nocertwarn set iDRAC.SNMP.Alert.Enable 1 } |
Automating the setting of NTPs on Dell iDRACs
This is a continuation of my series of posts about updating iDRAC configuration. This time is is the NTP servers.
http://topics-cdn.dell.com/pdf/idrac7-8-lifecycle-controller-v2.40.40.40_reference%20guide_en-us.pdf
http://topics-cdn.dell.com/pdf/idrac7-8-lifecycle-controller-v2.40.40.40_reference%20guide_en-us.pdf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | <# .SYNOPSIS set the ntp for dell iDRAC servers .DESCRIPTION opens up a csv file. put the ip addresses of idrac, root password and ntp .NOTES File Name : idracSetNtp.ps1 Author : gajendra d ambi Date : Dec 2017 Prerequisite : PowerShell v4+, Dell OpenManage DRAC Tools, includes Racadm (64bit) v8.1 or higher Copyright - None .LINK Script posted over: github.com/MrAmbiG/ #> #Start of Script Write-Host " A CSV file will be opened (open in excel/spreadsheet) populate the values, save & close the file, Hit Enter to proceed " -ForegroundColor Blue -BackgroundColor White $csv = "$PSScriptRoot/idracSetNtp.csv" get-process | Select-Object idrac_ip_address, root_password, ntp1, ntp2 | Export-Csv -Path $csv -Encoding ASCII -NoTypeInformation Start-Process $csv Read-Host "Hit Enter/Return to proceed" Write-Host "processing your entries from the csv file...." $csv = Import-Csv $csv foreach ($line in $csv) { $idrac_ip_address = $($line.idrac_ip_address) $root_password = $($line.root_password) $ntp1 = $($line.ntp1) $ntp2 = $($line.ntp2) $user = 'root' Write-Host setting $ntp1 as the ntp racadm -r $idrac_ip_address -u $user -p $root_password --nocertwarn set iDRAC.NTPConfigGroup.NTP1 $ntp1 if ($ntp2.Length -gt 2) { Write-Host settings $ntp2 as the ntp racadm -r $idrac_ip_address -u $user -p $root_password --nocertwarn set iDRAC.NTPConfigGroup.NTP1 $ntp2 } } |
automation of updating hostname on Dell iDRACs
This is another post in a series of automation of individual tasks on iDRAC using racadm.
http://topics-cdn.dell.com/pdf/idrac7-8-lifecycle-controller-v2.40.40.40_reference%20guide_en-us.pdf
This time it is to update the hostname on all the iDRACs.
http://topics-cdn.dell.com/pdf/idrac7-8-lifecycle-controller-v2.40.40.40_reference%20guide_en-us.pdf
This time it is to update the hostname on all the iDRACs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <# .SYNOPSIS set the hostname for dell iDRAC servers .DESCRIPTION opens up a csv file. put the ip addresses of idrac, hostname to be set .NOTES File Name : idracSetHostname.ps1 Author : gajendra d ambi Date : Dec 2017 Prerequisite : PowerShell v4+, Dell OpenManage DRAC Tools, includes Racadm (64bit) v8.1 or higher Copyright - None .LINK Script posted over: github.com/MrAmbiG/ #> #Start of Script Write-Host " A CSV file will be opened (open in excel/spreadsheet) populate the values, save & close the file, Hit Enter to proceed " -ForegroundColor Blue -BackgroundColor White $csv = "$PSScriptRoot/idracSetHostname.csv" get-process | Select-Object idrac_ip_address, root_password, hostname | Export-Csv -Path $csv -Encoding ASCII -NoTypeInformation Start-Process $csv Read-Host "Hit Enter/Return to proceed" Write-Host "processing your entries from the csv file...." $csv = Import-Csv $csv foreach ($line in $csv) { $idrac_ip_address = $($line.idrac_ip_address) $root_password = $($line.root_password) $hostname = $($line.hostname) Write-Host "setting $hostname as hostname on $idrac_ip_address" racadm -r $idrac_ip_address -u "root" -p $root_password --nocertwarn set iDRAC.NIC.DNSDomainName $domain } |
updating domain name on Dell iDRACs of VxRack
this is just another continuation post for idRAC automation examples. This will help you update domain name on multiple iDRACs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <# .SYNOPSIS set the hostname for dell iDRAC servers .DESCRIPTION opens up a csv file. put the ip addresses of idrac, root password and the domain name to be set .NOTES File Name : idracSetDomain.ps1 Author : gajendra d ambi Date : Dec 2017 Prerequisite : PowerShell v4+, Dell OpenManage DRAC Tools, includes Racadm (64bit) v8.1 or higher Copyright - None .LINK Script posted over: github.com/MrAmbiG/ #> #Start of Script Write-Host " A CSV file will be opened (open in excel/spreadsheet) populate the values, save & close the file, Hit Enter to proceed " -ForegroundColor Blue -BackgroundColor White $csv = "$PSScriptRoot/idracSetDomain.csv" get-process | Select-Object idrac_ip_address, root_password, DomainName | Export-Csv -Path $csv -Encoding ASCII -NoTypeInformation Start-Process $csv Read-Host "Hit Enter/Return to proceed" Write-Host "processing your entries from the csv file...." $csv = Import-Csv $csv foreach ($line in $csv) { $idrac_ip_address = $($line.idrac_ip_address) $root_password = $($line.root_password) $DomainName = $($line.DomainName) Write-Host "setting $DomainName as DomainName on $idrac_ip_address" racadm -r $idrac_ip_address -u "root" -p $root_password --nocertwarn set iDRAC.NIC.DNSDomainName $DomainName } |
update DNS on all dell iDRAC servers
We had to automate the updation of DNS servers on all of our Dell iDRACs on VxRack. Here is a script which will help you if you ever face a similar situation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | <# .SYNOPSIS set DNS on dell iDRAC servers .DESCRIPTION opens up a csv file. put the ip addresses of idrac, root username, password, dns1, dns2 .NOTES File Name : idracSetDns.ps1 Author : gajendra d ambi Date : Dec 2017 Prerequisite : PowerShell v4+, Dell OpenManage DRAC Tools, includes Racadm (64bit) v8.1 or higher Copyright - None .LINK Script posted over: github.com/MrAmbiG/ #> #Start of Script Write-Host " A CSV file will be opened (open in excel/spreadsheet) populate the values, save & close the file, Hit Enter to proceed " -ForegroundColor Blue -BackgroundColor White $csv = "$PSScriptRoot/idracSetDns.csv" get-process | Select-Object idrac_ip_address, root_password, dns1, dns2| Export-Csv -Path $csv -Encoding ASCII -NoTypeInformation Start-Process $csv Read-Host "Hit Enter/Return to proceed" Write-Host "processing your entries from the csv file...." $csv = Import-Csv $csv foreach ($line in $csv) { $idrac_ip_address = $($line.idrac_ip_address) $root_password = $($line.root_password) $dns1 = $($line.dns1) $dns2 = $($line.dns2) $user = 'root' Write-Host "setting on $idrac_ip_address" # setting dhcp to 0 and dns1 if ($dns1.Length -gt 2) { Write-Host Disabling dhcp dns on $server -ForegroundColor Yellow racadm -r $idrac_ip_address -u $user -p $root_password --nocertwarn config -g cfgLanNetworking -o cfgDNSDomainNameFromDHCP 0 Write-Host setting dns server1 $dns1 -ForegroundColor Green racadm -r $idrac_ip_address -u $user -p $root_password --nocertwarn config -g cfgLanNetworking -o cfgDNSServer1 $dns1 } # setting dns2 if ($dns2.Length -gt 2) { Write-Host setting dns server1 $dns2 -ForegroundColor Green racadm -r $idrac_ip_address -u $user -p $root_password --nocertwarn config -g cfgLanNetworking -o cfgDNSServer2 $dns2 } } |
Dell iDRAC's password change
if you ever want to udpate the password of dell iDRACs then here is a script for you.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <# .SYNOPSIS changes the idrac's root password .DESCRIPTION opens up a csv file. put the ip addresses of idrac, old password and new password in their respective columns. .NOTES File Name : idracRootPassChange.ps1 Author : gajendra d ambi Date : Dec 2017 Prerequisite : PowerShell v4+, Dell OpenManage DRAC Tools, includes Racadm (64bit) v8.1 or higher Copyright - None .LINK Script posted over: github.com/MrAmbiG #> #Start of Script Write-Host " A CSV file will be opened (open in excel/spreadsheet) populate the values, save & close the file, Hit Enter to proceed " -ForegroundColor Blue -BackgroundColor White $csv = "$PSScriptRoot/idracRootPassChange.csv" get-process | Select-Object idrac_ip_address, currentPassword, NewPassword| Export-Csv -Path $csv -Encoding ASCII -NoTypeInformation Start-Process $csv Read-Host "Hit Enter/Return to proceed" Write-Host "processing your entries from the csv file...." $csv = Import-Csv $csv foreach ($line in $csv) { $idrac_ip_address = $($line.idrac_ip_address) $currentPassword = $($line.currentPassword) $NewPassword = $($line.NewPassword) racadm -r $idrac_ip_address -u "root" -p $currentPassword --nocertwarn set iDRAC.Users.2.Password $NewPassword } |
Sunday, December 10, 2017
Saturday, December 9, 2017
Rename the vmware esxi data stores with custom names with powercli, powershell
These are to serve as tech notes for me and anyone who is starting up with powershell, powercli and showcase that how easy it is to automate. Once you know it becomes very trivial.
My team was building another VxRack.
challenge :
Each host had one datastore and that was local datastore. We needed to rename the local datastores with a <DIS>.<last octet of the ip address>. Here is the script to do so.
lines:
lines
8.foreach cluster we make a list of esxi hosts
9.foreach loop which will iterate through the list of esxi hosts that we prepared earlier
10.prints out the esxi host on which we are performing the operation
11.custom string name which is common in the new name of all the datastores
12.gets the management ip address of the esxi
13.if the ip is 1.2.3.4 then this line gets the .4, the last octet
14.new datastore name = DIS.4 -->custom string+the last octet of the management ip address
15.get that datastore of the esxi host
16.rename the datastore.
Sharing is caring! get some good karma by being social and share now.
My team was building another VxRack.
challenge :
Each host had one datastore and that was local datastore. We needed to rename the local datastores with a <DIS>.<last octet of the ip address>. Here is the script to do so.
1 2 3 4 5 6 7 8 9 10 | function renameDs { clear $clusters = "batman,superman,flash,wonderwoman" $clusters = $clusters.Split(',') foreach ($cluster in $clusters) { $cluster } } renameDs |
lines:
- function name
- blank
- clear any previous operations output on screen
- put a list of clusters here. you can also do a read-host here and expect the user to enter the cluster names separated by a comma
- prepare a list of clusters which can be easily iterated over
- foreach loop
- print out the cluster name
now run just this and if it prints out the cluster name then it means our code is good so far.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | function renameDs { clear $clusters = "batman,superman,flash,wonderwoman" $clusters = $clusters.Split(',') foreach ($cluster in $clusters) { $cluster $vmhosts = get-cluster $cluster | get-vmhost foreach ($vmhost in $vmhosts) { $vmhost.Name } } } renameDs |
lines
8.foreach cluster we make a list of esxi hosts
9.foreach loop which will iterate through the list of esxi hosts that we prepared earlier
10.prints out the esxi host on which we are performing the operation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | function renameDs { clear $clusters = "batman,superman,flash,wonderwoman" $clusters = $clusters.Split(',') foreach ($cluster in $clusters) { $cluster $vmhosts = get-cluster $cluster | get-vmhost foreach ($vmhost in $vmhosts) { $vmhost.Name $customName = "DIS" $mgmtIp = (Get-VMHost $vmhost | Get-VMHostNetworkAdapter | Where-Object {$_.Name -eq "vmk0"}).IP $mgmtIpLastOctet = $mgmtIp.Split('.')[-1] $newDsName = "$customName"+"$mgmtIpLastOctet" $datastore = (get-vmhost $vmhost | get-datastore) get-vmhost $vmhost | get-datastore $datastore.Name | Set-Datastore -Name $newDsName } } } renameDs |
11.custom string name which is common in the new name of all the datastores
12.gets the management ip address of the esxi
13.if the ip is 1.2.3.4 then this line gets the .4, the last octet
14.new datastore name = DIS.4 -->custom string+the last octet of the management ip address
15.get that datastore of the esxi host
16.rename the datastore.
Sharing is caring! get some good karma by being social and share now.
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.
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
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.
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.
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?.
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
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.,
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" } |
Automating the updating of redhat linux ethernet files
So we were building this big VxRack for our customer and there were some challenges. Client had some pretty interesting demands too. As you know VxRack goes with VMware and Redhat linux too if you want. It is all powered by Dell Emc Scale IO.
Challenge:
back up the file
extract the line with UUID and save it to temp file
remove the original file
rename the temp file as the new ifcfg-me3
create an array with the lines that you want to add
line 1 : iterate through each line
line 2 : telling shell to do stuff
line 3 : appending each line to ifcfg-me3
line 4 : finishing the loop
the whole code looks like this. notice that i have made many short shell scripts into just one line to make it all run at one shot.
lines vs meaning
1. location where it all happens
2. my filename and path
3. backup filename and path
4. path and temp filename
5. removing the existing temp or backup file in that location if any
The rest is something which we already know.
Now we have to just write another powershell+plink/babun/pscp script to copy this to around 70 redhat nodes and make it rain i mean run :)
Challenge:
- remove all lines except the one with UUID
- append some lines to the same file
as usual the file was at /etc/sysconfig/network-scripts/ifcfg-me3
1 | cp ifcfg-me3ifcfg-me3.backup |
1 | awk '/UUID/' ifcfg-me3> temp |
1 | rm -rf ifcfg-me3 |
1 | mv temp ifcfg-me3 |
rename the temp file as the new ifcfg-me3
1 2 3 4 | array=( 'BOOTPROTO=NONE' 'DEFROUTE=NO' 'NAME=me3') |
create an array with the lines that you want to add
1 2 3 4 | for i in "${array[@]}" do echo $i >> ifcfg-me3 done |
line 1 : iterate through each line
line 2 : telling shell to do stuff
line 3 : appending each line to ifcfg-me3
line 4 : finishing the loop
1 2 3 4 5 6 7 8 9 | cp ifcfg-me3 ifcfg-me3.backup; awk '/UUID/' ifcfg-me3 > temp; rm -rf ifcfg-me3 ; mv temp ifcfg-me3 array=( 'BOOTPROTO=NONE' 'DEFROUTE=NO' 'NAME=me3')for i in "${array[@]}" do echo $i >> ifcfg-me3 done |
the whole code looks like this. notice that i have made many short shell scripts into just one line to make it all run at one shot.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | path="/etc/sysconfig/network-scripts" myfile="$path/ifcfg-me3" backupfile="$path/ifcfg-me3.backup" mytemp="$path/temp" rm -rf $mytemp $backupfile cp -i $myfile $backupfile; awk '/UUID/' $myfile > $mytemp; rm -rf $myfile ; mv $mytemp $myfile array=( 'BOOTPROTO=NONE' 'DEFROUTE=NO' 'NAME=me3')for i in "${array[@]}" do echo $i >> $myfile done |
1. location where it all happens
2. my filename and path
3. backup filename and path
4. path and temp filename
5. removing the existing temp or backup file in that location if any
The rest is something which we already know.
Now we have to just write another powershell+plink/babun/pscp script to copy this to around 70 redhat nodes and make it rain i mean run :)
Wednesday, December 6, 2017
CentOS 7, Install, Rant, differentiating Opensource and Free software [FSF]
Yesterday I had the time to install CentOSs 7. In office we have a situation where we have to install 50+ RHEL servers on our solutions. I needed to experiment and moreover it has been long since I messed with any GNU OS.
In this I talk about RHEL, CentOS and how they are different. The top dogs of GNU OS. what is opensource? what is free software? are they same? how different are they? who is and was behind rhel and centOS? how connected are they?.
Sunday, December 3, 2017
PostgreSQL 10
- Installing PostgreSQL 10
- PostgreSQL and its components
Saturday, December 2, 2017
Renaming files via powershell
Okay, so those of you who are can already rename a bunch of files on your system to something else with powershell can skip it. This is for those who are still trying to get a grip on their powershell. Some of my colleagues are starting with it so it is for them.
I needed to rename a bunch of files on my system. 66 to be exact. I needed to rename them with just an incremental number.
Now we see here that I have the complete script above.
line1-the path to the folder where all the files are located
line2-this get-childitem and the path from the line 1 gets the all the child items from that path and stores that data into a variable called $items. This will be our iterator.
line3 - let us first set our beginning number to be 1. $n = 1
line4 - here we are iterating through the list of $items
line5 - here we will define or decide what the new name of the $item in the list $items will be. $n was set to 1 so it will be 1.jpeg
line 6 - here i am renaming the $item with the fullname blablablah.jpeg to the newname $newname which we have decided in the line 5
line 7 - here i am just printing out the new name. This line is optional
line 8 - now on when the next time this loop runs we want the value of $n to be 2 or 3, that is increment it by 1. $n++ means $n+1.
I needed to rename a bunch of files on my system. 66 to be exact. I needed to rename them with just an incremental number.
1 2 3 4 5 6 7 8 9 | $path = 'C:\Users\loser\Materials\my files' $items = Get-ChildItem $path $n = 1 foreach ($item in $items) { $newname = "$n" + '.jpeg' Rename-Item $item.FullName -NewName $newname -Confirm:$false $newname $n++ } |
Now we see here that I have the complete script above.
line1-the path to the folder where all the files are located
line2-this get-childitem and the path from the line 1 gets the all the child items from that path and stores that data into a variable called $items. This will be our iterator.
line3 - let us first set our beginning number to be 1. $n = 1
line4 - here we are iterating through the list of $items
line5 - here we will define or decide what the new name of the $item in the list $items will be. $n was set to 1 so it will be 1.jpeg
line 6 - here i am renaming the $item with the fullname blablablah.jpeg to the newname $newname which we have decided in the line 5
line 7 - here i am just printing out the new name. This line is optional
line 8 - now on when the next time this loop runs we want the value of $n to be 2 or 3, that is increment it by 1. $n++ means $n+1.
Friday, December 1, 2017
Automating mapping of vlans to vnic templates on cisco UCS
If you haven't read my article on Automating adding of vlans to UCS then please do. This is a continuation of that. You will also be able to understand this better if you have gone through that. Some of the explanation might be repetitive but please hold on. Repetition helps you to retain it better. So here we go. I am writing this to enable my nearly 90 solutions architects for whom I do automation on cisco, emc and vmware products to understand automation and logic behind it. make them realize that it is easier than they think.
Challenge :
Map 500 vlans to vnicTemplate-A
Map 500 vlans to vnicTemplate-B
So in total 1000 manual tasks to do.
Here is the powertool command for mapping vlans to vnic templates.
Now we need to loop it against all 500*2 templates.
Assuming that we are connected to UCS already. Let us begin.
The above 6 lines will just tell the user on what to do. Now we will use a csv file to take the input and use that csv file to map vlans to vnics.
Now here $PSScriptRoot means that the path for our csv file will be the current working directory or wherever this script is currently stored. Then we are giving a name to the csv file.
line 1 --> exports a csv file with vnicTemplate,Vlan as the name of the columns.
line 2 --> it will start the csv file and opens it up for the user.
line 3 --> This is to pause the script till the csv file is populated with all the values and saved. Once done just hit enter.
line1 --> imports the csv file's data and stores all that data into a variable's name called csv.
line2 --> processes each line in the csv file
line3 --> in the line $line from the column named vnicTemplate it takes the value and stores into a variable called $vnicTemplate
line4 --> in the line $line from the column named Vlan it takes the value and stores into a variable called $Vlan
for the 1st line when we create these variables we want to use these inside the powertool oneliner script to map this vlan with this vnicTemplate.
line 6--> uses the variables created from the line 3,4 and maps the vlan from 4 to vnic template from line 3.
Now I leave the combining all this into what you want. Have fun.
Challenge :
Map 500 vlans to vnicTemplate-A
Map 500 vlans to vnicTemplate-B
So in total 1000 manual tasks to do.
Here is the powertool command for mapping vlans to vnic templates.
1 | Get-UcsOrg -Level root | Get-UcsVnicTemplate -Name $vnicTemplate | Add-UcsVnicInterface -ModifyPresent -DefaultNet "false" -Name $vlan |
Now we need to loop it against all 500*2 templates.
Assuming that we are connected to UCS already. Let us begin.
1 2 3 4 5 6 | Write-Host " A CSV file will be opened (open in excel/spreadsheet) populate the values, save & close the file, Hit Enter to proceed " -ForegroundColor Blue -BackgroundColor White |
The above 6 lines will just tell the user on what to do. Now we will use a csv file to take the input and use that csv file to map vlans to vnics.
1 | $csv = "$PSScriptRoot/ucsVlanVnicMap.csv" |
Now here $PSScriptRoot means that the path for our csv file will be the current working directory or wherever this script is currently stored. Then we are giving a name to the csv file.
1 2 3 | get-process | Select-Object vnicTemplate,Vlan | Export-Csv -Path $csv -Encoding ASCII -NoTypeInformation Start-Process $csv Read-Host "Hit Enter/Return to proceed" |
line 1 --> exports a csv file with vnicTemplate,Vlan as the name of the columns.
line 2 --> it will start the csv file and opens it up for the user.
line 3 --> This is to pause the script till the csv file is populated with all the values and saved. Once done just hit enter.
1 2 3 4 5 | $csv = Import-Csv $csv foreach ($line in $csv) { $vnicTemplate = $($line.vnicTemplate) $Vlan = $($line.Vlan) } |
line1 --> imports the csv file's data and stores all that data into a variable's name called csv.
line2 --> processes each line in the csv file
line3 --> in the line $line from the column named vnicTemplate it takes the value and stores into a variable called $vnicTemplate
line4 --> in the line $line from the column named Vlan it takes the value and stores into a variable called $Vlan
for the 1st line when we create these variables we want to use these inside the powertool oneliner script to map this vlan with this vnicTemplate.
1 2 3 4 5 6 | $csv = Import-Csv $csv foreach ($line in $csv) { $vnicTemplate = $($line.vnicTemplate) $Vlan = $($line.Vlan) Get-UcsOrg -Level root | Get-UcsVnicTemplate -Name $vnicTemplate | Add-UcsVnicInterface -ModifyPresent -DefaultNet "false" -Name $vlan } |
line 6--> uses the variables created from the line 3,4 and maps the vlan from 4 to vnic template from line 3.
Now I leave the combining all this into what you want. Have fun.
Thursday, November 30, 2017
Automating adding of vlans to UCS
I often wonder why am I doing automation because it is boring. Doing stuff manually is time consuming, inefficient but gives a sense of 'hey i am doing something'. Lol... anyway we had a VxBlock and my colleague needed to add 500+ vlans on UCS and we needed to map them to fabric-A and fabric B.
System Requirements:
Powershell 4+
Cisco Powertool Suite 2.x
The purpose of this post is to make the reader understand that how easy it is to come up with your own automation and it is easier than resisting sugar in your diet.
So let us look at the command first which does the adding of vlan
It is simple as a pimple on a dimple on my niece cheek. Okay enough of showing off of my poetic (oh i know i suck at it...) let us move on shall we!
The above command is something you can find from UCS documentation or search online. Yes, why do stuff when it is already done. Build on it not rebuild it.
$vlan here is the name of the vlan, an alphanumeric.
$vlanID is the vlan ID, an integer.
If you have seen any of my previous scripts or even seen my powershell tutorials then you will know that I like csv files to take inputs. They are simple and easy to work with. When the user runs our script we want the script to open a csv file with proper headers .
1st part of the script will display a message on the terminal and tells the user on what to do.
After this we want to define the path where the csv file will be created and it's name.
I always love to $PSScriptRoot as the path since it means that wherever the script is currently located that is the our path. So it sets the current working directory as your path. the gnu/linux shell equivalent of cwd. After that I have given the name of the file with .csv as the extension. Since we have the path and the file we now need to add headers to the file.
Do not get confused with why I am using get-process here. It is just my street smart way of adding headers (headings) to the csv file. The above '-Encoding ASCII -NoTypeInformation' is important since that enables you to open it with an excel.
Okay the start-process opens up the file but we want to wait for the user to enter the data and wait. So we will Add a Read-host command below since it pauses the script till the user enters something, in this case, just hit enter once the user is done entering the data, save the file.
Processing a csv file is damn easy. the 1st line is importing the data from the $csv file and storing all that in the $variable $csv.
2nd line is going through each line of the $csv file and creating a variable. $vlan = $($line.vlan) here means that go in the line $line where the heading or column name is vlan, take the value from that cell and store it in the variable named $vlan. The same thing is true with $vlanID. Now once we have the variables for the 1st line we want to do something with that right? so let us do it.
So once I created the variables I am using those variables to create a vlan with a vlan ID in the last line. Above that I have another line which is just to tell the user what it is doing.
Combine all the above and now you have the script to add vlans with vlan IDs to the UCS. Yes, I leave that combining part to you. Anything where you have not put some effort of your own you won't find it interesting and struggle learn to something or do something, even if it is trivial will always make you know it than just remember it.
System Requirements:
Powershell 4+
Cisco Powertool Suite 2.x
The purpose of this post is to make the reader understand that how easy it is to come up with your own automation and it is easier than resisting sugar in your diet.
So let us look at the command first which does the adding of vlan
Get-UcsLanCloud | Add-UcsVlan -DefaultNet no -Id $vlan -Name $vlanID
It is simple as a pimple on a dimple on my niece cheek. Okay enough of showing off of my poetic (oh i know i suck at it...) let us move on shall we!
The above command is something you can find from UCS documentation or search online. Yes, why do stuff when it is already done. Build on it not rebuild it.
$vlan here is the name of the vlan, an alphanumeric.
$vlanID is the vlan ID, an integer.
If you have seen any of my previous scripts or even seen my powershell tutorials then you will know that I like csv files to take inputs. They are simple and easy to work with. When the user runs our script we want the script to open a csv file with proper headers .
1st part of the script will display a message on the terminal and tells the user on what to do.
Write-Host " A CSV file will be opened (open in excel/spreadsheet) populate the values, save & close the file, Hit Enter to proceed " -ForegroundColor Blue -BackgroundColor White
After this we want to define the path where the csv file will be created and it's name.
$csv = "$PSScriptRoot/ucsAddVlan.csv"
I always love to $PSScriptRoot as the path since it means that wherever the script is currently located that is the our path. So it sets the current working directory as your path. the gnu/linux shell equivalent of cwd. After that I have given the name of the file with .csv as the extension. Since we have the path and the file we now need to add headers to the file.
get-process | Select-Object vlan,vlanID | Export-Csv -Path $csv -Encoding ASCII -NoTypeInformation
Do not get confused with why I am using get-process here. It is just my street smart way of adding headers (headings) to the csv file. The above '-Encoding ASCII -NoTypeInformation' is important since that enables you to open it with an excel.
# this line opens the csv file Start-Process $csv # the below line waits for the user to save the csv file and hit enter Read-Host "Hit Enter/Return to proceed"
Okay the start-process opens up the file but we want to wait for the user to enter the data and wait. So we will Add a Read-host command below since it pauses the script till the user enters something, in this case, just hit enter once the user is done entering the data, save the file.
$csv = Import-Csv $csv foreach ($line in $csv) { $vlan = $($line.vlan) $vlanID = $($line.vlanID) }
Processing a csv file is damn easy. the 1st line is importing the data from the $csv file and storing all that in the $variable $csv.
2nd line is going through each line of the $csv file and creating a variable. $vlan = $($line.vlan) here means that go in the line $line where the heading or column name is vlan, take the value from that cell and store it in the variable named $vlan. The same thing is true with $vlanID. Now once we have the variables for the 1st line we want to do something with that right? so let us do it.
foreach ($line in $csv) { $vlan = $($line.vlan) $vlanID = $($line.vlanID) write-host -ForeGroundColor yellow "Add Vlan $vlan $vlanID" Get-UcsLanCloud | Add-UcsVlan -DefaultNet no -Id $vlan -Name $vlanID }
So once I created the variables I am using those variables to create a vlan with a vlan ID in the last line. Above that I have another line which is just to tell the user what it is doing.
Combine all the above and now you have the script to add vlans with vlan IDs to the UCS. Yes, I leave that combining part to you. Anything where you have not put some effort of your own you won't find it interesting and struggle learn to something or do something, even if it is trivial will always make you know it than just remember it.
Python 3.6 OOP Regular, Class and static methods
files here
https://github.com/MrAmbiG/LearningPython
Sunday, November 26, 2017
My PyCharm Setup
So these are my notes to get back my pycharm setup in case if i have reset it or lose it for whatever reason.
- PyCharm community edition ( i am poor, can't afford professional edition )
- Atom file icons IDEA
- CodeGlance
- Material Theme UI
- replace the default ctrl+shift+f10 to run the code with ctr+comma (this seems to be the only ctrl+ combo available, rest are all taken by others)
Wednesday, November 22, 2017
Python 3.6 GUI with tkinter
There is no fun in using an application which does not have a graphical user interface. When you are trying to make a game or a program and if it is for those who are not coders then tkinter is here for the rescue.
Get your notebook here.
tkinter is a built in GUI module. Here we are importing it. Creating a window. We can run just this code but the window will disappear too fast. saying window.mainloop() will make it stay till we close it.
I urge you to always do the following when you are exploring a module.
Once you are done exploring the module then let us improvise on our previous work.
Get your notebook here.
import tkinter as tk # create a window window = tk.Tk() # start the GUI window.mainloop()
tkinter is a built in GUI module. Here we are importing it. Creating a window. We can run just this code but the window will disappear too fast. saying window.mainloop() will make it stay till we close it.
I urge you to always do the following when you are exploring a module.
dir(tkinter)
help(tkinter)
Once you are done exploring the module then let us improvise on our previous work.
- create a Label for our window
- Give it a label
- define a size for the window
import tkinter as tk from tkinter import ttk # this is needed to add buttons and labels # create a window window = tk.Tk() window.title('Justice League') # let us give a title to our window window.geometry("500x400") # start the GUI window.mainloop()
What is the use of this justice league window without it's members? The house is empty. Let us bring in our heroes.
import tkinter as tk from tkinter import * # this is needed to add buttons and labels # create a window window = tk.Tk() window.title('Justice League') # let us give a title to our window window.geometry("500x400") # # Let us add our members to the window T = tk.Text(window) T.pack() T.insert(tk.END, "Batman \nSuperman \nFlash \nAquaman \nWonder woman \nCyborg") # Start the GUI event loop window.mainloop()
You can easily make out that we are now adding text to the window. This is how you add text in tkinter so I will not try to pour any logic here. You just have to remember it or do it so many times to that you won't forget.
If you run it now, you will have the text displayed. Why stop there? Let us add a button, give it a name and when you click on it, it will display the true identity of the members of the justice league.
import tkinter as tk from tkinter import ttk # this is needed to add buttons and labels # create a window window = tk.Tk() window.title('Justice League') # let us give a title to our window window.geometry("500x400") # Let us add our members to the window T = Text(window, height=10, width=30) # define the area of text T.pack() T.insert(END, "Batman \nSuperman \nFlash \nAquaman \nWonder woman \nCyborg") # member's real identity def RevealId(): print("Batman is Bruce Wayne \nSuperman is Clark Kent \nFlash is Barry Allen \ \nAquaman is Arthur Curry \nWonder woman is Diana Prince \nCyborg is Victor Stone") # add a button to do some stuff b = Button(text="Reveal their identity", command=RevealId) b.pack() # Start the GUI event loop so that it won't close window.mainloop()
go on. explore more.
Subscribe to:
Posts (Atom)