EXPECT script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting EXPECT script
# 1  
Old 10-15-2010
CPU & Memory EXPECT script

Hi,

unfortunately my programming skills are not the best although I try to write an expect script that connects via telnet to some switches, prompts a command and uploads the configuration to dedicated FTP server. Because it's an interactive process I coded in expect.

What I have and works is the code that manually takes input by executing the script, e.g.
Code:
expect ./cfgsaver hostname user password ftp-password

.

The script itself looks like this:

Code:
#!#/usr/bin/expect

if {[llength $argv] != 4 } {
        puts "ERROR: invalid command line arguments\n"
    puts " CFGSAVER 
    puts "Usage: cfgsaver <hostname> <user> <password> <ftp-password>"
    exit 1
}

# Record the parameters
set hostname [lindex $argv 0]
set user [lindex $argv 1]
set password1 [lindex $argv 2]
set password2 [lindex $argv 3]
set output "\nConfiguration of $hostname \n"

# Establish the connection to the device
spawn telnet $hostname
expect "login: "
send "$user\r"
expect "Password:"
send "$password1\r"

# Save configuration
set timeout 50
expect "admin>"
send "configupload\r"
expect ": "
send "ftp\r"
expect ": "
send "10.230.4.23\r"
expect ": "
send "user\r"
expect ": "
send "$hostname-config\r"
expect ": "
send "$password2\r"
expect "configUpload complete: All config parameters are uploaded"
send "\r"

# Exit Switch CLI
expect "admin>"
send "exit\r"
puts $file "\n$output"

# Move the configuration file to destination folder
exec mv /home/$hostname-config /tmp/cfgsaver/$hostname-config

What is missing is the part, where the script reads e.g. out of a file exactly the parameters "hostname user password ftp-password" line by line and executes my above code as there are lines.

I tried something but I don't get familiar with reading data out of a files, recording them into temporar variables and the while loop... If someone is a geek and has fun on coding these lines would be the best for me. Anyway all help would be great. Smilie

Cheers,
Jack
# 2  
Old 10-15-2010
As I am ignorant of the tk/tcl nature of expect scripting, I always use 'autoexpect' to write mine, and then use it as a template to generate a new expect script at run time with different parameters. If I used it often, I might learn!

One thing that removes the need to telnet is either ssh/ssh2 or rsh passwordless access by either saved keys or (~/.rhosts files always with IP and ID or host_name and ID or fully domain-qualified host_domain.host_name and ID), respectively. Ditto for ftp: scp/scp2 or rcp, plus NFS-like file sharing.

(It seems odd that the switch has a telnet server but not an ftp server, just an ftp client.)
# 3  
Old 10-15-2010
Quote:
What is missing is the part, where the script reads e.g. out of a file exactly the parameters "hostname user password ftp-password" line by line and executes my above code as there are lines.
On which computer is this file?


Quote:
# Move the configuration file to destination folder
exec mv /home/$hostname-config /tmp/cfgsaver/$hostname-config
On which computer should this "mv" execute?
# 4  
Old 10-15-2010
Maybe ftp under admin is not the usual ftp client, and needs no get or put? It does seem a bit out of order!

I think he is saying he needs to iterate a list of switches, and wants to do it in expect, not in a wrapper script. A wrapper would always be my general direction, if only for testing or reloading one host and escaping the world of expect.
# 5  
Old 10-18-2010
Hi, as there are confusions about the content of my current script, I'll give you additional information about it:
  1. The script itself will be executed on a server in a network connected to the switches.
  2. @methyl: the file containing the parameters shall be situated on the above mentioned server. The move command shall be executed on the server as well, because the ftpupload is unable to select another directoy as the users home drive. Therefore the move command moves the file into the destination directory.
  3. Telnetting servers is not the state of art and unsecure, I know. Anyway it's done so far and should be implemented in the script.
  4. The file containing the parameters may look like this:
Code:
10.17.120.120 admin password passw
10.17.120.121 admin password passw
10.17.120.122 admin passw123 passw

I hope this helps to understand what I'm doing.

@DGPickett: never heard of autoexpect but sounds much simpler than coding it myself in expect. I'll try that one out and post back.

Thanks so far.
Jack

---------- Post updated 18-10-10 at 09:19 AM ---------- Previous update was 17-10-10 at 07:50 PM ----------

Hi all,

autoexpect is a progress, but doesn't help me further as I'm going to get a loop and interating the loop as there are lines (=switches) in my source file. E.g. if cat the file I get all the lines. In best case the script loops my code as there are lines. I think this doesn't work with autoexpect, because when the lines in the file change (in case there will be more or less switches) the auto generated script won't work any more.

Any ideas for code I could use?

Cheers,
Jack
# 6  
Old 10-18-2010
Though we don't know what Operating Systems we have here, wouldn't his be much much easier with ftp autologin and a ".netrc" file ? See "man .netrc".

I can't see at the moment why the ftp cannot place the file in the correct place on the target server even if the file is first copied with ftp before renaming the file with ftp. It is good practice to ftp a file under a temporary name so it is quite clear to the remote computer when the ftp has finished.


