How to retrieve the CNAME on Windows? - powershell

I want to know if there is an existing command or script to retrieve the Canonical Name Record (CNAME) for a given computer or server. I would like to use this via CMD/Powershell I don't mind if such command or script uses the Windows Registry Editor (regedit).

The hostname program/command should give you the DNS name of the local machine, whereas the shell command echo %COMPUTERNAME% should show you the NetBIOS name (which might be different).

Related

How do I get the appcmd? Windows 10

How do I get the appcmd to write commands? Using Windows 10. Getting this error:
'appcmd' is not recognized as an internal or external command, operable program or batch file.
Also, if I have used this command:
appcmd delete site "Default Web Site"
If I what to do the opposite - do I use add instead of delete?
Appcmd.exe is located at path %windir%\system32\inetsrv and by default is not listed in PATH variable. So in order to use this command you have to navigate to this folder first and then you can update IIS configuration using appcmd utility.
By the way appcmd.exe is available only to the Administrator account or to users who are members of the Administrators group on the computer.
And to your second question, yes opposite verb to DELETE is ADD.
More information can be found at documentation: https://technet.microsoft.com/en-us/library/jj635852(v=ws.11).aspx#BKMK_Start

How to change & verify hostname in windows server 2012 (AWS EC2)

I am using Windows Server 2012-R2 (amazon ec2) machine.
and I want to change and verify hostname in it. I checked on net and found hostname command. But when I run hostname or hostname -i command I get following error..
sethostname: Use the Network Control Panel Applet to set hostname.
hostname -s is not supported.
I further googled this problem and apparently this command is not usable on windows 2012-r2 servers. Is there any alternative or workaround command that can do the job for me??
On windows the computer name is also often referred to as hostname. However this creates some confusion. Some people use the term hostname to refer to the leftmost part of domain name which can only be set on a DNS server. However another meaning of a hostname is just an arbitrary label assigned to a machine on a computer network, and this can be directly changed on the specific machine.
There are several ways to change the computer name from the command line on windows 2012:
Method 1. Open cmd and type:
SCONFIG and you will get selection menu where #2 says Computer Name:
Method 2. From cmd type:
netdom renamecomputer %computername% /newname:<NewName> /reboot:0
Method 3. From the PowerShell console:
netdom renamecomputer $env:computername /newname:<NewName> /reboot:0
To verify the name form cmd type:
nbtstat -A xxx.xxx.xxx.xxx (where x is the ip address)
Another way to verify the name form cmd just type "hostname" without any parameters:
hostname

Using Netsh with PsExec

I'm trying to dump DHCP settings from an older server thats being decommissioned. I ran the command fine while on the actual server but when trying to run it using psexec remotely, it keeps failing. The command is: psexec \\$source netsh dhcp server \\$source dump>$dhcpSettings
$source = the server being decommissioned
$dhcpSettings = the path to save the dumped settings
I've tried all sorts of combinations of encapsulating quotation marks but still nothing. the errors I'm getting is, "The system cannot find the file specified" and "The system cannot find the path specified"
EDIT: So I got rid of the path to save the dumped settings and now it works. But how should I format the command so that it'll save the settings to the remote computer's C:\USER.SET\LOG directory?
One solution might be to bundle the command you want to run and the stdout redirection into a single line cmd file and use PsExec -c or -f to copy and execute that file on the remote system. As an example
Create a line cmd file named DHCPSettings.cmd with the following in it and save it to C:\temp\:
netsh dhcp server \\localhost dump >c:\user.set\log\dhcpsetting.log
Then use
psexec \\$source -c c:\temp\DHCPSettings.cmd
You did not really provide any code to go by and I am not sure I understand the all requirements and constraints you have, so consider this as an idea; not the exact commands you need to run. Hope it helps.

Run a perl script inside another script using ssh

I have a perl script where I need to connect to another machine using ssh and there run another perl script. I tried using this line:
system("ssh $admin_server 'perl /Perl/scripts/capture_server_restarts_gse.pl $month $date'");
But everytime the script gets to that line, I get the prompt for the remote machine and the script doesn't run.
How can I fix this so the script runs automatically on the other machine without showing the prompt.
Note: I don't need the password and user to connect to the remote machine we already solved that.
Why not copy your public key onto the other machine ? That way you'll be pre-authorised.
Here are the instructions on how to do this using ssh-keygen
Otherwise you have to feed ssh with your password, and that's tricky since ssh normally takes input from a tty and you have to configure your script with the password.
The SSH server may be configured to run always some custom shell instead of the command passed from the client.
Try just running some simple command from the command line, i.e.:
ssh server ls
A less likely possibility is that the perl variables interpolated into the system argument could contain some shell metacharacters requiring better escaping. For instance, a semicolon inside $admin_server.

Is it possible to have Perl run shell script aliases?

Is it possible to have a Perl script run shell aliases? I am running into a situation where we've got a Perl module I don't have access to modify and one of the things it does is logs into multiple servers via SSH to run some commands remotely. Sadly some of the systems (which I also don't have access to modify) have a buggy SSH server that will disconnect as soon as my system tries to send an SSH public key. I have the SSH agent running because I need it to connect to some other servers.
My initial solution was to set up an alias to set ssh to ssh -o PubkeyAuthentication=no, but Perl runs the ssh binary it finds in the PATH instead of trying to use the alias.
It looks like the only solutions are disable the SSH agent while I am connecting to the problem servers or override the Perl module that does the actual connection.
Perhaps you could put a command called ssh in PATH ahead of the ssh which runs ssh as you want it to be run.
Alter the PATH before you run the perl script, or use this in your .ssh/config
Host *
PubkeyAuthentication no
Why don't you skip the alias and just create a shell script called ssh in a directory somewhere, then change the path to put that directory before the one containing the real ssh?
I had to do this recently with iostat because the new version output a different format that a third-party product couldn't handle (it scanned the output to generate a report).
I just created an iostat shell script which called the real iostat (with hardcoded path, but you could be more sophisticated), passing the output through an awk script to massage it into the original format. Then, I changed the path for the third-party program and it started working fine.
You could declare a function in .bashrc (or .profile or whatever) with that name. It could look like this (might break):
function ssh {
/usr/bin/ssh -o PubkeyAuthentication=no "$#"
}
But using a config file might be the best solution in your case.