Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Notices

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 04-12-2011, 10:53 AM
damienorourke damienorourke is offline
Registered User
 
Join Date: Apr 2011
Posts: 7
using variables in a script.

Hi,

I want to generate a script that can do the following:

1. check the ip of a local machine in unix. I have the following command:

ifconfig -a | grep inet | awk '{print $2}' | grep -v 127.0.0.1
192.168.50.66

2. isql into a database and update a table with that ip address eg.
isql
1. update table set hostName = '192.168.50.66'
2. go

3. vi a txt file and add the ip addresse to do file using something like.
:%s/192.168.1.1/192.168.50.66/g


Do you know if this is possible using vbscript.


Thanks,
Damien
Reply With Quote
  #2  
Old 04-12-2011, 11:58 AM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
Hello damienorourke,

VBScript is a pretty powerful language, and should be able to help you accomplish much of what you're wanting to do. In your third step, it's probably not going to very easy to driving "vi" with any scripting language, VBScript or otherwise. You might try using sed instead of vi for the particular edit you list.

Most of the steps in your algorithm appear to require sending data to the remote, and reading data from the remote. Fortunately, this is easy to do with SecureCRT, and is discussed in Scripting Essentials: A Guide to Using VBScript in SecureCRT. VBScript examples are provided as well.

One way you might get started is by using the SecureCRT Script Recorder. If you select Start Recording Script in the Script menu, then press the Enter key, then type the commands you want to start with:
ifconfig -a | grep inet | awk '{print $2}' | grep -v "192.168.50.66 127.0.0.1"
Next, you'll "isql into the database" and run your commands to update the table.
Whatever commands are used to "isql into the database" are going to be embedded into the script, so you may want to be careful - if this includes usernames and passwords then your script will contain the usernames and passwords.

Now that you've "isql'd into the database" run the commands:
update table set hostName = '192.168.50.66'
go
I assume you exit the isql database before you update a text file (not using vi but instead using sed), so run some command to exit the isql database.
exit
quit
done

Now, update the text file using sed. I suggest sed because the "vi" search and replace string you listed is based on a sed search and replace string. I believe that the following string
:%s/192.168.1.1/192.168.50.66/g
is actually an "ex" command. The '%' percent means edit the current file, 's' means search, the '/' are delimiters, and 'g' is global. You're globally searching and replacing 192.168.1.1 with 192.168.50.66 in the current file. You can do that with sed, or other programs.

It could be similar to the following:
echo 127.1.1.1 >> ipfile.txt
sed 's/192.168.1.1/192.168.50.66/' ipfile.txt > newipfile.txt
(or cat ipfile.txt | sed 's/192.168.1.1/192.168.50.66/')
mv newipfile.txt ipfile.txt
If you prefer to do that with vi (the "ex" command string), you'll need to remember that you're sending ESCto get into "ex" command mode, it's going to be terribly ugly and difficult to maintain.

When you're finished running your commands, select Stop Recording Script and save your VBScript. You'll see that much of the script is based on waiting for strings - the command prompt generally, and sending strings. This is the information you'll find discussed in chapters 4 and 5 in detail in the SecureCRT VBScript Guide.

Does this information help you get started?
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #3  
Old 04-13-2011, 03:57 AM
damienorourke damienorourke is offline
Registered User
 
Join Date: Apr 2011
Posts: 7
Hi,

Thanks for your quick reply. Sed works as an excellent replacement for vi.

However I am still stuck with the isql part. I want to run this script on multiple machines. so don't want to embed the specific ip address in the script this will result in me updating the script before each run.

I can run the following command to give me the ip address of the machine.
"ifconfig -a | grep inet | awk '{print $2}' | grep -v 127.0.0.1
192.168.50.66"

I then need some way of taking that ip address and inserting it as " crt.Screen.Send "update table set hostName = '192.168.50.66'" & chr(13)"

Do you know if this is possible?

Thanks,
Damien


crt.Screen.Send "cd damien" & chr(13)
crt.Screen.WaitForString "/export/home/damien> "
crt.Screen.Send "isql -Usa -Ppassword " & chr(13)
crt.Screen.WaitForString "1> "
crt.Screen.Send "update table set hostName = '192.168.123.240'" & chr(13)
crt.Screen.WaitForString "2> "
crt.Screen.Send "go" & chr(13)
crt.Screen.WaitForString "1> "
crt.Screen.Send "quit" & chr(13)
Reply With Quote
  #4  
Old 04-13-2011, 05:14 AM
damienorourke damienorourke is offline
Registered User
 
Join Date: Apr 2011
Posts: 7
Smile Figured it out. Thanks

I got it working using sh script

$ sh
$ tmpval=`ifconfig -a | grep inet | awk '{print $2}' | grep -v 127.0.0.1`
$ ISQL="isql -Usa -Ppassword -w1024 -DPP_DB"
$ ${ISQL} << !EOF
> update table set hostName = '$tmpval'
> go
> !EOF
(1 row affected)

Thanks for all your help
Reply With Quote
  #5  
Old 04-13-2011, 02:45 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
Hi Damien,

Thanks for posting your solution. A VBScript solution would be similar - isolate the IP address just like you did. I would recommend using crt.Screen.ReadString to store the value in a variable for use later.

Code:
Sub Main()
    crt.Screen.Synchronous = True

    strPrompt = "> "

    'Get machine's IP address, completely isolated
    strCommand = _
        "ifconfig -a | " & _
        "grep inet | " & _
        "awk '{print $2}' | " & _
        "grep -v 127.0.0.1 | " & _
        "sed 's/addr://'"
    
    ' Send the command
    crt.Screen.Send strCommand & vbcr
    
    ' Wait for command to be echo'd back to us
    crt.Screen.WaitForString strCommand & vbcrlf
    
    ' Read everything up to a prompt, a carriage return, or
    ' linefeed, whichever comes first
    strResults = crt.Screen.ReadString(strPrompt, vbcr, vblf)

    MsgBox "IP Address: " & strResults
End Sub
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]

Last edited by miked; 04-13-2011 at 04:33 PM.
Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 03:00 PM.