Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[shh] SSH Befehle in einem Script?
View unanswered posts
View posts from last 24 hours
View posts from last 7 days

Goto page 1, 2  Next  
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German) Diskussionsforum
View previous topic :: View next topic  
Author Message
3PO
Veteran
Veteran


Joined: 26 Nov 2006
Posts: 1110
Location: Schwabenländle

PostPosted: Sat May 15, 2010 5:03 pm    Post subject: [shh] SSH Befehle in einem Script? Reply with quote

Hallo Zusammen,

ich möchte verschiedene Befehle auf einem Remote-PC via SHH abarbeiten lassen.

Ich logge mich via RSA-Key ein, was auch funktioniert.

Wenn ich auf der Console folgendes eingebe:

Code:
ssh <IP_DES_REMOTE-PC>
<BEFEL_1>
<BEFEL_2>
<BEFEL_3>
<BEFEL_4>
logout


dann funktioniert das einwandfrei.

Wenn ich aber das ganze nun in ein Script packe und aufrufe, dann geht das nicht:

Code:
#!/bin/bash

ssh <IP_DES_REMOTE-PC>
<BEFEL_1>
<BEFEL_2>
<BEFEL_3>
<BEFEL_4>
logout

exit


Was mache ich falsch?
Back to top
View user's profile Send private message
manuels
Advocate
Advocate


Joined: 22 Nov 2003
Posts: 2146
Location: Europe

PostPosted: Sat May 15, 2010 5:36 pm    Post subject: Reply with quote

die BEFEHLE_X werden auf deinem lokalen Rechner ausgeführt, sobald du das SSH-Programm wieder schließt.

Du müsstest in etwa so das hier starten
Code:
ssh IP_NUMMER "BEFEHL_1 && BEFEHL_2 && ..."

EDIT: IP vergessen
_________________
Build your own live cd with catalyst 2.0!
Back to top
View user's profile Send private message
3PO
Veteran
Veteran


Joined: 26 Nov 2006
Posts: 1110
Location: Schwabenländle

PostPosted: Sat May 15, 2010 6:52 pm    Post subject: Reply with quote

Geht leider auch nicht. :(
Back to top
View user's profile Send private message
Evildad
Guru
Guru


Joined: 15 Apr 2004
Posts: 475

PostPosted: Sat May 15, 2010 6:55 pm    Post subject: Reply with quote

Ein paar Ausgaben sind hier sicher recht sinnvoll.
Was genau passiert denn wenn Du das Skript ausführst?

Grüsse
Back to top
View user's profile Send private message
3PO
Veteran
Veteran


Joined: 26 Nov 2006
Posts: 1110
Location: Schwabenländle

PostPosted: Sat May 15, 2010 7:01 pm    Post subject: Reply with quote

Evildad wrote:
Ein paar Ausgaben sind hier sicher recht sinnvoll.
Was genau passiert denn wenn Du das Skript ausführst?

Grüsse


Ich möchte damit eine VM auf einem Server starten:

Code:
vdr01 ~ # /var/epg/getepg/startVM_onServer.sh
Last login: Sat May 15 18:49:01 CEST 2010 from vdr01 on ssh
Server ~ # logout
Connection to server closed.
/var/epg/VMrun.sh: line 11: vmrun: command not found
bash: line 0: logout: not login shell: use `exit'
vdr01 ~ # /var/epg/getepg/startVM_onServer.sh



Die "startVM_onServer.sh" sieht so aus:
Code:
#!/bin/bash

EPG_SERVER="server"

ssh $EPG_SERVER "/var/epg/VMrun.sh start && logout"
exit
Back to top
View user's profile Send private message
tazinblack
Veteran
Veteran


Joined: 23 Jan 2005
Posts: 1146
Location: Baden / Germany

PostPosted: Sat May 15, 2010 7:24 pm    Post subject: Reply with quote

Also was Du auch machen kannst:

Du erstellst auf dem Zielrechner ein "Startskript" für die VM(s) und rufst dieses dann per SSH aus der Ferne auf.
Das finde ich oft einfacher. Denn bei einer Befehlskette per SSH passieren mir auch immer wieder Fehler.

Ich offe das kommt für Dich in Frage.
_________________
Gruß / Regards
tazinblack
_______________________________________________________
what's the point in being grown up if you can't be childish sometimes
Back to top
View user's profile Send private message
Evildad
Guru
Guru


Joined: 15 Apr 2004
Posts: 475

PostPosted: Sat May 15, 2010 7:26 pm    Post subject: Reply with quote

Also das funktioniert bei mir:

Code:

ssh root@<ip> "free -m  && df -h "


Logout musst Du durch exit ersetzen, dann ist die Fehlermeldung weg
Quote:
bash: line 0: logout: not login shell: use `exit'
Back to top
View user's profile Send private message
3PO
Veteran
Veteran


Joined: 26 Nov 2006
Posts: 1110
Location: Schwabenländle

PostPosted: Sat May 15, 2010 7:32 pm    Post subject: Reply with quote

Evildad wrote:
Also das funktioniert bei mir:

Code:

ssh root@<ip> "free -m  && df -h "


....


Das funktioniert bei mir auch, wenn ich es von der Console aus aufrufe, aber packe das doch mal in Script und Du wirst sehen, dass dann nicht geht.
Back to top
View user's profile Send private message
Evildad
Guru
Guru


Joined: 15 Apr 2004
Posts: 475

PostPosted: Sat May 15, 2010 7:36 pm    Post subject: Reply with quote

Da muss ich Dich leider enttäuschen...

Code:
./test.sh
             total       used       free     shared    buffers     cached
Mem:           ...
-/+ buffers/cache:        ...
Swap:           ...

Filesystem            Size  Used Avail Use% Mounted on
simfs                 ...



Als welcher User rufst Du das denn auf und wem gehört der RSA-KEY?
Der einzige Unterschied ist, dass ich ssh root@<ip> und Du ssh <ip> benutzt und der Key
Back to top
View user's profile Send private message
3PO
Veteran
Veteran


Joined: 26 Nov 2006
Posts: 1110
Location: Schwabenländle

PostPosted: Sat May 15, 2010 7:48 pm    Post subject: Reply with quote

Lustiger Weise geht das bei mir auch:


/tmp/test.sh:
Code:
#!/bin/bash

ssh server "free -m  && df -h "


Code:
vdr01 ~ # /tmp/test.sh
             total       used       free     shared    buffers     cached
Mem:          2015       1013       1001          0         60        794
-/+ buffers/cache:        158       1856
Swap:         5726         52       5673
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3             142G   32G  103G  24% /
udev                   10M  208K  9.8M   3% /dev
/dev/sda1              54M  8.1M   43M  17% /boot
vdr01 ~ #


Allerdings existeren die Befehle "free" und "df" auch auf beiden Systemen, wärend es "vmrun" bei mir nur auf dem Server gibt.
Back to top
View user's profile Send private message
Evildad
Guru
Guru


Joined: 15 Apr 2004
Posts: 475

PostPosted: Sat May 15, 2010 7:49 pm    Post subject: Reply with quote

Dann pack noch ein uname mit rein dann weisst wo der Befehl ausgeführt wird.
Bei mir waren es definitiv die Daten vom Zielserver.
Oder sind Deine Daten die des Zielservers?
Back to top
View user's profile Send private message
3PO
Veteran
Veteran


Joined: 26 Nov 2006
Posts: 1110
Location: Schwabenländle

PostPosted: Sat May 15, 2010 8:24 pm    Post subject: Reply with quote

Evildad wrote:
[...] Oder sind Deine Daten die des Zielservers?


Ja, sind die Daten des Zielrechners.
Back to top
View user's profile Send private message
manuels
Advocate
Advocate


Joined: 22 Nov 2003
Posts: 2146
Location: Europe

PostPosted: Sat May 15, 2010 9:18 pm    Post subject: Reply with quote

was ist denn die Ausgabe, wenn du
Quote:
ssh <user>@<ip> "/var/epg/VMrun.sh start"
startest?
_________________
Build your own live cd with catalyst 2.0!
Back to top
View user's profile Send private message
3PO
Veteran
Veteran


Joined: 26 Nov 2006
Posts: 1110
Location: Schwabenländle

PostPosted: Sat May 15, 2010 9:30 pm    Post subject: Reply with quote

manuels wrote:
was ist denn die Ausgabe, wenn du
Quote:
ssh <user>@<ip> "/var/epg/VMrun.sh start"
startest?


Dann kommt das:
Code:
vdr01 ~ # ssh root@server "/var/epg/VMrun.sh start"
/var/epg/VMrun.sh: line 11: vmrun: command not found
vdr01 ~ #



Einzeln gehen die Befehle aber auf der Console:
Code:
vdr01 ~ # ssh root@server
Last login: Sat May 15 23:25:09 CEST 2010 from vdr01 on ssh
Server ~ # /var/epg/VMrun.sh start
Server ~ # logout
Connection to server closed.
vdr01 ~ #


Nur eben aus einem Script heraus nicht.
Back to top
View user's profile Send private message
py-ro
Veteran
Veteran


Joined: 24 Sep 2002
Posts: 1734
Location: Velbert

PostPosted: Sat May 15, 2010 9:46 pm    Post subject: Reply with quote

Ich wette in deinem vmrun Skript sind keine absoluten Pfade angegeben. Geb die bitte mal an.

Py
Back to top
View user's profile Send private message
3PO
Veteran
Veteran


Joined: 26 Nov 2006
Posts: 1110
Location: Schwabenländle

PostPosted: Sat May 15, 2010 10:23 pm    Post subject: Reply with quote

py-ro wrote:
Ich wette in deinem vmrun Skript sind keine absoluten Pfade angegeben. Geb die bitte mal an.

Py


Wette gewonnen! :lol:

Genau daran hat es gelegen.

Das Script auf dem Server sah so aus:

Code:
#!/bin/bash

USER="root"
PASS="geheim"
VM="Windows XP Professional/Windows XP Professional.vmx"

case $1 in

     start)
      vmrun -T server -h 'https://localhost:8333/sdk' -u $USER -p $PASS start "[standard] ""$VM"
     ;;


     stop)
      vmrun -T server -h 'https://localhost:8333/sdk' -u $USER -p $PASS stop "[standard] ""$VM"
     ;;


     list)
      vmrun -T server -h 'https://localhost:8333/sdk' -u $USER -p $PASS list
     ;;

esac



Nachdem ich es geändert habe in:

Code:
#!/bin/bash

USER="root"
PASS="geheim"
VM="Windows XP Professional/Windows XP Professional.vmx"

case $1 in

     start)
      /opt/vmware/server/lib/bin/vmrun -T server -h 'https://localhost:8333/sdk' -u $USER -p $PASS start "[standard] ""$VM"
     ;;


     stop)
      /opt/vmware/server/lib/bin/vmrun -T server -h 'https://localhost:8333/sdk' -u $USER -p $PASS stop "[standard] ""$VM"
     ;;


     list)
      /opt/vmware/server/lib/bin/vmrun -T server -h 'https://localhost:8333/sdk' -u $USER -p $PASS list
     ;;

esac


geht es nun.

1000 THX für die Hilfe. :wink:
Back to top
View user's profile Send private message
3PO
Veteran
Veteran


Joined: 26 Nov 2006
Posts: 1110
Location: Schwabenländle

PostPosted: Sat May 15, 2010 10:37 pm    Post subject: Reply with quote

Übrigens,

mehrere Befehle hintereinader ausführen geht z.b. so: ;)

Code:
#!/bin/bash

ssh server "free -m" 
ssh server "df -h"
ssh server "uname -a"
Back to top
View user's profile Send private message
py-ro
Veteran
Veteran


Joined: 24 Sep 2002
Posts: 1734
Location: Velbert

PostPosted: Sun May 16, 2010 10:57 am    Post subject: Reply with quote

Folgendes geht definitiv auch.

Code:
ssh server 'free -m ; df -h ; uname -a'


Py
Back to top
View user's profile Send private message
3PO
Veteran
Veteran


Joined: 26 Nov 2006
Posts: 1110
Location: Schwabenländle

PostPosted: Sun May 16, 2010 1:10 pm    Post subject: Reply with quote

