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:

#!/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:

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:

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:

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:
#!/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 :slight_smile:

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.

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:

./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?

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

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:

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:

#!/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

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:

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.

ssh is designed to prevent you automatically giving a rawtext password because this is insecure. It has better mechanisms for using the key noninteractively. ssh-agent can hold the key for you to reuse repeatedly once you enter the password just once:

$ ssh-agent > ~/.ssh-agent
$ . ~/.ssh-agent
$ ssh-add
Enter passphrase for key '/home/gaia_user/.ssh/id_rsa': 
# After this point, you should be able to run ssh noninteractively, ssh-agent will store the key until it's killed.

Hi,
I need to pass to ssh-agent the passphrase at the beginning of the script.
I found something about about this issue and I wrote this:

spawn ssh $USER_SERVER@$HOST_WHITEBOARD
expect "*ssh/id_rsa*"
send -- "${PASSW_SERVER}\r"
send -- "\r"

I have problem with command spawn and send.
If I launch them I receive these errors:

If I search these packets with yum I receive these information:

So I'm blocked at this point.

How can I pass the passphrase only one time at the beginning?
Thanks, bye bye.

I thought Spawn was part of expect and does not require any other external binary.

Anyways..... Could you try this and update the results:

Create two files and chmod 755 them:

File: GetData.sh

#!/bin/bash
./Query.exp  > /infoman/test/juniper/file.txt

File: Query.exp

#!/usr/bin/expect -f
spawn $env(SHELL)
send -- "/usr/bin/ssh user@IP"
expect password:
send "Password\r"
send "get system | include mac|Int|Ser|Software"
expect eof

in 2nd file I have put expect -f not -- and also I spawn SHELL not ssh directly ... and most of it is same.

Hi,
I tried to execute the script you wrote but I receive this error message:

What do I forget or what is the mistake I make?
Thanks, bye bye.

Apologies the backtick ` in first script should not be there !! [updated script in my previous post above]

... also try executing 2nd script basically it (2nd script) will throw the output ..

Please make sure expect is correct path ... as mentioned in script

I tried again but I receive error.
I removed the ` and I receive:

What is this error?
Thanks again, bye bye.

Have you chmod 755 the files

$chmod 755 GetData.sh
and also
$chmod 755 Query.exp

try running ./Query.exp

Yes, I did.
If I launch Query.exp I receive the error I posted before.

please provide output of following

$ which bash
$ which expect
$ uname -a
$ echo $SHELL
also run the command as

$ bash -x GetData.sh

I executed the commands and here there are the following results:

Thanks a lot.

Looks like the files are not in same directory. Can you make sure both the files in same directory. Also can you run Query.exp and provide the output.

$ ./Query.exp 

Yes, I'm sure. Files are in the same directory.

---------- Post updated at 12:31 PM ---------- Previous update was at 08:57 AM ----------

Hi,
I changed the name Query.exp in Query.sh and now I found the file.
But I have a problem, in file.txt I found:

What kind of problem is this?
Thanks, bye bye.


  1. ↩︎

  2. ↩︎