SSH shell script does not work


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SSH shell script does not work
# 1  
Old 04-20-2010
Need help: SSH shell script does not work

Hello
I have a Zabbix Server were Linux kernel version 2.6.26-2-amd64 is running.
now my Question how can i make a script that does:
- connect with ssh to a device
- get the systeminfo (but only the interfaces, mac adresses, serialnumber and Software version)
- write the output in a file

Now i made this code but it doesnt work:
Code:
Code:

#!/bin/bash
#!/usr/bin/expect --
spawn ssh user@IP     > /infoman/test/juniper/file.txt
expect password:
send "Password\r"
send "get system | include mac|Int|Ser|Software"
expect eof

Now when i start the script it says this:
Code:
Errormessage:

./ZAJOH01_IP.sh: line 3: spawn: command not found
couldn't read file "password:": no such file or directory
./ZAJOH01_IP.sh: line 5: send: command not found
./ZAJOH01_IP.sh: line 6: send: command not found
couldn't read file "eof": no such file or directory

If i tip the command by myself it work that means i write:
Code:
ssh user@IP > /infoman/test/juniper/file.txt
Password
get system | include mac|Int|Ser|Software
exit

then it write in the file /infoman/test/juniper/file.txt:
Code:
Remote Management Console
Device-> get system | include mac|Int|Ser|Soft
Serial Number: xxxxxxxxxxxxxx, Control Number: 00000000
Software Version: 5.xx.xrxx.x, Type: Firewall+VPN
Base Mac: 0010.db44.xxxx
Use interface IP, Config Port: 8005
User Name: ig-root
Interface ethernet1:
  *ip 10.182.128.14/23   mac 0010.db44.xxxx
  *manage ip 10.182.128.14, mac 0010.db44.xxxx
Interface ethernet2:
  *ip 0.0.0.0/0   mac 0010.db44.xxxx
Interface ethernet3:
  *ip 0.0.0.0/0   mac 0010.db44.xxxx
Interface ethernet4:
  *ip 196.30.139.43/28   mac 0010.db44.xxxx
  *manage ip 196.30.139.43, mac 0010.db44.xxxx
Device-> exit

This Shellscript is automated created with a PERL script that look like this:
Code:
Code:
#!/usr/bin/perl -w

#############################################
### Creator: Kim Nussbaumer                                            ###
### Date:    19.04.2010                                                   ###
### Version: 0.1                                                             ###
### File:    jun_createIP_sh.pl                                           ###
#############################################

use Text::Trim;


### Include
require "/infoman/test/juniper_backup.conf";            


### backup.sh leeren
open (BK, '>', 'jun_backup.sh');
print BK "#!/bin/bash\n";
close (BK);


### devices.txt auslesen und zeilen in @lines abfuellen
open (FILE,$devpath);
#my @lines = <FILE>;


### zeilen von @lines verarbeiten
while (<FILE>) {
 
### Nach ' ' spliten
 @singleline = split(/ /,$_);

### Shellfile erstellen
 my $filename = "/infoman/test/devices/".$singleline[0]."_IP.sh";
 print $filename." added\n"; 


### Shellfile fuellen
 open (MYFILE, '>', $filename);
 print MYFILE "#!/bin/bash\n";
 print MYFILE "#!/usr/bin/expect --\n";
 print MYFILE "spawn ssh $singleline[2]\@$singleline[1] > $bkppath$singleline[0].txt\n";
 print MYFILE "expect password:\n";
 print MYFILE "send \"".trim($singleline[3])."\\r\"\n";
 print MYFILE "send "get system | include mac|Int|Ser|Software"";
 print MYFILE "expect eof";
close (MYFILE);

chmod 0755, $filename;


### Filename dem Backupfile hinzufuegen
open (BKFILE, '>>', 'jun_backup.sh');
print BKFILE "$filename\n";
close (BKFILE);

}

If you have any question just ask i'm avaible

Thanks!

I'm New at scripting with shell and perl so plz help i need it for my work Smilie

Last edited by Fraggy; 04-20-2010 at 09:12 AM.. Reason: Output
# 2  
Old 04-20-2010
Bumping up posts or double posting is not permitted in these forums.

Please read the rules, which you agreed to when you registered, if you have not already done so.

Thank You.

The UNIX and Linux Forums.
# 3  
Old 04-20-2010
instead of

spawn ssh user@IP

can you put complete path of ssh and try

spawn /usr/bin/ssh user@IP
# 4  
Old 04-20-2010
Quote:
Originally Posted by chakrapani
instead of

spawn ssh user@IP

can you put complete path of ssh and try

spawn /usr/bin/ssh user@IP
HI, first thanks for try to help

Now i tried it and this is the error message:
Code:
./MXCUA01_IP.sh: line 3: spawn: command not found
couldn't read file "password:": no such file or directory
./MXCUA01_IP.sh: line 5: send: command not found
./MXCUA01_IP.sh: line 6: send: command not found
couldn't read file "eof": no such file or directory

must i include some modules for send?
Does spawn work with the command ssh?
Code:
i tried it without spawn but then it ask me about the password and i enterd it and den i write exit to close the ssh session and then he tried to do the rest of the shellscript :/
so how can i open a ssh session in a shell script or something
Here the Input:

USER@IP's password: 
PTY allocation request failed on channel 0
Connection to IP closed.

