2つのイーサネットNICをWindows PowerShellで切り替えるか、Linux環境でコーディングするにはどうすればよいですか?


0

これらは私が今まで使っていたLinuxのルーティングコマンドです。

#!/bin/sh

# time checks in second
SLEEPTIME=58

#IP Address or domain to test ping.
TESTIP1=198.168.1.0
TESTIP2=198.168.1.1

#Ping timeout
TIMEOUT=15

# External interfaces
EXTIF1=eth0
EXTIF2=eth2

# IP external interfaces.
IP1=`/sbin/ifconfig $EXTIF1 | grep -i "117.239.106.82" | cut -f2 -d: | cut -f1 -d " "`
IP2=`/sbin/ifconfig $EXTIF2 | grep -i "14.139.183.178" | cut -f2 -d: | cut -f1 -d " "`

# Gateway
GW1=117.239.106.81  # provider 1
GW2=14.139.183.177     # provider 2

# Relative weights
W1=1
W2=1

# Broadband providers
NAME1=www.yahoo.com  
NAME2=www.gmail.com 

#success or failure before gateway change
SUCCESSREPEATCOUNT=4
FAILUREREPEATCOUNT=1

####### Do not change anything below this line #######

# Last link status indicates the macro status of the link we determined.
LLS1=1
LLS2=1

# Last ping status.
LPS1=1
LPS2=1

# Current ping status.
CPS1=1
CPS2=1

# Change link status indicates that the link needs to be changed.
CLS1=1
CLS2=1

# Count of repeated up status or down status.
COUNT1=0
COUNT2=0

while : ; do
 ping -W $TIMEOUT -I $IP1 -c 5 $TESTIP1 > /dev/null  2>&1
 RETVAL=$?

 if [ $RETVAL -ne 0 ]; then
 echo $NAME1 Down
 CPS1=1
 else
 CPS1=0
 fi

 if [ $LPS1 -ne $CPS1 ]; then
    echo Ping status changed for $NAME1 from $LPS1 to $CPS1
    COUNT1=1
 else
    if [ $LPS1 -ne $LLS1 ]; then
       COUNT1=`expr $COUNT1 + 1`
    fi
 fi

 if [[ $COUNT1 -ge $SUCCESSREPEATCOUNT || ($LLS1 -eq 0 && $COUNT1 -ge $FAILUREREPEATCOUNT) ]]; then

  echo Uptime status will be changed for $NAME1 from $LLS1
    CLS1=0
    COUNT1=0
        if [ $LLS1 -eq 1 ]; then
        LLS1=0
        else
        LLS1=1
        fi
 else
    CLS1=1
 fi

 LPS1=$CPS1

 ping -W $TIMEOUT -I $IP2 -c 5 $TESTIP2 > /dev/null  2>&1
 RETVAL=$?

 if [ $RETVAL -ne 0 ]; then
    echo $NAME2 Down
    CPS2=1
    else
    CPS2=0
 fi

 if [ $LPS2 -ne $CPS2 ]; then
    echo Ping status changed for $NAME2 from $LPS2 to $CPS2
    COUNT2=1
 else
 if [ $LPS2 -ne $LLS2 ]; then
    COUNT2=`expr $COUNT2 + 1`
 fi
 fi

 if [[ $COUNT2 -ge $SUCCESSREPEATCOUNT || ($LLS2 -eq 0 && $COUNT2 -ge $FAILUREREPEATCOUNT) ]]; then
    echo Uptime status will be changed for $NAME2 from $LLS2
    CLS2=0
    COUNT2=0
 if [ $LLS2 -eq 1 ]; then
    LLS2=0
 else
    LLS2=1
 fi
 else
    CLS2=1
 fi

 LPS2=$CPS2

 if [[ $CLS1 -eq 0 || $CLS2 -eq 0 ]]; then
    if [[ $LLS1 -eq 1 && $LLS2 -eq 0 ]]; then
    echo Switching to $NAME2
    ip route replace default via $GW2
    elif [[ $LLS1 -eq 0 && $LLS2 -eq 1 ]]; then
    echo Switching to $NAME1
    ip route replace default via $GW1
    elif [[ $LLS1 -eq 0 && $LLS2 -eq 0 ]]; then
    echo Restoring default load balancing
    ip route replace default scope global nexthop via $GW1 dev $EXTIF1 weight 1 \
    nexthop via $GW2 dev $EXTIF2 weight 1
    fi
 fi
 sleep $SLEEPTIME
don

WindowsでPowershellを使って2つのイーサネットNICを切り替えるか、Linux環境でコーディングするにはどうすればよいですか?

回答:


0

Win32_NetworkAdapter WMIクラスを使用して、一方のアダプタを無効にし、他方のアダプタを有効にします。

PS C:\Users\mmoor> $A=gwmi win32_networkadapter
PS C:\Users\mmoor> $A[0] | Get-Member


   TypeName: System.Management.ManagementObject#root\cimv2\Win32_NetworkAdapter