Personally for a compatible unix-unix copy in an insecure environment I'd use "rcp" in preference to "ftp". I tend to use "ftp" for awkward targets like Microsoft platforms and IBM Mainframes.

Btw, I still don't understand which computer is intended to run this line (local or remote?):
Quote:
exec mv /home/$hostname-config /tmp/cfgsaver/$hostname-config
# 7  
Old 10-18-2010
Quote:
Originally Posted by methyl
It is good practice to ftp a file under a temporary name so it is quite clear to the remote computer when the ftp has finished.
Great tip for any file transfer -- the name is the status. No need for additional flag files. Smilie

---------- Post updated at 10:18 AM ---------- Previous update was at 10:10 AM ----------

While .netrc/ftp or .rhosts/rcp are easy ways to get passwordless access, ssh2/scp2 with keys is a better direction to go:
  • upgrading security, ensuring privacy and passing future audits,
  • simplifying the commands compared to ftp
  • having the option to compress, to speed transfer and spare network bandwidth.
It takes a bit of effort to get your ssh* subtree set up with the right keys, files, entries, permissions, but then you can copy that subtree to other hosts for instant setup.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Expect script returning string following a found expect.

I'm fairly new to scripting so this might not be possible. I am using Expect with Cisco switches and need to capture the string after finding the expect request. For example, when I issue "show version" on a Nexus switch, I'm looking to capture the current firmware version: #show version ... (0 Replies)
Discussion started by: IBGaryA
0 Replies

2. Shell Programming and Scripting

SFTP or scp with password in a batch script without using SSH keys and expect script

Dear All, I have a requirement where I have to SFTP or SCP a file in a batch script. Unfortunately, the destination server setup is such that it doesn't allow for shell command line login. So, I am not able to set up SSH keys. My source server is having issues with Expect. So, unable to use... (5 Replies)
Discussion started by: ss112233
5 Replies

3. Programming

Calling another expect script inside an expect script

I have an expect script called remote that I want to call from inside my expect script called sudoers.push, here is the code that is causing me issues: set REMOTE "/root/scripts/remote" ... log_user 1 send_user "Executing remote script as $user...\n" send_user "Command to execute is: $REMOTE... (1 Reply)
Discussion started by: brettski
1 Replies

4. UNIX for Advanced & Expert Users

Unable to run the script on remote machine Using Expect script

Not able to execute the file in remote host using except utility I am automating the SFTP keys setp process: So i created the expect script for controlling the output of shell below is my main code: Code: #!/usr/bin/expect set fd set password close $fd set df set app close $df... (1 Reply)
Discussion started by: Manoj Bajpai
1 Replies

5. Programming

Calling expect script inside another expect

Hi, Am very new to expect scripting.. Can You please suggest me how to call an expect script inside another expect script.. I tried with spawn /usr/bin/ksh send "expect main.exp\r" expect $root_prompt and spawn /usr/bin/ksh send "main.exp\r" expect $root_prompt Both... (1 Reply)
Discussion started by: Priya Amaresh
1 Replies

6. Shell Programming and Scripting

Expect script to execute a script on a remote host

Hi, I am new to the expect scripting. I have this expect script as below : spawn ssh remote_server -l id set pass "12345" set opt "s" expect "Password:" {send "$pass\r" ; } expect "*ENTER*" {send "Enter\r"; exp_continue } expect "Please select option :" {send... (2 Replies)
Discussion started by: curt137
2 Replies

7. Shell Programming and Scripting

Expect script help needed- script failing if router unavailable

Hey all. Sometimes I'm tasked to change some router configs for the entire network (over 3,000 Cisco routers). Most of the time its a global config parameter so its done with a loop and an IP list as its the same configuration change for all routers. This is working OK. However, sometimes an... (3 Replies)
Discussion started by: mrkz1974
3 Replies

8. Programming

Expect script to run a Shell script on remote server

Hi All, I am using a expect script to run a shell script on remote server, the code is as follows. But the problem is that it executes only first command, and hangs it doesn't run the next commands. spawn ssh $uid@$host expect "password:" send "$password\r" expect "*\r" send... (2 Replies)
Discussion started by: yashwanthsn
2 Replies

9. Shell Programming and Scripting

Need help with Expect script for Cisco IPS Sensors, Expect sleep and quoting

This Expect script provides expect with a list of IP addresses to Cisco IPS sensors and commands to configure Cisco IPS sensors. The user, password, IP addresses, prompt regex, etc. have been anonymized. In general this script will log into the sensors and send commands successfully but there are... (1 Reply)
Discussion started by: genewolfe
1 Replies

10. Shell Programming and Scripting

strange expect script behavior, or am i misunderstanding expect scripting?

Hello to all...this is my first post (so please go easy). :) I feel pretty solid at expect scripting, but I'm running into an issue that I'm not able to wrap my head around. I wrote a script that is a little advanced for logging into a remote Linux machine and changing text in a file using sed.... (2 Replies)
Discussion started by: v1k0d3n
2 Replies
Login or Register to Ask a Question