I would like to partially automate the configuration of our Aruba switch using the daily configuration *.cfg backups.
With the following command I get the desired block of a vlan from the Configfile:
$content=Get-Content -Path $switch.fullname | Select-String 'vlan 53' -Context 0,4
The result can then look like this, for example:
> vlan 53
name "WIFI"
tagged 5,8,47-52
no ip address
exit
Does anyone know how I can extract only the "tagged" vlan ports from $Content?
I would then like to use these in further steps for configuration.
Maybe via magic regex commands.
But unfortunately I am too stupid for that.
Many thanks in advance
Daniel
Related
I'm actually zero to Powershell scripting, but I need to write one, to go through a .txt file with ip-adresses wrote in one per line and run nslookup for each adress to get it's hostname and write it in file (or get an error message if there is no DNS record for used IP).
I found this topic this almost the same task: Getting IP addresses for hostnames using nslookup in Powershell
so I need a similar solution, but using IP-adresses as input to get hostnames (or error message) in output.
Does anyone able to help me?
Thank you.
The GetHostBy* methods of System.Net.DNS have been deprecated and marked Obsolete.
Use GetHostEntry:
foreach ($ipAddress in $ipAddresses) {
[Net.Dns]::GetHostEntry($ipaddress).HostName
}
Use the GetHosetByAddress method and access the HostName property to retrieve the HostName:
$ipAddresses = get-content "path_to_the_file"
foreach ($ipAddress in $ipAddresses) {
[System.Net.Dns]::GetHostByAddress($ipAddress).HostName
}
I am looking for a way using the command line in UNIX to email the results from a grep command.
I am grepping the error logs looking for a "searchword". I temporarily want to email the results to my work account. This is a temporary solution until the SA has the time to write a script that will write it to a file where the file will be read by an automated analyzing program.
sendmail joetester#workemail.com < grep searchword error*
Does anyone have an idea on how to do it that they can share. Thank you.
You want something like grep searchword error* | sendmail joetester#workemail.com, see this question.
im using procmail to forward emails to different folders in my Maildir.
I use these two lines to get the FROM and TO from the mail, which works pretty fine.
FROM=`formail -x"From:"`
TO=`formail -x"To:"`
These two commands return the whole line without the From: and To: prefix.
So i get something like:
Firstname Lastname <firstname.lastname#mail-domain.com>
Now i want to extract the email between < and >.
For this i pipe the variable FROM and TO grepping it like this.
FROM_PARSED=`echo $FROM | grep -o '[[:alnum:]+\.\_\-]*#[[:alnum:]+\.\_\-]*'`
TO_PARSED=`echo $TO | grep -o '[[:alnum:]+\.\_\-]*#[[:alnum:]+\.\_\-]*'`
But when i print FROM_PARSED into the procmail log by using LOG=FROM_PARSED, i get an empty string in FROM_PARSED and TO_PARSED.
But if i run these commands on my console, all works fine. I tried many other grepping methods, using grep, egrep, sed and even cut (cutting < and >). All working on console, but i use it in procmail it just returns nothing.
Is it possible that procmail is not allowed to use grep and sed commands? Something like a chroot?
I dont get any error logs in my procmail log. I just want to extract the valid email address from the FROM and TO line. Extracting with formail works, but parsing it with grep or sed fails, even if expression is correct.
Could somebody help? Maybe i need to setup procmail somehow.
Strange.
I added this to the users .procmailrc file
SHELL=/bin/bash
The users shell was set to /bin/false, which is correct because its a mail user, no ssh access at all.
You should properly quote "$FROM" and "$TO".
You will also need to prefix grep with LC_ALL=POSIX to ensure [:alnum:] will actually match the 26 well-known characters + 10 digits of the English alphabet.
You already solved this, but to answer your actual question, it is possible to run procmail in a chroot, but this is certainly not done by Procmail itself. Sendmail used to come with something called the Sendmail Restricted Shell (originally called rsh but renamed to remsh) which allowed system administrators to chroot the delivery process. But to summarize, this is a feature of the MTA, not of Procmail.
Is there a way to check if my domain is setup correctly on the nameservers? Ideally I'd like to run a command from command line, alternatively can use a third party tool.
for example, I'm trying to register a domain that's to be hosted on godaddy.com. I have set up the domain and can see it in my DNS list. The servers that are supposed to be setup are ns51.domaincontrol.com and ns52.domaincontrol.com, however my registration is bouncing saying that there are "No Nameservers found for {my new domain}".
I think this is the shortest and quickest command to list your nameservers:
host -t ns yourdomain.com
If you're on a Mac or Unix machine you can do the following from command line:
whois site.com
Toward the end it should give you a list of DNS servers.
You can filter the list, showing only the Name Server information, with:
whois site.com | grep "Name S"
Note that www.site.com and site.com are two different end points and you need to set up appropriately for both of them.
Zonecheck is a free software and can be run from the command line:
% zonecheck stackoverflow.com
ZONE : stackoverflow.com.
NS <= : ns51.domaincontrol.com. [216.69.185.26]
NS : ns52.domaincontrol.com. [208.109.255.26]
_______________
,---------------.|
~~~~ | warning || ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
`---------------'
w> Nameservers are all part of the same AS
| Adv: ZoneCheck
| To avoid loosing all connectivity with the authoritative DNS in case
| of a routing problem inside your Autonomous System, it is advised to
| host the DNS on different AS.
`----- -- -- - - -
: All the nameservers are part of the same Autonomous System (AS number
: 26496), try to have some of them hosted on another AS.
`..... .. .. . . .
=> generic
==> SUCCESS (but 1 warning(s))
There is a built-in command line for that: nslookup
Submit it, then write set type=ANY and press enter. After which specify desired domain and you'll get what you are looking for. Alternatively, enter ? and see more options for the nslookup command line.
PS: while this answer comes with a delay, hope it to be useful for others who want to check nameserver/DNS setup from command line.
nm-tool run from the command line will give you much information, not the least of which is the DNS servers you're using. You may have to install this, but many distros have it included.
I'm trying to dig through some logs and need information before and after the line I can match on. How would I do this in PowerShell ala "grep -C 2"?
In version 1, I can't wait for r2, then I get to put it on production machines :)
The PowerShell equivalent of grep is select-string. You can use the following.
cat file | select-string "pattern" -context 2
Note: this works in PowerShell v2.0 only.
Instead of using (gc $bigfile) again, which will cause PowerShell to read in $bigfile to memory on every object piped to it by the ForEach-Object cmdlet, you should probably read the file into a variable and then array index from that, like so:
$bigfile = gc 'c:\scripts\bigfile.txt'
$bigfile | Select-String "melissao" | % {$bigfile[($_.LineNumber -3)..($_.LineNumber +1)]}
Also, since the line numbering starts at 1 and array indexing starts at 0 you'll have to go backwards by 3, not 2, to get the line two spaces above "melissao", and go forwards by 1, not 2, to get the line two spaces below "melissao." Doing this will net you the 5 lines you want, "melissao" flanked by the two lines above and below it.
I'm not super familiar with grep -C 2, so I don't know if this replicates that functionality exactly.
Alas, it looks like you will have to build it yourself or wait for the next version of powershell, as it seems to have been implemented there. You can download a pre-release version of Powershell 2.0 from here.
Getting closer here- because Select-String returns MatchInfo objects which I can pull a line number out of (39017), now I just need to pull the line surrounding... so:
gc $bigfile | Select-String melissao |
%{(gc $bigfile)[($_.LineNumber -2)..($_.LineNumber +2)]}
If anyone could clean this up a bit to make it less slow, you may have the answer. Otherwise, this solution works but obviously not quickly.
Download grep for Windows, and call grep from PowerShell?