Sponsored Content
Top Forums Shell Programming and Scripting How to use 'expect' to pass UID & Password to a "for loop" in shell script? Post 302873415 by thisissouvik on Tuesday 12th of November 2013 05:14:09 AM
Old 11-12-2013
Hello in2nix,

Your basic expect based script did the ground work for me - could login to any server passing the hostname to parameter "h", so this is how my little modified script looks now:

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

# set credentials
set u "msouvik"
set p "pass^123"

# get host name from command-line
# set h "my_desired_server"

# set the command to run
set cmd "echo Hello Souvik!  >> /export/home/msouvik/echo.out"

# spawn a connection to the host and run the given command
spawn -noecho ssh -t $u@$h $cmd

# handle the password prompt
expect "?assword:*"
send -- "$p\r"
send -- "\r"

# done
expect eof

I could not manage to pass multiple server names to this expect script, the methods I tried --

1. Called this expect script from an external "for" & "while" loop -- got an obvious error of parameter "h" not found:
Code:
for h in `cat my_server_list`
do
<Path of above written expect script>
done

2. Added a "for" loop within the above expect script:
Code:
#!/usr/bin/expect -f
#
#
for h in `cat my_server_list`
do

# set credentials
set u "msouvik"
set p "pass^123"

# get host name from command-line
# set h ""

# set the command to run
set cmd "echo Hello Souvik!  >> /export/home/msouvik/echo.out"

# spawn a connection to the host and run the given command
spawn -noecho ssh -t $u@$h $cmd

# handle the password prompt
expect "?assword:*"
send -- "$p\r"
send -- "\r"

# done
expect eof

done

That failed since it couldn't understand expect syntaxe(s) within the for loop.

Please further suggest!


-- Souvik
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

shell script & sqlplus "plzz help"

hi friends, i m tryin to load data from one set of table to other i have sql procedure al ready for it..! i m going to load the procedure only if data in one of my table for example table "landing " have 10 records each attribute of this table is file_name status date ... (2 Replies)
Discussion started by: kulbir
2 Replies

2. Shell Programming and Scripting

How to include RETURN KEY with Background process "&" in Shell Script

Hello All, I am a newbie in Shell script programming, and maybe you can help me with my query. I need to write a shell script (mntServer.ksh) that will start a background process and also to be able to run another script. The mntServer.ksh script contains: #!/bin/ksh... (1 Reply)
Discussion started by: racbern
1 Replies

3. Shell Programming and Scripting

ssh through "expect" in shell script

Hi, I am trying to use "Expect" in shell script to ssh and do some work in remote server but I am unable to connect. Here is the code I am using. #save as test.sh set ip "10.10.10.10" set username "uname" set password "upass" spawn ssh $username@$ip expect "Password:" send... (8 Replies)
Discussion started by: shekhar2010us
8 Replies

4. Red Hat

files having Script which works behind "who" & "w" commands

Dear All, plz print the path of files which have the script of "who" & "w" commands. thnx in advance. (6 Replies)
Discussion started by: saqlain.bashir
6 Replies

5. Shell Programming and Scripting

Passing username and password to a script running inside "expect" script

Hi I'm trying to run a script " abc.sh" which triggers "use.sh" . abc.sh is nothing but a "expect" script which provides username and password automatically to the use.sh script. Please find below the scripts: #abc.sh #!/usr/bin/expect -f exec /root/use.sh expect "*name*" send... (1 Reply)
Discussion started by: baddykam
1 Replies

6. Shell Programming and Scripting

Shell script using expect to login to couple of remote servers and read "crontab -l"

I need a shell script using expect to login to couple of remote servers and read "crontab -l -u <username>" & "cat /etc/rc.local" & "df -h" and able to create output into a file saved locally with hostname.crontab & hostname.rc.local & disk.status. I can supply a file as list of hostname or IP... (4 Replies)
Discussion started by: jaipsharma
4 Replies

7. AIX

How to use 'expect' to pass UID & Password to a "for loop" in shell script?

Friends, Need someone's help in helping me with the below requirement for a script: > For a list of servers(over 100+), I need to login into each of them(cannot configure password-less ssh) & grab few configuration details < I know, this is possible through expect programming in a simple... (2 Replies)
Discussion started by: thisissouvik
2 Replies

8. Shell Programming and Scripting

How to pass password to "su account" in script?

Hi, I need to run a test script to check all test accounts, is it possible to pass the password to su in following command? I've got following error: $ echo "${password}" | su ${test_account} -c "check_account.sh" standard in must be a tty Thank you. - j (3 Replies)
Discussion started by: hce
3 Replies

9. Shell Programming and Scripting

Can someone please show me a very simple "expect" script to change password in Solaris please?

Ladies & Gents, Can one of you gurus please show me a very simple "expect" script to change the password in Solaris in a script, please? Nothing fancy, no error checking, no nothing. Just to change the password of a new user, it's all. Many thanks in advance. U guys have honestly earned my... (1 Reply)
Discussion started by: Hiroshi
1 Replies

10. Shell Programming and Scripting

Expect: spawn id exp5 not open while executing "expect "$" { send "sudo su -\r" }"

