Expect Script to Automate SSH


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Expect Script to Automate SSH
# 8  
Old 08-06-2010
Quote:
Originally Posted by Prodiga1
I need the script to ssh into a switch and change some values, so I'm thinking expect is my best option to accomplish that.
yes it is;

Here is a script I use to logon into dell racs to chekc configurations... Id rather just post an example for you because I spent a good 7-8 hours trying to figure out how to do this type of stuff with expect.. so hopefully this will save you some time.

Code:
#!/usr/bin/expect -f

set ipaddress [lindex $argv 0]
set passwd1 [lindex $argv 1]
set passwd2 [lindex $argv 2]

set timeout 10
spawn ssh -o StrictHostKeyChecking=no root@$ipaddress {my commands here}
expect {
  "Warning: *" {
    exp_continue
  }
  "root@${ipaddress}*" {
    send "${passwd1}\r"
    exp_continue
  }
  "Permission denied*" {
    expect "root@${ipaddress}*"
    send "${passwd2}\r"
    exp_continue
  }
  "return comment of my command" {
    return 0
  }
  timeout {
    return 1
  }
  "*" {
    return 2
  }
}
expect eof

as you can see I run the ssh command... it will cycle through 2 possible passwords and then look for return phrase my command gives... I dont think the timeout portion works.. but I wrap the script in a bash script and verify I can connect to the ip before running this script.

simply put there is not much out on google for advanced expect scripts but hopefully this will get you going. There is also an expect automate tool to help you get the intial formatting down.. forget how to run it though.
# 9  
Old 08-07-2010
Quote:
Originally Posted by Prodiga1
I need the script to ssh into a switch and change some values, so I'm thinking expect is my best option to accomplish that.
This reminds me a lot of the late 80's and early 90's when we used ppp to dial a modem to connect to our ISP or a bulletin board and a chat script to wait for the username and password prompts and send them so we didn't have to do it manually.

Fast forward a couple decades. Now we have always-connected high-speed internet (do you?) and use SSH and keychain.

It sounds like you can already make an SSH connection, so I assume SSH is setup and keys generated.

Now copy the ~/.ssh/id_dsa.pub from the local machine to the ~/.ssh/authorized_keys on the remote machine. The .pub file is one very long line of text. If the authorized_keys file already exists, append the id_dsa.pub file to the end of it, or create a new file if it does not exist. Set the mode bits of authorized_keys to 0600 with chmod (important).

Now you should be able to do password-less logins to the remote computer, although you will be prompted to enter the passphrase to unlock your local ssh key (assuming you entered a passphrase when your created it). If you can't, you may need to change the local ssh configuration.

To avoid entering your passphrase each time, install keychain. It will remember your passphrase (in memory, not on disk) and make the connection automatically.

I routinely do things like this:
Code:
ssh mywebserver.com 'ls -tor public_html/img/ | grep png'
scp somefile.txt otherpc:text/
ssh myserver.com cat public_html/intro/index.html | grep something

Note that the first grep command ran on the remote server, while the third command copied the whole file to my PC and my local copy of grep parsed it.

The possibilities are endless. And no expect or chat scripts are required.
# 10  
Old 08-07-2010
Quote:
Originally Posted by KenJackson
It sounds like you can already make an SSH connection, so I assume SSH is setup and keys generated.
You're assuming the remote host is a computer that has such things as accounts or, for that matter, files. I suggest reading page 1 of the topic...
# 11  
Old 08-07-2010
Quote:
Originally Posted by Corona688
You're assuming the remote host is a computer... I suggest reading page 1 of the topic...
I don't understand the attitude. Or why your are suggesting anything to me.

In post 4 of this thread the OP expressed concern about how to use expect but not about ssh. And the target was identified as a linux box.

I did however make assumptions about what the OP wants. Questions posted on forums like this rarely have enough information. If we want to be helpful, we have to assume a likely scenario and answer it. If we guess wrong, it tends to draw out more detail.
# 12  
Old 08-10-2010
I appreciate all the comments, especially that of trey85-- those kinds of comments are the most useful.

Specifically, what I'm trying to figure out is how to change the passwords of an assortment of switches on my private network... so using ssh keys seems impossible (unless I'm mistaken).

