IF statement in Batch Script - if-statement

I'm writing a batch script that switches between ip addresses depending on the users input.
The core functionality of the script works however I want to add some checks into each function to check which setting is already applied and give a response accordingly.
My current code is:
#ECHO OFF
::Change Static IP address for simulator or network connection
:MAINMENU choose option from menu
::Set Current IP Address
for /F "tokens=2 delims=:" %%i in ('"ipconfig | findstr IP | findstr 192."') do SET LOCAL_IP=%%i
ECHO.
ECHO. Set Workstation...........1
ECHO. Set Simulator.............2
ECHO. Quit (no further action)..Q
ECHO.
choice /c:Q21
ECHO.
IF ERRORLEVEL 3 GOTO WORK
IF ERRORLEVEL 2 GOTO SIM
IF ERRORLEVEL 1 GOTO EOF
::Change IP To network settings
:WORK
IF "LOCAL_IP"=="192.168.1.4" (
ECHO. You are already in this mode
GOTO MAINMENU
) ELSE
(echo "Setting Static IP Information"
netsh int ip set address name="Local Area Connection" source=static addr=192.168.1.4 mask=255.255.255.0 gateway=192.168.1.254 gwmetric=1
netsh int ip set dns name="Local Area Connection" source=static addr=192.168.1.1
echo IP Address Set
goto MAINMENU)
::Set Simulator IP Address
:SIM
IF "LOCAL_IP"=="192.168.4.1" (
ECHO. You are already in this mode
GOTO MAINMENU
) ELSE
echo "Setting Static IP Information"
netsh int ip set address name="Local Area Connection" source=dhcp
netsh int ip set dns name="Local Area Connection" source=dhcp
netsh int ip set address name="Local Area Connection" source=static addr=192.168.4.1 mask=255.255.255.0
echo "Setting Static IP Information"
goto MAINMENU
:EOF
pause
I'm having trouble getting the IF statement to compare the current IP address with the one i want to set it to.
Can anybody suggest where I am going wrong or offer some ideas to get this working please?

