如何通过远程SSH登录确定子网掩码和网络接口卡? [英] How to determine the subnet mask and network interface card from remote SSH login?

查看:342
本文介绍了如何通过远程SSH登录确定子网掩码和网络接口卡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在另一个问题(下面的链接)中,建议我从远程SSH登录获取网络接口卡号和子网掩码,而不是提示用户输入.我所拥有的只是我的远程主机的IP地址.我需要获取子网掩码,以确定远程主机和本地主机是否在同一子网中,以及用于设置和配置用于别名的虚拟IP的接口卡号.

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.

有人可以建议我如何解析必要的信息,然后将其返回到启动远程ssh连接的shell脚本中吗?我的目标主机使用Linux或AIX作为操作系统.我对netstat函数很熟悉,但是我不确定从中解析信息是否有效,或者是否有更好的方法来获取所需的信息,该信息对于Linux和AIX操作系统均适用.

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!

如何从期望脚本中传递的shell读取"命令返回?

-更新-

AIX ifconfig -a:

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:

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      -      -

我尝试了route get,但是在我的AIX框上告诉我该路线不起作用.我唯一可以工作的是netstat -rn.我不确定是否还有另一个类似于ip route的命令将起作用.

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.

Linux盒同时支持ipifconfig.

The Linux boxes support both ip and ifconfig.

我不确定在有多个网络接口卡时该怎么办,因为我不知道在设置虚拟IP时真正应该使用哪一个.

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.

我更关心Linux设置,因为稍后我将最终为我的软件安装脚本添加对AIX的支持,然后可以对其进行更多研究.

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.

推荐答案

对于Linux,我可能会这样做(使用bash扩展名,因此使用#!/bin/bash shebang进行调用,或者通过stdin将脚本传递给解释器调用为ssh "$hostname" bash <<'EOF'):

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[@]}"

这篇关于如何通过远程SSH登录确定子网掩码和网络接口卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