replace missing content with sed - sed

In a file i have this structure:
192.168.1.53 # hostname, city, state
192.168.1.101 # hostname, city, state
192.168.1.10 # hostname, city, state
192.168.1.18 # hostname, city, state
192.168.1.19
192.168.1.104 # hostname, city, state
How can I use sed to find and replace when there is NOT a '#' but replace after ip then move on to next one that is missing '#'.
So it looks like below:
192.168.1.53 # hostname, city, state
192.168.1.101 # hostname, city, state
192.168.1.10 # hostname, city, state
192.168.1.18 # hostname, city, state
192.168.1.19 # newcontent, new, new
192.168.1.104 # hostname, city, state

If the piece you want to add is a fixed string, it's pretty easy:
sed 's/^[^#]*$/& # newcontent, new, new/'
In the replacement string, & stands for the matched text.

Related

Powershell import-csv anad get the latest date

I'm importing a CSV file and I need to return the most recent logged in date for a specific hostname. Any ideas? I've been trying to group objects and run a for-each on them but I can't manage to pull the latest. Here's an example of the data:
e.g. for mypc1 I want to return an object that includes the hostname, username and also the date of the latest login. In this case 2022-08-15 07:51:51.
Computer Name
Username
Date
mypc1
jcitizen
2022-07-06 18:00:20
mypc1
bmcgee
2022-08-15 07:51:51
mypc1
jmarsh
2021-05-25 12:49:14
mypc2
jmarsh
2022-08-12 07:45:55
mypc2
jsmith
2022-03-12 07:00:07
mypc3
osmith
2022-08-08 08:36:03
mypc3
moliver
2022-08-08 07:37:27

How to check when a dhcp IP was released?

My DHCP server is 2008R2. I was wondering that if there is any way to know the release date and time for a given DHCP IP address?
I see there is a command "get-dhcpserverv4lease", but it only exists on Win 10 and Server 2016.
How can I know it on 2008R2 or 2012R2?
You can use WMI oder netsh.
WMI
Get-WmiObject -Namespace ROOT\Microsoft\Windows\DHCP -List | Where-Object Name -eq 'DhcpServerv4Lease'
Unfortunately I cannot validate this WMI query, because I only have a Windows Server 2016 DHCP server and it is returning an empty object there. However, the PS_DhcpServerv4IPLease class is working fine on 2016.
NETSH
You need to parse the output and iterate throug all scopes
netsh dhcp server \\dhcpservername show scope
==============================================================================
Scope Address - Subnet Mask - State - Scope Name - Comment
==============================================================================
192.168.178.0 - 255.255.255.0 -Active -Default IPv4 Scope -
Total No. of Scopes = 1
Command completed successfully.
Then through all IP-Addresses
C:\Windows\system32>netsh dhcp server \\dhcpservername scope 192.168.178.0 show clients
Changed the current scope context to 192.168.178.0 scope.
Type : N - NONE, D - DHCP B - BOOTP, U - UNSPECIFIED, R - RESERVATION IP
==================================================================================
IP Address - Subnet Mask - Unique ID - Lease Expires -Type
==================================================================================
192.168.178.170 - 255.255.255.0 - b0-72-bf-cb-97-a0 -25.04.2019 18:56:41 -D
192.168.178.171 - 255.255.255.0 - 00-0d-2f-d5-b1-d6 -25.04.2019 12:03:38 -D
192.168.178.172 - 255.255.255.0 - b0-4e-26-72-9b-5f -19.04.2019 23:08:19 -D
192.168.178.173 - 255.255.255.0 - 70-4f-57-11-01-03 -20.04.2019 23:00:00 -D
No of Clients(version 4): 4 in the Scope : 192.168.178.0.
Command completed successfully.
Sebastian Segerer, netsh dhcp server only works up to windows 8.1....beyond that there is no netsh DHCP SERVER ...only netsh dhcpclient and it wont get what you would need.
so if you have windows 10 it wont work, or 2008 R2 or 2012 or 2016.

How to get the computers domain name in matlab?