I guess LOCAL_IP is an environment variable, to use them you have to enclose the name within % so your code will be:
IF "%LOCAL_IP%"=="192.168.1.4" (
and
IF "%LOCAL_IP%"=="192.168.4.1" (
Note: where do you set that environment variable to actual value?

Related

Getting EKS Service IP Address from DNS using nslookup using commandline and grep

I am having a question on how to retrieve the IP address of an EKS Service using the commandline using grep.
This is the nslookup command I used, and well it shows me the key Address: and the IPs but I only need one specific IP in that index and nothing else:
nslookup internal-a0cn71b0675e49.eu-central-1.elb.amazonaws.com | grep Address
This gives me something like this:
Address: 10.178.0.8#53
Address: 10.107.206.89
Address: 10.107.205.124
I just need the middle IP part without the Address in front, how can I get one IP using grep on the commandline?
For example how can I get only:
10.107.206.89
using nslookup and grep in a linux commandline
central-1.elb.amazonaws.com | awk '/Address/ { addr[cnt++]=$2 } END { print addr[2] }'
Search the output from nslookup for lines with "Address" using awk, create an array addr with the index incrementing and the value equal to the ip address. At the EnD, given we are only interested in the second address, print the second index of the addr array.

Regex Match for first line of Powershell Error Output

I am using new-pssession to connect to machines and am storing any errors in a value specified by the -errorVariable paramater. I want to call this variable later for output but on want to show the the first line, minus the comp name in brackets.
Heres an example of what i get.
New-PSSession : [ABCGFAXYZUT579] Connecting to remote server ABCGFAXYZUT579 failed with the
following error message : WinRM cannot complete the operation. Verify that the
specified computer name is valid, that the computer is accessible over the network, and that a
firewall exception for the WinRM service is enabled and allows access from this
computer. By default, the WinRM firewall exception for public profiles limits access to remote
computers within the same local subnet. For more information, see the
about_Remote_Troubleshooting Help topic.
At line:1 char:1
+ New-PSSession -ComputerName ABCGFAXYZUT579 -ErrorVariable errors912 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-
PSSession], PSRemotingTransportException
+ FullyQualifiedErrorId : WinRMOperationTimeout,PSSessionOpenFailed
I have tried passing the variable to select-string to get only what i need but everything i have tried still gives me this
[ABCGFAXYZUT579] Connecting to remote server ABCGFAXYZUT579 failed with the following error
message : WinRM cannot complete the operation. Verify that the specified computer name
is valid, that the computer is accessible over the network, and that a firewall exception for
the WinRM service is enabled and allows access from this computer. By default, the
WinRM firewall exception for public profiles limits access to remote computers within the same
local subnet. For more information, see the about_Remote_Troubleshooting Help topic.
I have also tried splitting it into seperate lines like this
($errors912 -split '\r?\n' -ne '') and piping to select-string but i still get the above returned.
is there an easy way to get everything after the first white space and before the first period? leaving this?
Connecting to remote server ABCGFAXYZUT579 failed with the following error
message : WinRM cannot complete the operation.
$NewSession = New-PSSession -ComputerName ABCGFAXYZUT579 -ErrorVariable errors912 -ErrorAction SilentlyContinue
if ( $errors912.Count -gt 0 ) {
[string]$errors912 -replace "^(\[.*?\] )|(?<=\.).*"
}
Connecting to remote server ABCGFAXYZUT579 failed with the following error message : The WinRM client cannot process the request.
The $errors912.Count -gt 0 condition is equivalent to $null -eq $NewSession.
Please go to some on-line regex tester/debugger e.g. https://regex101.com/ for the regex explanation.

How DNS cache clears in dnsmasq

Does dns cache clears after the max-cache-ttl seconds even if receives negative response from the parent name server which defined in resolv-file=/etc/resolv.dnsmasq..?
# Server Configuration
listen-address=127.0.0.1
port=53
bind-interfaces
user=dnsmasq
group=dnsmasq
pid-file=/var/run/dnsmasq/dnsmasq.pid
# Name resolution options
resolv-file=/etc/resolv.dnsmasq
cache-size=1000
neg-ttl=2
max-cache-ttl=5
domain-needed
bogus-priv
cat /etc/hosts
127.0.0.1 localhost
cat /etc/resolv.conf
nameserver 127.0.0.1
search eu-west-1.compute.internal
My domain resolves fine with #127.0.0.1 and not resolves with the parent name server in resolv.dnsmasq. So is it resolving from cache..? In that case I have max-cache-ttl as 5 seconds so does it maintain the cache if the parent name server provides negative response..?
The domain I am trying to dig ends with rds.amazonaws.com
Thanks in advance.

Handling telnet timeout/connection refused in pexpect python router

I am trying to write a script where I need to ssh multiple jump server to access a router.
e.g LocalMachine----ssh---server1---ssh---server2---telnet/ssh---router
Not sure what is the best way of doing it, i tried achieving it through pexpect.
My requirement is when i telnet from server server2 to router and it timeout/connection refused i want try ssh
any suggestions please?
import time, pexpect
child = pexpect.spawn('ssh username#server-1') ##########ssh to 1st Jump server
child.expect('password: ')
child.sendline('abc')
child.expect('$')
child.sendline('ssh username#server-2') ##########ssh to 2nd Jump server
print child.before
child.expect('password:')
child.sendline('xyz')
child.expect('$')
print child.before
host=raw_input("Enter Router name: ")
try:
print "Trying Telnet ", host
child.sendline(' telnet ' + host) ### router telnet at this point i want if telnet is timeout/connection refused try ssh
print child.before
except pexpect.TIMEOUT:
print child.before
else:
child.expect(':')
child.sendline("User")
child.expect(":")
child.sendline('passwprd')
child.expect('#')
child.sendline("\n")
child.expect("#")
finally:
print "Trying SSH ", host
child.sendline(' ssh -l User ' + host)
print child.before
child.expect(":")
child.sendline('password')
child.expect('#')
child.sendline("\n")
print child.before
child.interact()
The pexpect.TIMEOUT exception would be raised by the program instance running on your machine. It does not know if there is a timeout for the telnet instance running on the remote machine that you have ssh'ed to. The only way you can see if the telnet connection has a timeout or similar error, ie. connection refused, is to look at the output from the the process through pexpect. If you look at the documentation for expect it says that you can give a list of conditions - this could be one way to go.
For example:
index = p.expect (['timeout', 'connection refused]) # Fill in correct words :)

KeepAlived + HAProxy gets connection refused after a while

I´ve the next scenario, 4 VM´s running Red Hat Enterprise Linux 7:
20.1.67.230 server (VIRTUAL IP) (not a host)
20.1.67.219 haproxy1 (LOAD BALANCER)
20.1.67.229 haproxy2 (LOAD BALANCER)
20.1.67.223 server1 (LOAD TO BALANCE)
20.1.67.213 server2 (LOAD TO BALANCE)
My keepalived.conf file is:
vrrp_script chk_haproxy {
script "killall -0 haproxy" # check the haproxy process
interval 2 # every 2 seconds
weight 2 # add 2 points if OK
}
vrrp_instance VI_1 {
interface enp0s3 # interface to monitor
state MASTER# MASTER on haproxy1, BACKUP on haproxy2
virtual_router_id 51
priority 101 # 101 on haproxy1, 100 on haproxy2
unicast_src_ip 20.1.67.229 # This is the IP of the interface keepalived listens on
unicast_peer { # This is the IP of the peer instance
20.1.67.219
}
virtual_ipaddress {
20.1.67.230 # virtual ip address
}
track_script {
chk_haproxy
}
}
When a execute a request to the VIRTUAL IP, for instance:
curl server:8888/info
everything is ok, but just for a while, after some requests the command returns me : connection refused
So I´ve to restart the keepalived service manually , this way:
systemctl restart keepalived.service
The whole system seems work well, VRRP messages between haproxy1 and haproxy2 are OK, it´s just like the Virtual IP is not working properly.
Can anyone point me in the right direction to diagnose and fix this problem?
It was a networking issue. There was a device on the net with same IP as the Virtual IP I had chosen.