如何在while和for中使用ssh

    最近在写一个自动更新的shell,可是发现如果在使用while循环从一个文件中读取ip地址,然后访问就只能读取第一行纪录。代码如下:
while read LINE
do
    echo *******************************************$LINE
    ssh 192.168.10.233 ls
done < data
  但是如果使用for就没有这个问题,非常的奇怪,for的代码如下:
for LINE in `cat data`
do
    echo *******************************************$LINE
    ssh 192.168.10.233 ls
done
  难道for和while竟然有这么大的差别,不可能吧。经过一番试验以后发现,原来并不是while的for的差别,而是while中使用重定向机制,data文件中的信息都已经读入并重定向给了整个while语句。所以当我们在while循环中再一次调用read语句,就会读取到下一条记录。问题就出在这里,ssh语句正好回读取输入中的所有东西,下面这个shell能很好的说明这个问题:
while read LINE
do
    echo *******************************************$LINE
    ssh 192.168.10.233 cat
done < data
执行这个shell就会发现,ssh中的cat语句会打印出data文件中的其他纪录,这就导致调用玩ssh语句后,输入缓存中已经都被读完了,当read语句再读的时候当然也就读不到纪录,循环也就退出了。改进方法是,将ssh的输入重定向输入:
while read LINE
do
    echo *******************************************$LINE
    ssh 192.168.10.233 ls < /dev/null
done <  data
  这样,在while语句中也能顺利的执行ssh语句了。linux的重定向还真复杂。

附录:测试用的data文件
line1
line2
line3
line4

  现在插入的代码怎么不能换行阿!
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
netterm 连接工具 NetTerm is a general purpose communications program designed to work with bulletin boards, local area networks, and the Internet network. Terminal emulation includes VT-52, VT-100, VT-220, VT-320, QNX 2, TVI-925, WYSE-50, IBM-3101, IBM-3151, IBM-3161, IBM-3163, XTERM, SCO ANSI, ANSI, Nixdolf BA-80, and FTTERM. So what is the difference between bulletin boards, local area networks, direct connections and the Internet? In short, the method that you use to connect to the host. If your computer contains a network card, you are on a local area network; if your local area network is connected to the Internet, you are both locally connected as well as Internet connected. If you must dial an Internet provider and establish a PPP or SLIP connection, then you are connected to the Internet. If you must dial a site direct, without using Trumpet or the Windows 95/NT dialup services, then you are using a direct modem connection, normally a bulletin board. NetTerm supports all of these. You should also be aware that NetTerm can handle any combination of the above. For example, you can have a NetTerm session connected to a host on the local area network, a site connected via the Internet, and a site connected by a direct modem connect all at the same time. All of the combinations are controlled through the phone book. So what emulation do I need? Well the answer to that depends upon the host that you connect to. Most sites will document the required emulation in their access documents, while others will provide a menu selection at connect time which allows you to specify the emulation type. In either case you must provide the emulation type in the phone book for that host. A good selection is ANSI if you are not sure. To use NetTerm, you first must determine your connection type and create a phone book entry for the host that you desire to connect to. In general, there are two major ways to connect to a host, (1) via direct modem dialup or (2) by a network either connected directly or by a dialup line. The connection type is referred to as Modem and TCPIP respectively. Although NetTerm contains many options, a new user can get up and running very quickly once the connection type is determined. For example, most users connect to the Internet with the Windows 95/NT dialup software or Trumpet under Windows 3.x. In this case, your phone book connection type will be TCPIP. NetTerm ships with several example phone book entries to help new users to get online quickly.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值