【问题标题】:IP Address Input from Jenkins to Variable powershell从 Jenkins 到变量 powershell 的 IP 地址输入
【发布时间】:2020-12-27 14:23:10
【问题描述】:

我有要求 IP 地址的 Jenkins 工作

$ip = $env:Lan_ip

用户输入的内容转到$ip

现在 $ip 是 192.168.10.10 示例

现在我正在尝试将此变量插入 FortiGate SSH

Invoke-SshCommand $Firewall -command ‘config system interface 
edit port1
set ip $ip 255.255.255.0
end’

但他看不懂$ip我需要让它像INT一样与.分开

我收到此错误

node_check_object fail! for ip $ip 

当用户输入 IP 地址时,我如何转换从用户那里获得的即时信息 --> 192.168.10.10 到代码中的可用变量

【问题讨论】:

    标签: powershell jenkins fortigate


    【解决方案1】:

    根据我从 here 收集到的信息,您需要将 子网掩码 作为 CIDR 格式的子网掩码,例如 255.255.255.0/24

    要从子网 IP 地址获取 CIDR 值,您可以使用此函数:

    function Get-SubnetMaskLength ([string]$SubnetMask) {
        # $cidr = Get-SubnetMaskLength "255.255.240.0" --> 20
        $result = 0
        [IPAddress]$ip = $SubnetMask
        foreach ($octet in $ip.GetAddressBytes()) {
            while ($octet) {
                $octet = ($octet -shl 1) -band [byte]::MaxValue
                $result++
            }
        }
        $result
    }
    

    所以

    $subNet = '255.255.255.0'
    $cidr = Get-SubnetMaskLength $subNet          # --> 24
    $subNetAndCidr = '{0}/{1}' -f $subNet, $cidr  # --> 255.255.255.0/24
    

    附注在代码中始终使用引号而不是卷曲的

    【讨论】:

      猜你喜欢
      • 2012-06-23
      • 2011-06-10
      • 1970-01-01
      • 2013-11-02
      • 2022-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多