Hi All, i am trying to ssh to a remote machine and execute certain command to remote machine through script. i am able to ssh but after its getting hung at the promt and after pressing ctrl +d i am gettin the out put as expect: spawn id exp5 not open while executing "expect "$" {... (3 Replies)
Discussion started by: Siddharth shivh
3 Replies
startslip(8)						      System Manager's Manual						      startslip(8)

NAME
startslip - Configures SLIP connections SYNOPSIS
/usr/sbin/startslip [-i filename] OPTIONS
Specifies the file containing the startslip subcommands for dial-out connections. DESCRIPTION
The startslip program configures two types of SLIP connections: Dial-in connections -- The system is to answer calls from remote systems. Dial-out connections -- The system is to place calls to a remote system. Dial-in Connections If your system is configured as a dial-in system, a getty process is continuously run on the serial line that is connected to a modem. When a call comes in, getty prompts for a login name. If the login name matches a SLIP user name entry in the /etc/passwd file, getty starts startslip as a login shell. The startslip program then reads the /etc/slhosts files until it finds an entry matching the current login name. If an entry is found, startslip configures the tty line as a SLIP link using information in the slhosts file entry. See Network Administration for information on setting up your system as a SLIP dial-in system. Dial-out Connections If your system is configured as a dial-out system and you want to create a SLIP connection to a remote system, you invoke the startslip program from the command line and specify -i filename. The filename file contains the startslip subcommands to set up the dial-out connec- tion (see the Subcommands section). After making the connection, startslip runs in the background. When you are finished with the SLIP connection, you can either turn off your modem or kill the tty process. The tty process ID is stored in the /var/run/ttyxx.tel-pid file, where ttyxx specifies the terminal line. Subcommands The following startslip subcommands can be used to set up SLIP dial-out connections. The subcommands are specified in a SLIP setup script file that is passed to startslip with the -i filename option. In the setup script file, any text following the comment character (#) is ignored. In addition, each line can contain up to 256 characters. Specifies the callback option. If this subcommand is specified, start- slip raises Data Terminal Ready (DTR) if carrier is lost during a read from the serial line. Causes startslip to configure the network interface with the values specified with the myip, dstip, and netmask subcommands, and to attach the serial line to the network interface. If the gateway is specified, it also adds the destination IP address as the default gateway. Specify this subcommand at the end of the script file. Specify the myip, dstip, netmask, and, optionally, gateway subcommands prior to connslip. Enables debugging and prints debug messages to the logfile file. Specify this subcommand at the beginning of the script file. Dials the telephone_number. The format of telephone_number is a string of numbers with no separators. Specify this subcommand after opentty and modemtype. Sets destination IP address. You can specify either a host name or IP address. Terminates startslip and exits. Causes startslip to wait to receive string from the remote host. If a match occurs, startslip continues read- ing this file. If a match does not occur after the specified seconds, startslip prints message and exits. Specify this subcommand after opentty. Flushes the read and write queues. Sets the destination IP address as the default gateway. Specifies a hard-wired link. Ignore modem control signals (CLOCAL flag set in termios structure) when switching to a SLIP connec- tion. Enables ICMP traffic suppression. Sets the modem type. The modem_name specified must be defined in the /etc/acucap file. Sets my local IP address. You can specify either a host name or IP address. Sets the network mask. Opens the ttyxx serial line and sets line speed to baud_rate. The value for baud_rate must be one of the standard speeds listed in </usr/include/sys/termios.h>. Prints string to the standard output; everything to the end of the line, including punctuation, is printed. Use this subcommand to cue the user as to which subcommand is executing. Causes startslip to read either the local IP address, destination IP address, or network mask from the remote system in dotted decimal notation. Use this subcommand after opentty. An expect subcommand should be used prior to raddr ensure that the desired information is read. Sends the string to the serial line. Use this subcommand after opentty. If string is set to the keyword ASKPASSWD, startslip prompts for a password, reads it from standard input, and sends it to the serial line. Causes startslip to sleep number_of_seconds before continuing to read subcommands. Displays current status of start- slip. Enables TCP header compression. Enables automatic TCP header compression. RESTRICTIONS
Only a person with superuser authority can run the startslip program. EXAMPLES
The following sample setup script file shows how to use various startslip dial-out subcommands. debug /tmp/debuglog myip 27.140.208.38 dstip 27.140.208.40 netmask 255.255.255.0 # set modemtype before dialing # microcom modem attributes defined in /etc/acucap modemtype microcom printmsg open /dev/tty00 at speed 38400 opentty tty00 speed 38400 printmsg dial 18154231234 dial 18154231234 # expect login prompt. timeout and exit after 5 seconds. expect ogin: timeout 5 errmsg "not getting login: prompt" printmsg send login send sliplogin # expect password prompt. timeout and exit after 5 seconds. expect assword: timeout 5 errmsg "not getting Password: prompt" printmsg Prompt and send password send ASKPASSWD # expect Starting SLIP after login expect SLIP timeout 10 errmsg "not getting startup message" # connect slip - ifconfig, slattach printmsg connect slip connslip FILES
Specifies the command path Contains the startslip process ID Specifies the termios header file SEE ALSO
Commands: ifconfig(8) Files: acucap(4), slhosts(4) Network Administration Specifications: A Nonstandard for Transmission of IP Datagrams Over Serial Lines: SLIP (RFC1055) Compressing TCP/IP Headers for Low-speed Serial Links (RFC1144) startslip(8)
All times are GMT -4. The time now is 06:20 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy