how to write powershell script to check if the VM is jumpbox VM from a list of VMs in azure - azure-powershell

Powershell script to retreive Jumpbox VM from a list of VMs, which VMs are all have jumpbox connection. How to differentiate Jumpbox VM from other VMs. The output will be like this VM is Jumpbox VM.
I have tried to retrive ip addresses, security rules, tags associated with it, But all are same with other VMs, So I am not getting efficient output.I need to find unique configuration jumpbox vm has from other vms.
The output will be like this "This VM is Jumpbox VM".

I deployed one Jumpbox VM with its NIC connected to the Private IP securely and ran this PowerShell command to get the Jumpbox VM's.
Thanks #Fabricio godboy for the script.
Reference- How to write powershell script to identify whether the Azure Virtual machine has Jumpbox or bastionhost or other private vms - Microsoft Q&A
I created one Jumpbox VM and ran the Powershell script like below:-
Script:-
#
Connect-AzAccount
# Specify the resource group name and VM name
$resourceGroupName = "jumpboxvm"
$vmName = "jumpboxvm12"
# Get the VM object
$vm = Get-AzVM -ResourceGroupName $resourceGroupName -Name $vmName
# Get the VM's network interface object
$nic = Get-AzNetworkInterface -ResourceId $vm.NetworkProfile.NetworkInterfaces[0].Id
# Get the VM's private IP address
$privateIpAddress = $nic.IpConfigurations.PrivateIpAddress
# Check if the VM is connected to a jumpbox
$jumpbox = Get-AzVM -ResourceGroupName $resourceGroupName -Name "jumpboxvm12"
if ($jumpbox) {
$jumpboxNic = Get-AzNetworkInterface -ResourceId $jumpbox.NetworkProfile.NetworkInterfaces[0].Id
$jumpboxPrivateIpAddress = $jumpboxNic.IpConfigurations.PrivateIpAddress
if ($privateIpAddress -eq $jumpboxPrivateIpAddress) {
Write-Host "This VM is connected to a jumpbox"
exit
}
}
And got the output like below:-

Related

Running Automation on Google Cloud Virtual Instances using Ansible

I'm trying to automate google cloud virtual instances remotely only using external ip addresses of virtual machines. I can ssh into the virtual machines using command line with user name shishir9159_gmail_com . But If I use any ansible commands like this:
ansible -i hosts -u shishir9159_gmail_com --private-key=~/.ssh/google_compute_engine -m ping all
and it results in this following error:
"msg": "Failed to connect to the host via ssh: shishir9159#35.202.219.6: Permission denied (publickey,gssapi-keyex,gssapi-with-mic)."
I've added some parameters in my ansible.cfg:
host_key_checking = False
ssh_args = -o ControlMaster=no
But I don't think they do much of a help according to this post:
https://serverfault.com/questions/929222/ansible-where-do-preferredauthentications-ssh-settings-come-from
And I tried many methods and recommendations. I have a service account but it doesn't seem to me necessary for this simple ping command.
The problem is in the underscores of the user name. Try to add a username without underscore or try using quote.
I solved the problem by adding ansible_ssh_user and ansible_ssh_pass at the hosts file. This post contain the solution.
ansible SSH connection fail

Get VM Name and FQDN

Im trying to write a Powershell Query to get the VM Name & FQDM, this is what I have but its not returning the FQDN
Get-AzureRmVm | Select-Object Name , #{Name="FullyQualifiedDomainName"; Expression={$_.ToPSVirtualMachine().FullyQualifiedDomainName.Value}} | Format-Table
Update 1
So I have been able to get the VM, and then the FQDN, and I feel its safe enough for me to do it this was because i know a single RSG will only even contain one VM and one FQDN, so its safe for me to assume they are linked.
Get-AzureRmVm | Foreach-Object{Get-AzureRmPublicIpAddress -ResourceGroupName $_.ResourceGroupName} | Select-Object ResourceGroupName, Name , Location , #{Name="FQDN"; Expression={$_.DnsSettings.Fqdn}}
However it is slow because of the Foreach, wondered if anyone has any other suggestions on this.
When you create an Azure VM, a public IP resource for the VM is automatically created. Refer to this. None of FQDN is created for a VM in the Azure portal. You could configure it once the VM is created. You could not get the FQDN directly by using the AzureRM.Compute module since the FQDN or DNS name is not a property of VM but is a public IP address configuration.
You can get an FQDN of an Azure VM using
(Get-AzureRmPublicIpAddress -ResourceGroupName $rg -Name $publicipaddress ).DnsSettings.Fqdn