I tried getting domain name in c# as below,
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
Console.WriteLine("Domain name: {0}", properties.DomainName);
I have searched similar kind of approach in matlab for finding domain name. But I am not successful. Could anyone help me out like how we can get the domain name matlab(The domain name looks like yyy.xxxxx.com).
I got to know how to get computer name as below:
cc=getenv('computername');
How about this:
[s,cout] = system('net config workstation | findstr /C:"Full Computer name"');
FQDN= strtrim(strrep(cout,'Full Computer name',''))
FQDN=
XXXX.YYYY.com
This returns the fully qualified domain name (FQDN) of the computer . Where XXXX is your PC Name and YYYY is the domain.
Similarly if you just want the domain name:
[s,cout] = system('systeminfo | findstr /C:"Domain:"');
Domain = strtrim(strrep(cout,'Domain:',''))
Domain =
YYYY.com
EDIT: You can also get the FQDN using java in matlab like this:
FQDN = java.net.InetAddress.getLocalHost.getCanonicalHostName
FQDN =
XXXX.YYYY.com
You can read from the environment variable:
getenv('USERDOMAIN')

How to Update DNS Records Programatically

I am trying to update a DNS Record which is at (I believe) the following path on the server:
ServerName -> Forward Lookup Zones -> domain.com -> test
Where the DNS record is called test, and is of type Host(A).
I downloaded the DNSShell module from here and tried changing the 'Data' column of the DNS record (which contains an IP address) with this command:
Set-DNSRecord -Identity "test.domain.com"
But I got this error:
Cannot validate argument on parameter 'Identity'. The argument "test.domain.com" does not match the "^\\.\root\MicrosoftDNS:MicrosoftDNS_" pattern. Supply an argument that matches "^\\.\root\MicrosoftDNS:MicrosoftDNS_" and try the command again.
So I updated the Identity parameter as follows:
Set-DNSRecord -Identity "\\Servername\root\MicrosoftDNS:MicrosoftDNS_"
But now I am seeing:
Set-DNSRecord : Specified argument was out of the range of the valid values. Parameter name: Path
When I try adding a -Path, it says there is not such parameter! Does anyone know where I need to add the test.domain.com part of the DNS to tell the command which record to update? The documentation on this module is incomplete for this part of it and I can't seem to find any alternatives.
I can call Get-DNSRecord and see the record I need to update, but any attempts to Set are blocked because I have no idea how these paths are built.
Any help is much appreciated.
You can use also default PowerShell module for DNS server from Microsoft, example would then look something like this:
Add-DnsServerResourceRecordA -Name "recordname" -IPv4Address "192.168.0.0" -ZoneName "domain.com" -AllowUpdateAny -AgeRecord
In case you need removal before that something like this should work:
Remove-DnsServerResourceRecord -ZoneName "domain.com" -Name "recordname" -RRType "A" -Force
I managed to overcome this by using ye olde classic CMD command:
dnscmd /RecordAdd domain.com recordname RecordType /Aging /OpenAcl A 192.168.0.0
This adds a new record, and seems to do so even when another record with the same name (but different IP) exists.
So in my case, I will have to use dnscmd to first remove the existing record before adding a new one (as I actually just want to update the IP address of the existing record).
Command for removing dns record :
dnscmd /recorddelete domain.com recordname
More details can be found here.

Execute task on hosts which match a pattern

We use this pattern for our hosts (and linux users):
coreapp_customer_stageID#server
I want to run fabric commands on a list of hosts which match a pattern.
Example: I want to run "date" on all hosts of core app "foocms" of customer "c1".
I could use roles, but there are a lot of customers ... A glob matching way would be good.
you can use this
#task
def set_hosts(lookup_param):
'''
'''
hosts=get_all_systems() # that needs to be implemented by you.
regex=re.compile('^%s' % lookup_param.replace('*', '.*'))
sub_hosts=[host for host in hosts if regex.match(host)]
if not sub_hosts:
raise ValueError('No host matches %s. Available: %s' % (lookup_param, hosts))
env.hosts = sub_hosts
#task
def date():
run('date')
Example: fab set_hosts:coreapp_customer1_* date
Taken from: http://docs.fabfile.org/en/1.7/usage/execution.html#the-alternate-approach