My plan was to use an expect script that automated ssh as a template to do so.

One problem I'm running into is sending a command to the machine I ssh into-- I do something like:

Code:
spawn ssh -o StrictHostKeyChecking=no <some address>
expect "Password:"
send <some password>
expect "%" # I think this is the proper notation for expecting a blank prompt
send ls

using ls as an example command. And for whatever reason, this script does not operate as intended.

Specifically, using the verbose output stream of expect-- I am given a:
Code:
send: sending "ls" to { exp6 }

and then the script times out. Any ideas?

---------- Post updated at 09:57 AM ---------- Previous update was at 09:56 AM ----------

By the way, I am aware that you can send commands through ssh.

But in the case of using switch commands after a successful ssh-- that implementation would not be useful.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Automate OTPW login to ssh via bash script

Hello everyone. I'm a Linux novice trying out a lot of bash scripting lately, as it is so very addictive. Lately I have been setting up one of my boxes remotely and have been hardening it as much as possible. Please allow me to explain the scenario, as it does tend to become a little... (1 Reply)
Discussion started by: instro
1 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. Shell Programming and Scripting

Unable to automate SSH in Script

Hi I have a script at Server B. I want to run it from server A via another script. I tried the following command. ssh mss@247.123.456.123 "sh pm10.sh" It's getting login automatically, but while running the script through error like "reppar: command not found" where reppar is an application... (4 Replies)
Discussion started by: rajeshmepco
4 Replies

4. Shell Programming and Scripting

How to automate SSH remote connection with a shell script

Hi Guys! I am trying to write a shell script for automated ssh. vairable user and passwd have initialized correctly, but when I use the following it still prompting me for the password. #!/usr/bin/bash user='root@10.14.76.225' passwd='admin' ssh $user $passwd uptime exit I... (3 Replies)
Discussion started by: pinpe
3 Replies

5. Shell Programming and Scripting

Script using SSH with expect command

Hi all, I want to connect to some host with "ssh". I have googled and got some commands of "expect" and "spawn". I was not aware of these commands and tried below script. $ cat auto.sh set host xx.xx.xx.xx set password abcd@1234 set user root spawn ssh $user@$host expect "*?assword:*"... (4 Replies)
Discussion started by: divya bandipotu
4 Replies

6. Shell Programming and Scripting

Using expect to automate sftp

I am trying to use a for loop in my expect cmdFile that I am calling. I want to be able to call either one file name or a series of file names in the working directory (that I won't know the names before hand) and then pass the names to the sftp program. Something like for i in (ls *txt) do (0 Replies)
Discussion started by: vedder191
0 Replies

7. Shell Programming and Scripting

Expect script to automate telnet session

Hi all, I am currently running a daemon which creates a virtual terminal for testing purposes. Essentially, if I were to interact with it manually, this is what I get. john@test1:~$telnet localhost 7777 Trying ::1... Connected to localhost. Escape character is '^]' mip6d> pl eth2... (6 Replies)
Discussion started by: abxccd
6 Replies

8. Shell Programming and Scripting

How to automate sftp without using expect script?

How to automate sftp with out using expect script? My batch file has the password but it is not taking. Please see below. I want to use this sftp connection in a loop for pushing new files in a directory one at a time. Hence I can not use an expect script. bash-2.05$... (5 Replies)
Discussion started by: Tuxidow
5 Replies

9. Shell Programming and Scripting

SSH Expect Script

Ok, i don't know if anyone else here have had to deal with something like this before, but here's my situation. I have about 1000+ servers I need to log into to do something. What i need to do is to log into each server, go to a certain directory on each of the servers, copy the files that... (3 Replies)
Discussion started by: SkySmart
3 Replies

10. Shell Programming and Scripting

Expect script to ssh into MMI

Guys, I know this is tricky.. I'm trying to write a script to pull info from a MMI device. Following script logins into the server and then changes to super user. Opens MMI session and then exits out. I need to run the command under the MMI session "dsp_alarm_span all" Is there any other way to... (2 Replies)
Discussion started by: miltonrods
2 Replies
Login or Register to Ask a Question