View Full Version : [Script BASH] Lanciare un comando ssh
Abdujaparov
20-04-2010, 12:34
Ciao a tutti,
dovrei lanciare, da uno script un comando su una macchina remota tramite ssh.
Il mio problema è il seguente come faccio ad aprire una sessione ssh?
Se io do ssh user@host mi chiede la password subito dopo....come posso fare per fornirla direttamente in input?
Grazie, ciao ciao.
DanieleC88
20-04-2010, 12:43
Con expect.
http://linux.die.net/man/1/expect
Abdujaparov
20-04-2010, 14:33
Ciao,
nel mio script ho inserito questi comandi:
ssh $USER_SERVER@$HOST_WHITEBOARD
/usr/local/bin/expect "*ssh/id_rsa*"
/usr/local/bin/send -- "${PASSW_SERVER}\r"
/usr/local/bin/send -- "\r"
Solo che quando lo eseguo mi vengono dati i seguenti errori:
./Test.sh: line 73: /usr/local/bin/spawn: No such file or directory
./Test.sh: line 74: /usr/local/bin/expect: No such file or directory
./Test.sh: line 75: /usr/local/bin/send: No such file or directory
./Test.sh: line 76: /usr/local/bin/send: No such file or directory
Cosa sbaglio?
Grazie, ciao ciao.
Abdujaparov
20-04-2010, 14:38
Mi sa che non ho spawn :(
lupoxxx87
20-04-2010, 15:11
prova a togliere /usr/local/bin dalle chiamate dei comandi
Abdujaparov
20-04-2010, 15:18
Ciao,
ho installato expect, spawn non lo trovo e non trovo send (se cerco com yum).
Ho una redhat. Cosa posso fare?
Ho provato a togliere /usr/local/bin e non mettere niente, come ho provato /usr/bin, come ho provato singolarmente i comandi.
Dopo l'installazione trovo solo expect.
Qualche idea?
Grazie, ciao ciao.
lupoxxx87
20-04-2010, 15:34
prova a digitare il nome del comando direttamente su terminale.
così se è installato ti dovrebbe dire che la sintassi è sbagliata, o alternativamente ti dovrebbe dire in quali pacchetti puoi reperirlo
Abdujaparov
20-04-2010, 15:41
Questo per spawn:
[gaia_user@server01 RMS_GSRWorkflow]$ spawn
bash: spawn: command not found
[gaia_user@server01 RMS_GSRWorkflow]$ /usr/local/spawn
bash: /usr/local/spawn: No such file or directory
e su yum:
[root@server01 RMS_GSRWorkflow]# yum search spawn
Loaded plugins: rhnplugin, security
======================================================================= Matched: spawn =======================================================================
inn.x86_64 : The InterNetNews (INN) system, an Usenet news server.
pexpect.noarch : Pure Python Expect-like module
Nessun paccheto spawn, ma altri pacchetti, devo installare quelli?
Per send:
[gaia_user@server01 RMS_GSRWorkflow]$ send
bash: send: command not found
[gaia_user@server01 RMS_GSRWorkflow]$ /usr/bin/send
bash: /usr/bin/send: No such file or directory
Pacchetti che mi vengono trovati:
[root@server01 RMS_GSRWorkflow]# yum search send
Loaded plugins: rhnplugin, security
======================================================================= Matched: send ========================================================================
lam.x86_64 : The LAM (Local Area Multicomputer) programming environment.
lam.i386 : The LAM (Local Area Multicomputer) programming environment.
ORBit2.i386 : A high-performance CORBA Object Request Broker
ORBit2.x86_64 : A high-performance CORBA Object Request Broker
arptables_jf.x86_64 : Userspace control program for the arptables network filter.
dbus.i386 : D-BUS message bus
dbus.x86_64 : D-BUS message bus
efax.x86_64 : A program for faxing using a Class 1, 2 or 2.0 fax modem.
exim.x86_64 : The exim mail transfer agent
inews.x86_64 : Sends Usenet articles to a local news server for distribution.
iputils.x86_64 : Network monitoring tools including ping.
mailx.x86_64 : The /bin/mail program for sending e-mail messages.
mgetty.x86_64 : A getty replacement for use with data and fax modems.
mgetty-sendfax.x86_64 : Provides support for sending faxes over a modem.
procps.x86_64 : System and process monitoring utilities.
psmisc.x86_64 : Utilities for managing processes on your system.
pump-devel.i386 : Development tools for sending DHCP and BOOTP requests.
pump-devel.x86_64 : Development tools for sending DHCP and BOOTP requests.
radvd.x86_64 : A Router Advertisement daemon
rsync.x86_64 : A program for synchronizing files over a network.
rsyslog.x86_64 : Enhanced system logging and kernel message trapping daemons
sendmail.x86_64 : A widely used Mail Transport Agent (MTA).
sendmail-cf.x86_64 : The files needed to reconfigure Sendmail.
sendmail-devel.i386 : Extra development include files and development files.
sendmail-devel.x86_64 : Extra development include files and development files.
sendmail-doc.x86_64 : Documentation about the Sendmail Mail Transport Agent program.
sg3_utils.x86_64 : Utils for Linux's SCSI generic driver devices + raw devices
sharutils.x86_64 : The GNU shar utilities for packaging and unpackaging shell archives.
spamassassin.x86_64 : Spam filter for email which can be invoked from mail delivery agents.
synaptics.x86_64 : Synaptics Touchpad Driver
sysklogd.x86_64 : System logging and kernel message trapping daemons.
Non mi sembra di scorgerli.....
DanieleC88
20-04-2010, 17:04
Mi pare che spawn e send siano dei comandi di expect, non dei veri e propri eseguibili.
Devi usare uno script del tipo:
#!/usr/bin/expect -f
<comando1>
...
<comandoN>
e rendere il file eseguibile. :)
ssh $USER_SERVER@$HOST_WHITEBOARD
/usr/local/bin/expect "*ssh/id_rsa*"
/usr/local/bin/send -- "${PASSW_SERVER}\r"
/usr/local/bin/send -- "\r"
Lo script non è valido, deve essere simile a questo se lo inserisci in uno script bash:
#!/bin/bash
HOST="localhost"
USER="myuname"
PASS="mypassword"
VAR=$(expect -c "
spawn ssh $USER@$HOST
expect \"password:\"
send \"$PASS\r\"
expect \"\\\\$\"
send \"ls\r\"
expect -re \"$USER.*\"
send \"logout\"
")
echo "==============="
echo "$VAR"
Una alternativa può essere quella di usare un certificato per non dover mai usare la password:
http://linuxproblem.org/art_9.html
Nel caso tu voglia pure un certificato protetto da password puoi usare anche la procedura sopra descritta con ssh-agent (per inserire una volta sola la password all'avvio)
http://mah.everybody.org/docs/ssh
Abdujaparov
21-04-2010, 09:19
Ciao,
grazie mille dell'aiuto. Sono riuscito a fare quello che serviva grazie ancora.
Ora ho un piccolo problema.
In pratica uso ssh-agent ed ssh-add, con quello che mi hai suggerito riesco correttamente ad eseguire l'operazione.
Adesso ho un altro problema.
Per esempio. Successivamente nello script eseguo il comando:
ssh server04
Effettivamente accedo a quel server ma a quel punto lo script non avanza con il comando seguente ma ottengo il controllo della console.
VAR=$(expect -c "
spawn ssh-add
expect \"password:\"
send \"$PASS\r\"
")
echo "==============="
echo "$VAR"
echo "Vado sulla macchina server04"
ssh server04
sqlplus / as dba<<EOF
startup
/
EOF
Dopo l'ssh ottengo la console. Se faccio exit, ad esempio, lo script continua l'esecuzione ma, a quel punto, sono su un'altra macchina ed il comando non ha alcun senso.
Come dovrei impostare il tutto per riuscire a far girare lo script?
Grazie mille, ciao ciao.
Per esempio. Successivamente nello script eseguo il comando:
ssh server04
Effettivamente accedo a quel server ma a quel punto lo script non avanza con il comando seguente ma ottengo il controllo della console.
"ssh nomeserver" ti apre una sessione interattiva
Se vuoi far eseguire dei comandi devi specificare il singolo comando in uno script da caricare sul sistema remoto preventivamente.
In alternativa puoi usare "here-document" per leggere i comandi da pipe, ad esempio
ssh server04 << END
sqlplus / as dba<<EOF
startup
/
EOF
END
Dovrebbe funzionare
Abdujaparov
21-04-2010, 14:34
Grazie mille!
Continuo ad avere un problema in una questione simile.
Nello script ho inserito il seguente codice:
ssh $HOST_WHITEBOARD<<END
COMMAND_GET_PID_WHITE="ps aux | grep gaia.dpct.pfs.launcher.PFSCoordinatorTask | awk '/Sl+/ {print \$2}' | awk 'begin {row=0} row==0 {print; row++} row>0 {next}'"
echo "Comando da lanciare:"
echo $COMMAND_GET_PID_WHITE
echo "Provo a prendere il PID"
PID=`eval $COMMAND_GET_PID_WHITE`
echo "Pid trovato: $PID"
kill -9 $PID
#eval $WHITEBOARD
END
Il problema l'ho a monte, nel senso che la variabile COMMAND_GET_PID_WHITE se provo a stamparla mi risulta vuota. Perchè? A me sembra logicamente corretto quello che provo a fare. Che sbaglio?
Grazie, ciao ciao.
Abdujaparov
22-04-2010, 09:48
Ciao ho risolto cambiato la prima riga da:
ssh $HOST_WHITEBOARD<<END
in
ssh $HOST_WHITEBOARD<<"END"
Grazie ancora, ciao ciao.
vBulletin® v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.