Komutu ssh ve çalıştırmak için komut dosyası çalışmıyor


10

Komut dosyası aşağıdadır.

Birkaç sunucuda oturum açmak ve çekirdek sürümünü kontrol etmek istedim.

#!/bin/bash
#input server names line by line in server.txt
cat server.txt | while read line
do
sshpass -p password ssh root@$line << EOF
hostname
uname -r
EOF
done

Gibi çıktı çıktı beklenir ..

server1_hostname
kernel_version
server2_hostname
kernel_version

ve bunun gibi..

Bu komut dosyasını server.txt dosyasında yaklaşık 80 sunucuyla çalıştırdım

Ve ben çıktı çıktı gibiydi .....

Pseudo-terminal will not be allocated because stdin is not a terminal. 
Pseudo-terminal will not be allocated because stdin is not a terminal.
Pseudo-terminal will not be allocated because stdin is not a terminal.
Pseudo-terminal will not be allocated because stdin is not a terminal.
Pseudo-terminal will not be allocated because stdin is not a terminal.
Pseudo-terminal will not be allocated because stdin is not a terminal.
Pseudo-terminal will not be allocated because stdin is not a terminal.
Pseudo-terminal will not be allocated because stdin is not a terminal.
Pseudo-terminal will not be allocated because stdin is not a terminal.

========================================================================
================================ WARNING ===============================
========================================================================
This system is solely for the use of authorized personnel. Individuals
using this system are subject to having some or all of their activities
monitored and recorded. Anyone using this system expressly consents to
such monitoring and is advised that any unauthorized or improper use of
this system may result in disciplinary action up to and including
termination of employment. Violators may also be subject to civil and/or
criminal penalties.
========================================================================

Warning: no access to tty (Bad file descriptor).
Thus no job control in this shell.
xxxxdev01
2.6.32-431.23.3.el6.x86_64
Pseudo-terminal will not be allocated because stdin is not a terminal.
Pseudo-terminal will not be allocated because stdin is not a terminal.
Pseudo-terminal will not be allocated because stdin is not a terminal.
Pseudo-terminal will not be allocated because stdin is not a terminal.
Pseudo-terminal will not be allocated because stdin is not a terminal.

Burada sadece 1 ana bilgisayar için çıktı var, xxxxdev01ve bu da ssh afiş ve diğer uyarı ile geliyor.

Ben diğer tüm ana çıktı ve ssh afiş olmadan .. Burada ne oluyor?


Sunuculardan birini manuel olarak kullanır ve çalıştırırsanız ne olur sshpass -p password root@server histname?
terdon

1
ssh -t -t root@Bir sözde terminali zorlamak için ... tuşunu kullanın .
garethTheRed

Yanıtlar:


11

hostnameVe unamekomutlarından beklenen çıktıyı neden almadığınızı söyleyemem , ancak yabancı metinlere yardımcı olabilirim.

"Pseudo-terminal" satırları ssh, komut satırında yürütülecek komut sağlanmadığında varsayılan olarak bir TTY ayırmaya çalıştığından yazdırılıyor . Ssh komutuna "-T" ekleyerek bu iletiden kaçınabilirsiniz:

sshpass -p password ssh -T root@$line

Uzak sistemdeki kabuktan "Uyarı: tty'ye erişim yok" satırı geliyor. cshve tcshbu mesajı belirli koşullar altında basacaktır. .cshrcTTY gerektiren bazı özelliklere erişmeye çalışarak uzak sistemdeki veya benzer bir dosyadaki bir şey tarafından tetiklenmesi mümkündür .


4

Aşağıdaki kodu kullanın,

#!/bin/bash
#input server names line by line in server.txt
cat server.txt | while read line
do
  sshpass -p password ssh root@$line 'hostname;uname -r'
done

İlk denediğim bu. Ama bana beklenen çıktıyı vermiyor.
Gokul olmak

1

Ana bilgisayarlarınız aşağıdaki gibi depolanırsa server.txt

host1.tld
host2.tld
....

Yapabilirsin

mapfile -t myhosts < server.txt; for host in "${myhosts[@]}"; do ssh username@"$host" 'hostname;uname -r'; done

1

Stdin'e uzak komutlarınız erişemez. Yapabileceğiniz şey stdin komutlarını okumak için bash "-s" bayrağını kullanmaktır:

Bash kılavuzundan:

-s        If the -s option is present, or if no arguments remain after
          option processing, then commands are read from the standard 
          input.  This option allows the positional parameters to be set
          when  invoking  an  interactive shell.

Yani bu istediğinizi yapmalı:

#!/bin/bash
#input server names line by line in server.txt
cat server.txt | while read line
do
    sshpass -p password ssh root@$line bash -s << EOF
hostname
uname -r
EOF
done

Ayrıca bakınız: /programming/305035/how-to-use-ssh-to-run-shell-script-on-a-remote-machine


1

Bu benim için harika çalışıyor:

 # cat hostsname.txt 
operation01  172.20.68.37 5fDviDEwew
ngx-gw01     172.20.68.36 FiPp2UpRyu
gateway01    172.20.68.35 KeMbe57zzb
vehicle01    172.20.68.34 FElJ3ArM0m

# cat hostsname.txt | while read hostname ipaddr passwd; do sshpass -p $passwd /usr/bin/ssh-copy-id $ipaddr;done

dikkat: -t -tyerine -Thatayı önlemek için

Stdin bir terminal olmadığı için sözde terminal ayrılmayacak


1
Biçimlendirme konusunda okumalısınız. Cevabınız mükemmel olabilir ama şimdi okunamıyor.
roaima

0

Sanırım ssh ise geri kalanını stdin ye. ayrıntılar için Bash FAQ 89'a başvurabilirsiniz . Bir FileDescriptor ile, aşağıdaki kodlar beklenti olarak çalışmalıdır.

while read line <& 7
do
sshpass -p password ssh root@$line << EOF
hostname
uname -r
EOF
done 7< server.txt

alternatif yol ssh için / dev / null kullanmaktır. FileDescriptor yok sayılabilir. `satır okurken; sshpass -p şifre yapmak ssh root @ $ line </ dev / null << EOF .... bitti <server.txt`
Leon Wang
Sitemizi kullandığınızda şunları okuyup anladığınızı kabul etmiş olursunuz: Çerez Politikası ve Gizlilik Politikası.
Licensed under cc by-sa 3.0 with attribution required.