Name                        MemberType   Definition
----                        ----------   ----------
Disable                     Method       System.Management.ManagementBaseObject Disable()
Enable                      Method       System.Management.ManagementBaseObject Enable()
Reset                       Method       System.Management.ManagementBaseObject Reset()
SetPowerState               Method       System.Management.ManagementBaseObject SetPowerState(System.UInt16 PowerState, System.String Time)
AdapterType                 Property     System.String AdapterType {get;set;}
AdapterTypeId               Property     System.UInt16 AdapterTypeId {get;set;}
AutoSense                   Property     System.Boolean AutoSense {get;set;}
Availability                Property     System.UInt16 Availability {get;set;}
Caption                     Property     System.String Caption {get;set;}
ConfigManagerErrorCode      Property     System.UInt32 ConfigManagerErrorCode {get;set;}
ConfigManagerUserConfig     Property     System.Boolean ConfigManagerUserConfig {get;set;}
CreationClassName           Property     System.String CreationClassName {get;set;}
Description                 Property     System.String Description {get;set;}
DeviceID                    Property     System.String DeviceID {get;set;}
ErrorCleared                Property     System.Boolean ErrorCleared {get;set;}
ErrorDescription            Property     System.String ErrorDescription {get;set;}
GUID                        Property     System.String GUID {get;set;}
Index                       Property     System.UInt32 Index {get;set;}
InstallDate                 Property     System.String InstallDate {get;set;}
Installed                   Property     System.Boolean Installed {get;set;}
InterfaceIndex              Property     System.UInt32 InterfaceIndex {get;set;}
LastErrorCode               Property     System.UInt32 LastErrorCode {get;set;}
MACAddress                  Property     System.String MACAddress {get;set;}
Manufacturer                Property     System.String Manufacturer {get;set;}
MaxNumberControlled         Property     System.UInt32 MaxNumberControlled {get;set;}
MaxSpeed                    Property     System.UInt64 MaxSpeed {get;set;}
Name                        Property     System.String Name {get;set;}
NetConnectionID             Property     System.String NetConnectionID {get;set;}
NetConnectionStatus         Property     System.UInt16 NetConnectionStatus {get;set;}
NetEnabled                  Property     System.Boolean NetEnabled {get;set;}
NetworkAddresses            Property     System.String[] NetworkAddresses {get;set;}
PermanentAddress            Property     System.String PermanentAddress {get;set;}
PhysicalAdapter             Property     System.Boolean PhysicalAdapter {get;set;}
PNPDeviceID                 Property     System.String PNPDeviceID {get;set;}
PowerManagementCapabilities Property     System.UInt16[] PowerManagementCapabilities {get;set;}
PowerManagementSupported    Property     System.Boolean PowerManagementSupported {get;set;}
ProductName                 Property     System.String ProductName {get;set;}
ServiceName                 Property     System.String ServiceName {get;set;}
Speed                       Property     System.UInt64 Speed {get;set;}
Status                      Property     System.String Status {get;set;}
StatusInfo                  Property     System.UInt16 StatusInfo {get;set;}
SystemCreationClassName     Property     System.String SystemCreationClassName {get;set;}
SystemName                  Property     System.String SystemName {get;set;}
TimeOfLastReset             Property     System.String TimeOfLastReset {get;set;}
__CLASS                     Property     System.String __CLASS {get;set;}
__DERIVATION                Property     System.String[] __DERIVATION {get;set;}
__DYNASTY                   Property     System.String __DYNASTY {get;set;}
__GENUS                     Property     System.Int32 __GENUS {get;set;}
__NAMESPACE                 Property     System.String __NAMESPACE {get;set;}
__PATH                      Property     System.String __PATH {get;set;}
__PROPERTY_COUNT            Property     System.Int32 __PROPERTY_COUNT {get;set;}
__RELPATH                   Property     System.String __RELPATH {get;set;}
__SERVER                    Property     System.String __SERVER {get;set;}
__SUPERCLASS                Property     System.String __SUPERCLASS {get;set;}
PSStatus                    PropertySet  PSStatus {Availability, Name, Status, StatusInfo, DeviceID}
ConvertFromDateTime         ScriptMethod System.Object ConvertFromDateTime();
ConvertToDateTime           ScriptMethod System.Object ConvertToDateTime();


PS C:\Users\mmoor>

これにより、システム上のすべてのネットワークアダプタの配列が返されます。それから、最初のものをあなたのニーズに合った配列の関連するメンバーのメソッドに呼び出すことができます。例えば:

PS C:\Users\mmoor>$A[0].disable()
PS C:\Users\mmoor>$A[1].enable()

もちろん持っているネットワークアダプタの量や、使いたい特定のアダプタを見つけるためには、探しているプロパティを見つけるためにいくつかの反復を実装する必要があるかもしれませんが、これはおそらくそれが得られるのと同じくらい良いです/ パワーシェル。

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.