2

This question already has an answer here:

I got the task of logging into some 300 devices, execute the following commands and then copy the output into one file.

I can use SSH as a login method and I can give a predefined username and password.

Once we are in the device I need to log into expert mode and provide the predefined password.

$ expert
>>> provide password 
$ lspci | egrep -i --color 'network|ethernet'

Copy the output to file

I prepared the following script:

#!/bin/bash   
username=XXXX
passwd=XXX
cd /tmp

for host in `cat servers.txt`; 
do 
ssh $username@$host $passwd;

expert

echo "### $host ###" >> output.txt

lspci | egrep -i --color 'network|ethernet' >> output.txt

done

After prompting for the password, it gives me following:

Running commands is not allowed
./fibertest.sh: line 9: expert: command not found 

It seems like it is not running the command on the remote, but on the local machine.

marked as duplicate by Ulrich Schwarz, Anthon, don_crissti, Raphael Ahrens, G-Man Says 'Reinstate Monica' Sep 14 '15 at 18:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • I reformatted your code block and change (command) to $ not sure if that is actually what you meant. Please update the question if not. – Anthon Nov 12 '14 at 8:18
  • What exactly is the question? – Wtower Nov 12 '14 at 8:48
  • On another note, if you will be managing such number devices, you should look at using configuration management tools like Puppet or Chef. – Sree Nov 12 '14 at 10:46
  • Edited the question the way it made sense to me. Could someone review? Hope that clears things up. – Minix Nov 13 '14 at 10:45
0

You may give pssh (aka parallel ssh) a try, it runs command(s) on multiple machines and fetches the result / output in a local folder.

  • Hi Anthon,yes this is what i was trying to mean. – sonal khandekar Nov 13 '14 at 1:32
  • I prepared following script #!/bin/bash username=cpadmin passwd=vpn123 cd /tmp for host in cat servers.txt; do ssh $username@$host $passwd; expert echo "### $host ###" >> output.txt lspci | egrep -i --color 'network|ethernet' >> output.txt done but not getting proper output. its just keep on jumping to next server and never runs the commands – sonal khandekar Nov 13 '14 at 9:54
0

I suggest you start using Expect. Expect is tcl based language created for this kind of purposes - to interact (but really interact like remote commands and output parsing, etc...) with remote systems.

You can write really simple expect script and achieve a lot. Give it a try. There are modules for Perl and Python as well, so if you are keen on writing scripts in those languages you can.

Here is the list of some very good tutorials on Expect: http://wiki.tcl.tk/11584

I'm using Expect to manage around 40 servers, various admin tasks, collecting command outputs on defined interval, etc...

For example it's great if you have machines were you don't use SSH key auth and you don't want to use programs like sshpass, but you still want to be able to login (automatically, without human interaction) through script. This is just one example, but Expect is much, much more powerful than that.

0

In order to run the commands on the server using SSH you need to place the commands on the same line as the SSH command.

Example

EDIT: after thinking about it I removed the $password in the ssh command, because the server will try to execute that as the command. You are either required to setup ssh keys or combine this with an expect script to get the password in.

ssh $username@$host "expert; echo \"### $host ###\" >> output.txt; lspci | egrep -i --color 'network|ethernet' >> output.txt; scp output.txt $ADMIN_SERVER:$host-output.txt"

cat *-output.txt > output.txt

It's a bit of a long line, but all you need to do is wrap your commands in quotes and seperate them with semi-colons, remember to escape any quotes you use in the inner commands. Also remember that you'll want to copy your output files back to your admin server/wherever you're running the script form and then concatenate them all together.

  • Kind of sort of. I've never seen a ssh that takes the password like that on the command line, though. – Ulrich Schwarz Sep 14 '15 at 16:43
  • Could also use a heredoc: ssh user@host > output.file <<EOF (multiple lines of commands) EOF, minus the problem of providing the password. – Ulrich Schwarz Sep 14 '15 at 16:47
  • @UlrichSchwarz I haven't seen it either, but I assume that because it didn't throw an error for the asker that it might be working. I normally stick to keys if I want to automate, but then you have to set up keys first. Works great if you think of it before grooming 300 machines so their configuration includes them. Otherwise the asker will need expect to get the password in there – Centimane Sep 14 '15 at 17:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.