couldn't read file "password:": no such file or directory
./MXCUA01_IP.sh: line 5: send: command not found
./MXCUA01_IP.sh: line 6: send: command not found
couldn't read file "eof": no such file or directory
asrv-12:/infoman/test/devices#

spawn works with scp but with ssh?..

Greets

Last edited by Fraggy; 04-20-2010 at 09:46 AM.. Reason: Answer
# 5  
Old 04-20-2010
Lose the first line in your script, because you're trying to run an expect script inside an shell.
# 6  
Old 04-20-2010
Quote:
Originally Posted by pludi
Lose the first line in your script, because you're trying to run an expect script inside an shell.
did you mean #!/bin/bash or #!/usr/bin/except -- ?
anyway i tried both one time without bash and onetime without except and without bash gaves an error that he dont know ssh and without except it doesnt change anything, he also wanted a password (which i gave him manualy) and after closing the session he said:
Code:
couldn't read file "password:": no such file or directory
./MXCUA01_IP.sh: line 5: send: command not found
./MXCUA01_IP.sh: line 6: send: command not found
couldn't read file "eof": no such file or directory

this is now my code:
Code:
#!/bin/bash
/usr/bin/ssh user@IP > /infoman/test/juniper/MXCUA01.txt
expect password:
send "password\r"
send "get system | include mac|Int|Ser|Software"
expect eof

# 7  
Old 04-20-2010
Hi,
I have a similar problem.
I have to connect through ssh to a remote host from a script file.
I try "ssh user@host" but then system asks me:
Quote:
Enter passphrase for key '/home/gaia_user/.ssh/id_rsa':
How can I pass the passphrase? I read about "spawn command" but I don't have this command and I cannot install it.
How can I solve?
Thanks, bye bye.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Shell script - if statements dont work

hi all, i have made a shell script and it runs until it reaches the if statement, doesn't the ! mean only if the command fails it will echo me that message and then exit can anyone please help me what is wrong with my code? many thanks, rob #!/bin/bash echo "is this archive... (10 Replies)
Discussion started by: robertkwild
10 Replies

2. Shell Programming and Scripting

Ssh remote command doesn't work from script file

I have 10 application servers in a distributed architecture generating their own application logs. Each server has application utility to continuously tail the log. for example following command follows tails and follows new logfiles as they are generated server1$ logutility logtype When I run... (8 Replies)
Discussion started by: indianya
8 Replies

3. Shell Programming and Scripting

Shell script to work on dates

Hi Sir/Madam I have a file data.txt like below file_name date_of_creation x 2/10/2012 y 8/11/2010 z 11/3/2013 a 2/10/2013 b 3/10/2013 c ... (4 Replies)
Discussion started by: kumar85shiv
4 Replies

4. Shell Programming and Scripting

Ssh agent forwarding in script did not work

Sorry for the wrong question. (2 Replies)
Discussion started by: hce
2 Replies

5. Shell Programming and Scripting

ssh does not work in script while running crontab

Hi All, I have prepared a small script to monitor few applications running on diff unix boxes(frontend/backed node1/node2 etc). ssh does not work for node2 when script executed from crontab..:wall: it work fine when i run it manually. Regards, Pavan (4 Replies)
Discussion started by: pavanchouksey
4 Replies

6. UNIX for Dummies Questions & Answers

How to work command 'cd' in shell script?

I have simple script. like that, I am working on /usr/local/src and also under src folder there is a ft folder #!/bin/ksh #!/bin/bash dirpath="/usr/local/src/ft" echo $dirpath cd $dirpath echo displays ok "/usr/local/src/ft" but that doesn't enter "ft" folder. stays in current... (4 Replies)
Discussion started by: F@NTOM
4 Replies

7. AIX

AIX Shell script does not work

Hi. I created schell script for sending messages to some processes in AIX: #!/bin/sh BSE=/infor/ERPLN/bse BSE_TMP=/infor/ERPLN/bse/tmp export BSE BSE_TMP for i in `ps -eo pid,comm | grep bshell | cut -f 1 -d " "` do /something $i done Unfortunatelly this script does not work on... (6 Replies)
Discussion started by: giovanni
6 Replies

8. Shell Programming and Scripting

How to use ssh execute other shell script on other host (shell script include nohup)?

i want use ssh on the host01 to execute autoexec.sh on the host02 like following : host01> ssh host02 autoexec.sh autoexec.sh include nohup command like follwing : nohup /home/jack/deletedata.sh & after i execute ssh host02 autoexec.sh one the host01. i can't found deletedata.sh... (1 Reply)
Discussion started by: orablue
1 Replies

9. Shell Programming and Scripting

$RANDOM does not work inside a shell script

Hi folks I'm coding on Ubuntu 9.04 standard shell. I'm writing a script that needs to generate a random number at some point of its execution. When I do echo $RANDOMas a command inside shell, I clearly get some randomly generated number However when I do i=`$RANDOM` echo $ior even... (14 Replies)
Discussion started by: ksk
14 Replies

10. Shell Programming and Scripting

I need to do a work to my job, but i m new in script shell, someone can help with this..

I need to do a work to my job, but i m new in script shell, someone can help with this.. :confused: Description Bsafe The command creates a backup directory of each month at the command line (arguments of the script). The names of directories to copy will always be specified for the... (4 Replies)
Discussion started by: strshel
4 Replies
Login or Register to Ask a Question