login into multiple boxes to execute commands

B

birdboyee

Guest
Hello! We use a SecureCRT .vbs script to automate logging into multiple boxes, however, I'd like to have something run in Linux vs. our emulator. Below is my objective:

Our current .vbs script will look at an IP list .txt file on our PC, then login to each IP within the .txt, and execute the following:

Code:
ssh router1@[IP Address]
su - root  # We get prompted for PW.
su - secadmin2  # We do not get prompted for PW.
cd .ssh
cp authorized_keys authorized_keys_bkup
cat /app2/.ssh/Vkey >> authorized_keys
exit
exit
cd .ssh
cp Vkey Vkey.orig
cat Vkey >> authorized_keys
rm /app2/.ssh/Vkey
exit

Again, the script must prompt for root password (only once since all root PW's will be the same), and loop commands above for all IP's found on file. Can anyone help with this? Your assistance and cooperation is GREATLY appreciated!
 


Since you seem to understand the "programming part" of this, I will skip how to retrieve addresses / host names from a text file. Let us know if you need some help with this as well.

What you want to research is the expect command. This allows you to send command, parse results, then act on those results. For example, ssh to a server, expect to see a "username" prompt, send the username, expect to see a password prompt, send a password, expect to see a command prompt, send a command, etc.

A decent tutorial for expect is here:
http://www.pantz.org/software/expect/expect_examples_and_tips.html

We can help with a bit more if necessary - this should "get you close".

I am resisting the urge to post a whole script - I am a believer in the "give a man a fish / teach a man to fish" analogy.
 
Since you seem to understand the "programming part" of this, I will skip how to retrieve addresses / host names from a text file. Let us know if you need some help with this as well.

What you want to research is the expect command. This allows you to send command, parse results, then act on those results. For example, ssh to a server, expect to see a "username" prompt, send the username, expect to see a password prompt, send a password, expect to see a command prompt, send a command, etc.

A decent tutorial for expect is here:


We can help with a bit more if necessary - this should "get you close".

I am resisting the urge to post a whole script - I am a believer in the "give a man a fish / teach a man to fish" analogy.
Thanks Unixfix! Please don't resist lol! Im trying! :) I did read up on expect, very neat! I do need help on how to read a file, and loop thru each line. Below is the VB script used in SecureCRT to accomplish my goal, but a Linux version would be groovy!

Code:
# $language = "VBScript"
# $interface = "1.0"
'-------------------------------------------------------------------------------
' BIRDBOYEE
'-------------------------------------------------------------------------------
Sub Main
'------------------------------------------------------------
Dim ObjFSO, InitFSO, Wscript
sDir = "D:\Documents and Settings\BIRDBOYEE"
Set ObjFSO = CreateObject("UserAccounts.CommonDialog")
ObjFSO.Filter = "Text Files|*.txt|All Files|*.*"
ObjFSO.FilterIndex = 1
ObjFSO.InitialDir = sDir
InitFSO = ObjFSO.ShowOpen

If InitFSO = False Then
  Exit Sub
End If
'-----------------------------------------------------------
Const ForReading = 1

  Dim fso, file, str
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set file = fso.OpenTextFile(ObjFSO.FileName, ForReading, False)

  crt.Screen.Synchronous = True
  crt.Screen.Send "VMENU" & vbCr
  crt.Screen.WaitForString "Enter Form Name (or ?): "
  crt.Screen.Send "REIP" & vbCr
    crt.Sleep 1000
'----------------------------------------------------------- 
Do While file.AtEndOfStream <> True
  str = file.Readline
  crt.Screen.WaitForString "Enter Command I=Insert R=Review U=Update D=Delete: ", 60
  crt.Sleep 1000
    crt.Screen.Send "u" & vbCr
  crt.Screen.WaitForString ">"
  crt.Sleep 1000
    crt.Screen.Send "IPm=" & str & vbCr
  crt.Screen.WaitForString ">"
  crt.Sleep 1000
    crt.Screen.Send "IPs" & vbCr
  crt.Screen.WaitForString ">"
  crt.Sleep 1000
    crt.Screen.Send "u" & vbCr
 
  Loop
'----------------------------------------------------------
crt.Screen.WaitForString "Enter Command I=Insert R=Review U=Update D=Delete: ", 60
  crt.Sleep 1000
  crt.Screen.Send "<" & vbCr
    crt.Screen.WaitForString "Enter Form Name (or ?): ", 60
    crt.Sleep 1000
      crt.Screen.Send "<" & vbCr
'----------------------------------------------------------
crt.Screen.Synchronous = False
'----------------------------------------------------------
End Sub
 
My bad...i provided the wrong updated script for what i need help on.....

Code:
# $language = "VBScript"
# $interface = "1.0"
'-------------------------------------------------------------------------------
' BIRDBOYEE
'-------------------------------------------------------------------------------
Sub Main
'------------------------------------------------------------
Dim ObjFSO, InitFSO, Wscript
sDir = "D:\Documents and Settings\BIRDBOYEE"
Set ObjFSO = CreateObject("UserAccounts.CommonDialog")
ObjFSO.Filter = "Text Files|*.txt|All Files|*.*"
ObjFSO.FilterIndex = 1
ObjFSO.InitialDir = sDir
InitFSO = ObjFSO.ShowOpen

If InitFSO = False Then
  Exit Sub
End If
'-----------------------------------------------------------
Const ForReading = 1

  Dim fso, file, str
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set file = fso.OpenTextFile(ObjFSO.FileName, ForReading, False)

  crt.Screen.Synchronous = True
  crt.Screen.Send "grep rtr /etc/hosts" & vbCr
  crt.Screen.WaitForString ">"
'----------------------------------------------------------- 
Do While file.AtEndOfStream <> True
  str = file.Readline
  crt.Screen.Send "ssh root@" & str & vbCr
  crt.Screen.WaitForString ">"
  crt.Screen.Send "su - root" & vbCr
  crt.Screen.WaitForString ">"
    crt.Screen.Send "password" & vbCr    '#Manually enter password here.
  crt.Screen.WaitForString ">"
    crt.Screen.Send "su - secadmin2" & vbCr
  crt.Screen.WaitForString ">"
    crt.Screen.Send "cd .ssh" & vbCr
  crt.Screen.WaitForString ">"
    crt.Screen.Send "cp authorized_keys authorized_keys_bkup" & vbCr
  crt.Screen.WaitForString ">"
    crt.Screen.Send "cat /app2/.ssh/Vkey >> authorized_keys" & vbCr
  crt.Screen.WaitForString ">"
    crt.Screen.Send "exit" & vbCr
  crt.Screen.WaitForString ">"
    crt.Screen.Send "exit" & vbCr
  crt.Screen.WaitForString ">"
    crt.Screen.Send "cd .ssh" & vbCr
  crt.Screen.WaitForString ">"
    crt.Screen.Send "cp Vkey Vkey.orig" & vbCr
  crt.Screen.WaitForString ">"
    crt.Screen.Send "cat Vkey >> authorized_keys" & vbCr
  crt.Screen.WaitForString ">"
    crt.Screen.Send "rm /app2/.ssh/Vkey" & vbCr
  crt.Screen.WaitForString ">"
    crt.Screen.Send "exit" & vbCr
 
  Loop
'----------------------------------------------------------
crt.Screen.Synchronous = False
'----------------------------------------------------------
End Sub
 
I don't do VB, but I think I understand where this is going - you get a list of IP addresses or server names from all the *.txt files in a directory, then "do the expect stuff" against those. This will take two scripts, or an expect script and a shell command. This example assumes you are using bash.

I made an xx.txt that looks like this (I always use comments)

# Primary servers
10.10.10.1
10.10.10.2
10.10.10.3
# Secondary servers
10.20.10.1
10.20.10.2
10.20.10.3

So you need a command to get non-comment lines from your *.txt files, then pass that as a parameter to your expect script. A for loop will do that:

for hostiporname in `cat *.txt | grep -v "^\#"`
do
./expectscriptname.exp ${hostiporname}
done

or, on a single line

for hostiporname in `cat *.txt | grep -v "^\#"`; do ./expectscriptname.exp ${hostiporname}; done

This is telling you to type out *.txt, ignore lines that start with a "#" (the "^" in the grep), and assign that value to a shell variable hostiporname. Then this variable is passed to the expect script as a parameter. The {} around the name is not necessary, but good form.

The expect script will capture that variable as

set hostiporname "[lindex $argv 0]"

and you reference it in the script as $hostiporname

For example, you woudl probably have a line like this:

spawn ssh $hostiporname

There are many examples on the 'net on how to do an expect script to log on to a server and issue commands. One of these examples and what I've given you should get you there, or at least in the ballpark.

Make sure you address the new fingerprint issue - or that you have already set it manually before you automate the process!
 

Attachments

  • upload_2014-2-25_8-40-14.png
    upload_2014-2-25_8-40-14.png
    65.1 KB · Views: 1,373

Members online


Latest posts

Top