py-ro wrote:
Folgendes geht definitiv auch.

Code:
ssh server 'free -m ; df -h ; uname -a'


Py


Natülich, aber wenn es dann mal 20 oder mehr Kommandos sind, dann wird halt die Zeile doch etwas lang.
Back to top
View user's profile Send private message
69719
l33t
l33t


Joined: 20 Sep 2004
Posts: 865

PostPosted: Sun May 16, 2010 6:08 pm    Post subject: Reply with quote

3PO wrote:
Übrigens,

mehrere Befehle hintereinader ausführen geht z.b. so: ;)

Code:
#!/bin/bash

ssh server "free -m" 
ssh server "df -h"
ssh server "uname -a"

Kann man auch mittels
Code:

#!/bin/bash

ssh user@server "
cmd1
cmd2
cmd3
...
cmd20
"

lösen.
Back to top
View user's profile Send private message
schachti
Advocate
Advocate


Joined: 28 Jul 2003
Posts: 3765
Location: Gifhorn, Germany

PostPosted: Sun May 16, 2010 6:11 pm    Post subject: Reply with quote

3PO wrote:
py-ro wrote:
Folgendes geht definitiv auch.

Code:
ssh server 'free -m ; df -h ; uname -a'


Py


Natülich, aber wenn es dann mal 20 oder mehr Kommandos sind, dann wird halt die Zeile doch etwas lang.


Besser, als 20 Mal hintereinander eine ssh-Verbindung zu öffnen und wieder zu schließen - das müllt die Logs voll. :wink:
_________________
Never argue with an idiot. He brings you down to his level, then beats you with experience.

How-To: Daten verschlüsselt auf DVD speichern.
Back to top
View user's profile Send private message
Tinitus
Veteran
Veteran


Joined: 20 Sep 2004
Posts: 1754

PostPosted: Wed Aug 17, 2011 6:29 am    Post subject: Reply with quote

schachti wrote:
3PO wrote:
py-ro wrote:
Folgendes geht definitiv auch.

Code:
ssh server 'free -m ; df -h ; uname -a'


Py


Natülich, aber wenn es dann mal 20 oder mehr Kommandos sind, dann wird halt die Zeile doch etwas lang.


Besser, als 20 Mal hintereinander eine ssh-Verbindung zu öffnen und wieder zu schließen - das müllt die Logs voll. :wink:


Hallo,

habe Euren Thread mal ausgegraben. Leider kann ich in meinem Script kein Paßwort mit übergeben. Lt. Manpage gibt es keine -u -p Option bei ssh.. Ist das richtig? Wie macht ihr das? Paßwort jedes mal per Hand. Mit Schlüsseldateien ist das ein wenig lästig, da ständig neue Rechner mit gleicher IP hier aufschlagen.

G. Roland
Back to top
View user's profile Send private message
3PO
Veteran
Veteran


Joined: 26 Nov 2006
Posts: 1110
Location: Schwabenländle

PostPosted: Wed Aug 17, 2011 4:00 pm    Post subject: Reply with quote

@ Tinitus,

ich habe das mittlerweile mit "expect" gelöst. ;)

Info: --> http://bash.cyberciti.biz/security/expect-ssh-login-script/
Back to top
View user's profile Send private message
Tinitus
Veteran
Veteran


Joined: 20 Sep 2004
Posts: 1754

PostPosted: Wed Aug 17, 2011 7:03 pm    Post subject: Reply with quote

3PO wrote:
@ Tinitus,

ich habe das mittlerweile mit "expect" gelöst. ;)

Info: --> http://bash.cyberciti.biz/security/expect-ssh-login-script/


Hallo,

Danke für die Antwort. Welches gentoo Paket enthält das expect?
Warum unterstützt ssh keine Username und Paßwortangaben mehr?

G. Roland
Back to top
View user's profile Send private message
3PO
Veteran
Veteran


Joined: 26 Nov 2006
Posts: 1110
Location: Schwabenländle

PostPosted: Wed Aug 17, 2011 7:06 pm    Post subject: Reply with quote

Code:
dev-tcltk/expect


Info: http://www.nist.gov/el/msid/expect.cfm
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German) Diskussionsforum All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum