How to determine the subnet mask and network inter

2019-09-03 11:55发布

In a separate question (link below), it was suggested that I obtain the network interface card number and subnet mask from a remote SSH login, rather than prompting the user for it. All I have is the IP address of my remote host. I need to obtain the subnet mask to determine if the remote and local host are on the same subnet, and the interface card number to set up and configure a virtual IP for aliasing purposes.

Could someone suggest how I might be able to parse out the necessary information and return it to my shell script that initiates the remote ssh connection? My target hosts have Linux or AIX as the operating system. I am familiar with the netstat function, but I'm not sure if parsing information from this is valid, or if there is a better way to get what I need that will work for both Linux and AIX operating systems.

Thanks for any help you can provide!

How to return from shell 'read' command passed in expect script?

--UPDATE--

AIX ifconfig -a:

$ ifconfig -a
en0: flags=1e080863,480<UP,BROADCAST,NOTRAILERS,RUNNING,SIMPLEX,MULTICAST,GROUPR
T,64BIT,CHECKSUM_OFFLOAD(ACTIVE),CHAIN>
        inet 10.105.65.131 netmask 0xffff0000 broadcast 10.105.255.255
         tcp_sendspace 262144 tcp_recvspace 262144 rfc1323 1
lo0: flags=e08084b,c0<UP,BROADCAST,LOOPBACK,RUNNING,SIMPLEX,MULTICAST,GROUPRT,64
BIT,LARGESEND,CHAIN>
        inet 127.0.0.1 netmask 0xff000000 broadcast 127.255.255.255
        inet6 ::1%1/0
         tcp_sendspace 131072 tcp_recvspace 131072 rfc1323 1

AIX netstat -rn:

$ netstat -rn
Routing tables
Destination        Gateway           Flags   Refs     Use  If   Exp  Groups

Route tree for Protocol Family 2 (Internet):
default            10.105.65.1       UG       31  39412125 en0      -      -
10.105.0.0         10.105.65.131     UHSb      0         0 en0      -      -   =
>
10.105/16          10.105.65.131     U       219 985607244 en0      -      -
10.105.65.131      127.0.0.1         UGHS      5   1326738 lo0      -      -
10.105.255.255     10.105.65.131     UHSb      3   6926640 en0      -      -
127/8              127.0.0.1         U        36  11962928 lo0      -      -

Route tree for Protocol Family 24 (Internet v6):
::1%1              ::1%1             UH        1    393270 lo0      -      -

I tried route get and that doesn't work on my AIX box to tell me the route. The only thing I can get to work is netstat -rn. I'm not sure if there is another command similar to ip route that would work.

The Linux boxes support both ip and ifconfig.

I am not sure what to do when there are multiple network interface cards, as I do not know which one really should be used when setting up a virtual IP.

The Linux setup is more what I am concerned with, as I will be eventually adding in the AIX support for my software installation script later and can do more research on it then.

1条回答
Fickle 薄情
2楼-- · 2019-09-03 12:09

For Linux, I might do this something like so (using bash extensions, so invoked using a #!/bin/bash shebang, or piping the script over stdin to an interpreter invoked as ssh "$hostname" bash <<'EOF'):

internet_address=8.8.8.8 # read data for the NIC used to route here
dev_re='dev ([^[:space:]]+)($|[[:space:]])'
read default_route < <(ip -o route get "$internet_address")
[[ $default_route =~ $dev_re ]] && devname=${BASH_REMATCH[1]}

IFS=$'\n' read -r -d '' -a addresses < \
  <(netstat -rn | 
    awk -v dev="$devname" '$8 == dev && ($2 == "0.0.0.0" || $2 == "default") { print $1 }')

# emit this output however you like
printf '%s\n' "$dev_re" "${addresses[@]}"
查看更多
登录 后发表回答