How to get own vm name in guest environment

I want to change VM setting in my own guest VM.
I can connect to vCenter but I'm not detecting my own VM.
What is a simple way to get the VM name?
Pretty sure you can use a simple Powershell command like so:
get-vmmserver localhost | get-vm "vmname" | select Name, ComputerName,
HostName | format-list
PowerCLI allows you to do this simply with:
Connect-VIServer -Server vcenterservername.fqdn
Get-VM -Name vmname
You can also just connect to the web client and perform a search at: https://vcenterservername.fqdn/

gcloud compute ssh from one VM to another VM on Google Cloud

I am trying to ssh into a VM from another VM in Google Cloud using the gcloud compute ssh command. It fails with the below message:
/usr/local/bin/../share/google/google-cloud-sdk/./lib/googlecloudsdk/compute/lib/base_classes.py:9: DeprecationWarning: the sets module is deprecated
import sets
Connection timed out
ERROR: (gcloud.compute.ssh) [/usr/bin/ssh] exited with return code [255]. See https://cloud.google.com/compute/docs/troubleshooting#ssherrors for troubleshooting hints.
I made sure the ssh keys are in place but still it doesn't work. What am I missing here?
There is an assumption that you have connected to the externally-visible instance using SSH beforehand with gcloud.
From your local machine, start ssh-agent with the following command to manage your keys for you:
me#local:~$ eval `ssh-agent`
Call ssh-add to load the gcloud compute public keys from your local computer into the agent, and use them for all SSH commands for authentication:
me#local:~$ ssh-add ~/.ssh/google_compute_engine
Log into an instance with an external IP address while supplying the -A argument to enable authentication agent forwarding.
gcloud compute ssh --ssh-flag="-A" INSTANCE
source: https://cloud.google.com/compute/docs/instances/connecting-to-instance#sshbetweeninstances.
I am not sure about the 'flags' because it's not working for me bu maybe I have a different OS or Gcloud version and it will work for you.
Here are the steps I ran on my Mac to connect to the Google Dataproc master VM and then hop onto a worker VM from the master MV. I ssh'd to the master VM to get the IP.
$ gcloud compute ssh cluster-for-cameron-m
Warning: Permanently added '104.197.45.35' (ECDSA) to the list of known hosts.
I then exited. I enabled forwarding for that host.
$ nano ~/.ssh/config
Host 104.197.45.35
ForwardAgent yes
I added the gcloud key.
$ ssh-add ~/.ssh/google_compute_engine
I then verified that it was added by listing the key fingerprints with ssh-add -l. I reconnected to the master VM and ran ssh-add -l again to verify that the keys were indeed forwarded. After that, connecting to the worker node worked just fine.
ssh cluster-for-cameron-w-0
About using SSH Agent Forwarding...
Because instances are frequently created and destroyed on the cloud, the (recreated) host fingerprint keeps changing. If the new fingerprint doesn't match with ~/.ssh/known_hosts, SSH automatically disables Agent Forwarding. The solution is:
$ ssh -A -o UserKnownHostsFile=/dev/null ...

Unable to enable replication

I want to configure a multi-master replication between two instances of Opendj on two different machine: the first is on localhost, the second is on virtualbox.
I have Opendj installed on both machines. I can't enable replication because I don't know how to connecto to the virtual station. The command:
./bin/dsreplication enable --host1 work_station --port1 4444 --bindDN1 \
"cn=Directory Manager" --bindPassword1 password --replicationPort1 8989 \
--host2 virtual_station --port2 4444 --bindDN2 "cn=Directory Manager" \
--bindPassword2 password --replicationPort2 8989 --adminUID admin \
--adminPassword password --baseDN "dc=example,dc=com" -X -n
gives me the output:
Establishing connections .....
Could not connect to virtual_station:4444. Check that the server is running and
that it is accessible from the local machine. Details: virtual_station:4444
To be able to enable replication between 2 machines (physical or virtual), the OpenDJ service must be able to resolve both names (either from DNS or local hosts file) and to connect to the different machines and ports.
Make sure both names can be resolved from each machine, and also make sure that you do not have a firewall or other configuration that are blocking ports.