【问题标题】:Bash "if ping then else" My else is not workingBash“如果 ping 然后其他”我的其他不起作用
【发布时间】:2012-10-04 19:12:53
【问题描述】:

我有一个 bash 脚本,它应该根据我是否可以 ping 我的网络地址来工作。在某些情况下,此脚本在我的网络之外运行,在这种情况下,我不希望它运行。所以我把这个放在一起

if ping -c 1 xxx.xxx.x.x > /dev/null; then

当它在网络中时,“then”语句可以正常工作。但是,每当我关闭无线网卡并断开以太网电缆以测试“else”时,我的 else 语句永远不会被执行。我应该用另一种方式来写这个吗?

这是完整的脚本;

#!/bin/bash

computerid=`/usr/sbin/scutil --get LocalHostName`

# Standard parameters
domain="xxx.xxxxxxx.xxx"                            # fully qualified DNS name of Active Directory Domain
udn="xxxxxx"                                            # username of a privileged network user
password="xxxxxx"                                           # password of a privileged network user
ou="OU=MacOS,DC=xxx,DC=xxxxxxx,DC=xxx"              # Distinguished name of container for the computer

# Advanced options
alldomains="enable"                 # 'enable' or 'disable' automatic multi-domain authentication
localhome="enable"                  # 'enable' or 'disable' force home directory to local drive
protocol="smb"                              # 'afp' or 'smb' change how home is mounted from server
mobile="enable"                         # 'enable' or 'disable' mobile account support for offline logon
mobileconfirm="disable"             # 'enable' or 'disable' warn the user that a mobile acct will be created
useuncpath="enable"                 # 'enable' or 'disable' use AD SMBHome attribute to determine the home dir
user_shell="/bin/bash"              # e.g., /bin/bash or "none"
preferred="-preferred xxx.xxxxxx.xxx"       # Use the specified server for all Directory lookups and authentication
# (e.g. "-nopreferred" or "-preferred ad.server.edu")
admingroups="xxx\domain admins,xxx\enterprise admins,xxx\teacher98,xxx\ADManagement - Computers,xxx\admin employees"        # These comma-separated AD groups may administer the machine (e.g. "" or "APPLE\mac admins")

### End of configuration

# Delay the login window by unloading the com.apple.loginwindow
# LaunchDaemon in /System/Library/LaunchDaemons/

launchctl unload -w /System/Library/LaunchDaemons/com.apple.loginwindow.plist


## Wait until all network services are up.
sleep 10

# Check to see if we're in the district
if ping -c 1 xxx.xxx.x.x > /dev/null; then


    #make sure time is set correctly
    ntpdate -u time.xxxxxxx.xxx


    # Activate the AD plugin
    defaults write /Library/Preferences/DirectoryService/DirectoryService "Active Directory" "Active"
    plutil -convert xml1 /Library/Preferences/DirectoryService/DirectoryService.plist
    sleep 5

    # Remove computer from OU
    dsconfigad -f -r -u xxxxxx -p xxxxxx
    sleep 5

    # Bind to AD
    dsconfigad -f -a $computerid -domain $domain -u $udn -p "$password" -ou "$ou"

    # Configure advanced AD plugin options
    if [ "$admingroups" = "" ]; then
        dsconfigad -nogroups
    else
        dsconfigad -groups "$admingroups"
    fi

    dsconfigad -alldomains $alldomains -localhome $localhome -protocol $protocol \
        -mobile $mobile -mobileconfirm $mobileconfirm -useuncpath $useuncpath \
        -shell $user_shell $preferred

    # Restart DirectoryService (necessary to reload AD plugin activation settings)
    killall DirectoryService

    # Add the AD node to the search path
    if [ "$alldomains" = "enable" ]; then
        csp="/Active Directory/All Domains"
    else
        csp="/Active Directory/$domain"
    fi

    # This works in a pinch if the above code does not
    defaults write /Library/Preferences/DirectoryService/SearchNodeConfig "Search Node Custom Path Array" -array "/Active Directory/All Domains"
    defaults write /Library/Preferences/DirectoryService/SearchNodeConfig "Search Policy" -int 4
    defaults write /Library/Preferences/DirectoryService/ContactsNodeConfig "Search Node Custom Path Array" -array "/Active Directory/All Domains"
    defaults write /Library/Preferences/DirectoryService/ContactsNodeConfig "Search Policy" -int 4

    plutil -convert xml1 /Library/Preferences/DirectoryService/SearchNodeConfig.plist

    ## Remove the script and launchd job. Be sure to delete the script. 
    rm /Library/LaunchDaemons/com.xxxx.adbind.plist
    rm /usr/local/bin/adbind.bash
    launchctl unload -w /Library/LaunchDaemons/com.xxxx.adbind.plist

    # Remove the loginwindow delay by loading the com.apple.loginwindow   
    # LaunchDaemon in /System/Library/LaunchDaemons/

    launchctl load -wF /System/Library/LaunchDaemons/com.apple.loginwindow.plist

    exit 0

else

    echo "District not Available Quitting"

    # Remove the loginwindow delay by loading the com.apple.loginwindow   
    # LaunchDaemon in /System/Library/LaunchDaemons/

    launchctl load -wF /System/Library/LaunchDaemons/com.apple.loginwindow.plist

fi

exit 1

感谢您的任何建议!

【问题讨论】:

  • ping 在这些条件下在命令行上运行它的行为如何?
  • $ ping -c 1 xxx.xxx.x.x PING xxx.xxx.x.x (xxx.xxx.x.x): 56 个数据字节 ping: sendto: 没有到主机的路由 --- xxx.xxx。 x.x ping 统计 --- 1 个数据包传输,0 个数据包接收,100.0% 数据包丢失
  • 是的,但是退出代码是什么?
  • 对不起,我要表明我的真正无知,我不确定如何生成退出代码。这是我能做的简单的事情吗?我一直在阅读 ping man 但不知道如何。
  • 运行它,然后if [ $? ]; then echo true; else echo false; fi

标签: macos bash


【解决方案1】:

您可能需要使用超时 (-W) man ping

【讨论】:

  • 对不起 Nahuel,这对我的脚本没有帮助。我很欣赏这个想法。
猜你喜欢
  • 2021-09-16
  • 2016-07-21
  • 1970-01-01
  • 1970-01-01
  • 2016-04-24
  • 1970-01-01
  • 2011-03-07
  • 2018-01-03
相关资源
最近更新 更多