Sample Header Ad - 728x90

Unix & Linux Stack Exchange

Q&A for users of Linux, FreeBSD and other Unix-like operating systems

Latest Questions

1 votes
2 answers
3736 views
expect script for getting output for multiple servers
I am trying to get output of IQN from multiple servers. I am getting the below error. **Error output** spawn /usr/bin/ssh VM2 | /usr/bin/cat /etc/iscsi/initiatorname.iscsi root@VM2's password: bash: -c: line 0: syntax error near unexpected token `|' bash: -c: line 0: `| /usr/bin/cat /etc/iscsi/initi...
I am trying to get output of IQN from multiple servers. I am getting the below error. **Error output** spawn /usr/bin/ssh VM2 | /usr/bin/cat /etc/iscsi/initiatorname.iscsi root@VM2's password: bash: -c: line 0: syntax error near unexpected token `|' bash: -c: line 0: `| /usr/bin/cat /etc/iscsi/initiatorname.iscsi' **script** #!/bin/bash HOSTS="VM2 VM3 VM4 VM1" read -p "Password: " PASSWORD for HOST in $HOSTS do expect -c " spawn cat /etc/iscsi/initiatorname.iscsi expect { "*password:*" { send $PASSWORD\r;interact } } exit " done Am I doing something wrong?
user155159
Nov 8, 2016, 10:08 AM • Last activity: Jul 15, 2025, 11:06 PM
5 votes
2 answers
7240 views
Log the output of Expect command
I have made the below expect script and I need to log the output of that script. SOURCE_FILE=`ls -l *.txt --time-style=+%D | grep ${DT} | grep -v '^d' | awk '{print $NF}' ` if [ -n "${SOURCE_FILE}" ] then cp -p ${SOURCE_FILE} ${T_FILES} /usr/bin/expect " send "put /opt/AppServer/ES_TEST/todays_repor...
I have made the below expect script and I need to log the output of that script. SOURCE_FILE=ls -l *.txt --time-style=+%D | grep ${DT} | grep -v '^d' | awk '{print $NF}' if [ -n "${SOURCE_FILE}" ] then cp -p ${SOURCE_FILE} ${T_FILES} /usr/bin/expect" send "put /opt/AppServer/ES_TEST/todays_report/*.txt\r" expect "sftp>" send "bye\r" expect EOD EOD else echo "No Files to copy" >> ${LOGFILE} fi I need to log the output of expect command in ${LOGFILE}. How can It be done? I have tried adding the below things, it doesn't work. What could be done? /usr/bin/expect> ${LOGFILE} 2>&1 set timeout 60 spawn sftp $ES_SFTP_USER@$ES_SFTP_HOST_NAME:$R_LOCATION expect "*?assword:" send "$password\r" expect "sftp>" send "put /opt/AppServer/ES_TEST/todays_report/*.txt\r" expect "sftp>" send "bye\r" expect EOD EOD
sabarish jackson (628 rep)
Jul 11, 2016, 05:59 AM • Last activity: Jul 12, 2025, 05:07 AM
0 votes
1 answers
1968 views
'expect' not matching server response string
I'm using a bash script to log into a telnet server and execute a number of commands. It looks like: **login_and_run.sh** #!/bin/bash unset TELNET_USER_NAME_STRING unset TELNET_PASSWORD_STRING unset TELNET_USER_NAME unset TELNET_PASSWORD TELNET_USER_NAME_STRING=`cat SAP_output` TELNET_PASSWORD_STRIN...
I'm using a bash script to log into a telnet server and execute a number of commands. It looks like: **login_and_run.sh** #!/bin/bash unset TELNET_USER_NAME_STRING unset TELNET_PASSWORD_STRING unset TELNET_USER_NAME unset TELNET_PASSWORD TELNET_USER_NAME_STRING=cat SAP_output TELNET_PASSWORD_STRING="Password:" TELNET_USER_NAME="UserNam3\r" TELNET_PASSWORD="Passw0rd\r" # Expect script starts here expect <<- DONE spawn telnet localhost 50008 expect '$TELNET_USER_NAME_STRING' send "$TELNET_USER_NAME" sleep 3 expect "$TELNET_PASSWORD_STRING" send "$TELNET_PASSWORD" sleep 3 spawn ls expect eof DONE where **SAP_output**: Trying ::1... Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. *********************************************** ********************************************** ****###*******####*****#######************** **##***##****##**##****##****##************ ***##*******##****##***##****##********** *****##*****########***######*********** ******##****##****##***##************* **##***##**##******##**##************ ****###****##******##**##********** ********************************** ******************************** Telnet Administration SAP Java EE Application Server v7.50 User name: telnet logs in, I get the banner, but it stops there (as if the strings are not matching). Would it be safer to use wildcards instead of the exact response (and match only " User name: ")?
Sebi (1029 rep)
Aug 15, 2019, 02:40 PM • Last activity: Jun 18, 2025, 03:04 PM
1 votes
1 answers
2861 views
Expect script + automated telnet process between three Linux machines
Is it possible to do the following by expect script? I have Linux server that connected to **linux_machine1** and **linux_machine1** connected to **linux_machine2** My Linux server have access only to **linux_machine1** ( linux server can telnet to **linux_machine1** but can’t telnet to **linux_mach...
Is it possible to do the following by expect script? I have Linux server that connected to **linux_machine1** and **linux_machine1** connected to **linux_machine2** My Linux server have access only to **linux_machine1** ( linux server can telnet to **linux_machine1** but can’t telnet to **linux_machine2** ) But from **linux_machine1** I have access VIA telnet to **linux_machine2** So my question is: I need to develop expect script that perform telnet from the Linux server to **linux_machine1** then expect will perform telnet to **linux_machine2** from **linux_machine1** and will copy the **/etc/hosts** file from **linux_machine2** to my Linux server Is this scenario can be implemented by expect script? Short example that descript the process: ( run expect from Linux server ) --> linux_machine1 --> linux_machine2 Linux server <-- /etc/hosts from linux_machine2 <-- * remark - expect script need to be activate from the linux server
user58412
Apr 29, 2014, 01:47 AM • Last activity: Jun 14, 2025, 04:05 AM
0 votes
2 answers
3053 views
pexpect fails to match command if greater than 65 characters
Issue with [pexpect][1], python module. Pexpect fails to do expected behavior if the length of the command is greater than 65. #!/usr/bin/python import pexpect sshCmd='ssh root@15.186.22.105' prompt='[root@user ~]#' ConnectToClient=pexpect.spawn(sshCmd) ConnectToClient.expect('password') ConnectToCl...
Issue with pexpect , python module. Pexpect fails to do expected behavior if the length of the command is greater than 65. #!/usr/bin/python import pexpect sshCmd='ssh root@15.186.22.105' prompt='[root@user ~]#' ConnectToClient=pexpect.spawn(sshCmd) ConnectToClient.expect('password') ConnectToClient.send('abcd') ConnectToClient.expect(prompt) ConnectToClient.send('echo abcdefghijklmnopqrst111111111111111111111111111111111111111111111111111111111 ') ConnectToClient.expect(prompt) ---> PASS command = 'echo abcdefghijklmnopqrst111111111111111111111111111111111111111111111111111111111' ConnectToClient.expect(command, prompt) While expecting ":", the command (.*) prompt ---> *FAILS* if the length of command is greater than 65. expect_str = "%s(.*)%s" % (command, self._default_prompt)
Bangi (449 rep)
Aug 4, 2015, 05:10 PM • Last activity: May 10, 2025, 02:04 AM
0 votes
1 answers
3472 views
How to use EXPECT in a loop for ssh
Our Linux setup doesn't have keygen configured for passwordless authentication. Hence I am supposed to use only `EXPECT` for passwordless authentication in a shell script. /usr/bin/expect > ${LOG_FILE} set timeout 60 spawn sftp ${EWS_USER}@${EWS_HOST}:${TGT_DIR} expect "*?assword:" send "$password\r...
Our Linux setup doesn't have keygen configured for passwordless authentication. Hence I am supposed to use only EXPECT for passwordless authentication in a shell script. /usr/bin/expect> ${LOG_FILE} set timeout 60 spawn sftp ${EWS_USER}@${EWS_HOST}:${TGT_DIR} expect "*?assword:" send "$password\r" expect "sftp>" send "put $local_dir/$line\r" expect "sftp>" send "bye\r" expect EOF EOF filename=$(basename "$line") # echo "File Name: $filename" #Calculate the MD5Sum locally. local_md5sum=$(md5sum "$line") #echo "Local MD5Sum: ${local_md5sum}" #Calculate the MD5sum in remote machine remote_md5sum=$(ssh ${EWS_USER}@${EWS_HOST} "cd '$TGT_DIR' ; find -name '$filename' -exec md5sum {} \;" > ${LOG_NAME} put ${LOCAL_DIR}/${line} ${TGT_DIR}/${line} EOF But any idea how to use EXPECT here in the scenario below to make the connection passwordless? remote_md5sum=$(ssh ${EWS_USER}@${EWS_HOST} "cd '$TGT_DIR' ; find -name '$filename' -exec md5sum {} \;" < /dev/null)
sabarish jackson (628 rep)
Oct 26, 2016, 01:56 PM • Last activity: May 7, 2025, 06:10 PM
0 votes
1 answers
2904 views
Use a script for a telnet session
I have a problem with my script. It seems my password is wrong. #!/usr/bin/expect set ip [lindex $argv 0] spawn telnet $ip set timeout 10 expect "Login:" send "administrator\r" expect "Password:" send "123456" send "\r" It says, `login incorrect`. But it works, when i use `cmd` on Windows to telnet...
I have a problem with my script. It seems my password is wrong. #!/usr/bin/expect set ip [lindex $argv 0] spawn telnet $ip set timeout 10 expect "Login:" send "administrator\r" expect "Password:" send "123456" send "\r" It says, login incorrect. But it works, when i use cmd on Windows to telnet to my phone, it works with "administrator" and 123456 as password. I don't know why in my script, I could not log on successfully. I have to use this script to downgrade/upgrade my phones.
Xiuur (1 rep)
Feb 13, 2015, 11:08 AM • Last activity: May 3, 2025, 05:06 PM
1 votes
1 answers
3273 views
expect: get error status when spawn command fails
The below is the sample of a script which we execute daily for getting info from the server. For the last couple of days, some server data is missing from the output which is captured on the local file `VS-HV-Report_2017.txt`. Is it possible to get error status when the script is executed, but its u...
The below is the sample of a script which we execute daily for getting info from the server. For the last couple of days, some server data is missing from the output which is captured on the local file VS-HV-Report_2017.txt. Is it possible to get error status when the script is executed, but its unable to connect the server? So that we have a black line or and error status instead of output? #!/usr/bin/expect set timeout 5 #find /path/to/files -type f -mtime +10 -delete set date [exec date "+%d-%B-%Y"] spawn sh -c "yes | cp -ifr .ssh/VS-HV-config .ssh/config" spawn sh -c "> VS-HV-Report_2017.txt" #cat ./info.py spawn sh -c "ssh L1n \"./info.py | sed 's/total.*//g'\" >> VS-HV-Report_2017.txt" expect "Enter passphrase for key '/root/.ssh/id_rsa':" send "passwd\r" spawn sh -c "ssh L2n \"./info.py | sed 's/total.*//g'\" >> VS-HV-Report_2017.txt" expect "Enter passphrase for key '/root/.ssh/id_rsa':" send "passwd\r" spawn sh -c "ssh L3n \"./info.py | sed 's/total.*//g'\" >> VS-HV-Report_2017.txt" expect "Enter passphrase for key '/root/.ssh/id_rsa':" send "passwd\r" set timeout 5 #spawn sh -c "./format-VS-HV.sh > format-allinement-output.csv" #spawn sh -c \"./format-VS-HV.sh\" > format-allinement-output.csv exec ./format-VS-HV.sh > /root/format-allinement-output/format-allinement-output-$date.csv
Shanker (117 rep)
Jul 6, 2017, 09:38 AM • Last activity: Apr 25, 2025, 10:02 PM
6 votes
3 answers
24280 views
Get the output from expect script in a variable
I have an expect script which provides the IP address: #!/bin/expect -f set nodename [lindex $argv 0] spawn virsh console $nodename expect "Escape character is" send "\n" expect "localhost login: " { send "root\n" expect "Password: " send "cloud123\n" } expect "~]#" { send "\n" send "ifconfig | grep...
I have an expect script which provides the IP address: #!/bin/expect -f set nodename [lindex $argv 0] spawn virsh console $nodename expect "Escape character is" send "\n" expect "localhost login: " { send "root\n" expect "Password: " send "cloud123\n" } expect "~]#" { send "\n" send "ifconfig | grep 192.168.1. | awk \'{print \$2}\'" send "\n" expect '(\d+\.\d+\.\d+\.\d+)' send "logout" } I want the script to return this IP address. I am calling this expect script from a shell script as below #!/bin/bash ip=$(expect GetIP.exp nodetwo) echo $ip How can I make my expect script return output to the shell script?
karan ratnaparkhi (111 rep)
Oct 28, 2015, 08:47 AM • Last activity: Mar 18, 2025, 03:22 PM
3 votes
2 answers
101 views
How might I make this expect script consistently work as expected?
I use an interactive wallet [`spectre-cli`][1] of [Spectre][3], a coin similar to Kaspa in its design. When I manually want to get the balance of my wallet I need to run `spectre-cli` and then `connect`, `open` from within `spectre-cli`. All this looks like this: ``` spectre-cli Spectre Cli Wallet v...
I use an interactive wallet spectre-cli of Spectre , a coin similar to Kaspa in its design. When I manually want to get the balance of my wallet I need to run spectre-cli and then connect, open from within spectre-cli. All this looks like this:
spectre-cli
Spectre Cli Wallet v0.3.16 (type 'help' for list of commands)
$ connect
Connected to Spectre node version 0.3.16 at ws://127.0.0.1:19110
$ open
Enter wallet password: 

Your wallet hint is: This is a hint.

$ $ 
• ****************
    • [********]: 4.47042578 SPR   1 UTXOs
      spectre:******************************************************

[********] • 4.47042578 SPR $
I wrote an expect script meant to do those steps for me (so that the only thing I would need to type is my password):
#!/usr/bin/expect -f

# Set a timeout (in seconds) for how long to wait for each prompt
set timeout 5

spawn spectre-cli

expect "$ "   
send "connect\r"

expect "$ "  
send "open\r"


interact
I get inconsistent output from the script. About 70% of the time I get:
./get_balance_works.exp 
spawn spectre-cli
Spectre Cli Wallet v0.3.16 (type 'help' for list of commands)
$ connect
connectopen
command not found: connectopen
$
whereas the rest of the time the script works as expected:
./get_balance_works.exp 
spawn spectre-cli
Spectre Cli Wallet v0.3.16 (type 'help' for list of commands)
$ connect
$ open
Connected to Spectre node version 0.3.16 at ws://127.0.0.1:19110
Enter wallet password: 

Your wallet hint is: This is a hint.

$ $ 
• ****************
    • [********]: 4.47042578 SPR   1 UTXOs
      spectre:******************************************************

[********] • 4.47042578 SPR $
Is there a way to make the script work as expected in a consistent manner? P.S. I am on a Ubuntu LTS.
John Smith (827 rep)
Dec 25, 2024, 11:38 AM • Last activity: Dec 28, 2024, 04:38 PM
0 votes
3 answers
96 views
Expect script with Xargs and multiple parameters
I have an expect script that will SSH into a list of routers. The expect script works on a list of routers in a file and is called from a bash script with xargs. Bash : script get-rtr.sh #!/bin/bash /usr/bin/xargs -P 10 -L 1 -r ./get-rtr-expect.sh router_list_temp list=`cat router_list` for i in `ec...
I have an expect script that will SSH into a list of routers. The expect script works on a list of routers in a file and is called from a bash script with xargs. Bash : script get-rtr.sh #!/bin/bash /usr/bin/xargs -P 10 -L 1 -r ./get-rtr-expect.sh router_list_temp list=cat router_list for i in echo $list do echo $i $foo $bar >> router_list_temp done /usr/bin/xargs -P 10 -L 1 -r ./get-rtr-expect.sh router_list_temp I am looping thru the router list, and writing to a new file with the args added. Then feed the new list to the expect script, each line of the line is arg0 arg1 and arg 2. Then wipe it clean at the end. I don't like this at all. I don't want to create a temp file, but it seems to be working. All better ideas are welcome. If router_list_temp could be an array of routers with 2 args, rather than a file, that would be better .... it would all live in RAM. but I don't know how to create and array or pass it to an expect script.
Roger McCarrick (21 rep)
Nov 22, 2024, 06:20 PM • Last activity: Nov 23, 2024, 10:07 AM
0 votes
0 answers
90 views
Expect Script TCL ending early
I have the following expect script which works for the most part. I have it connecting to a Cisco switch (eventually a couple hundred) and configuring syslog servers. It checks to make sure that buffering wasn't already configured and if it isn't, set it. If "logging buffered" is set, the script wor...
I have the following expect script which works for the most part. I have it connecting to a Cisco switch (eventually a couple hundred) and configuring syslog servers. It checks to make sure that buffering wasn't already configured and if it isn't, set it. If "logging buffered" is set, the script works fine. However, if "no logging buffered" is present, the script dies early before saving the config.
#!/usr/bin/expect -f
    set timeout -1
    set ip [lindex $argv 0]
    set host [lindex $argv 1]
    set username "admin"
    set password "password"
    set prompt "#|%|>|\$ $"
    set timestamp [timestamp -format %Y-%m-%d_%H:%M]
    
    log_file ./$host.$timestamp.log
    
    spawn ssh -o StrictHostKeyChecking=no $username@$ip
    
    match_max 100000
    
    expect {
    	"assword:" { send "$password\r" }
    	"continue connecting (yes/no)?" {
    	send "yes\r"
    		expect {
    		"assword:" { send "$password\r" }
    		}
    	}
    }
    expect "#"
    send -- "\r"
    expect "#"
    send -- "conf t\r"
    expect "#"
    send -- "logging on\r"
    expect "#"
    send -- "do show run | inc logging buffered\r"
    expect {
            "no logging buffered" {send "logging buffered 16384\r"; exp_continue}
            "#"  {send "\r"}
           }
    expect "#"
    send -- "logging host 1.2.3.4 transport tcp port 514\r"
    expect "#"
    send -- "logging host 1.2.3.5 transport tcp port 514\r"
    expect "#"
    send -- "logging trap notifications\r"
    expect "#"
    send -- "end\r"
    expect "#"
    send -- "wr mem\r"
    expect "#"
    send -- "exit\r"
With "no logging buffered" present
@ubuntu:~/test# ./config-syslog-password.sh
spawn ssh -o StrictHostKeyChecking=no admin@192.168.1.2
(admin@192.168.1.2) Password:

core-switch#
core-switch#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
core-switch(config)#logging on
core-switch(config)#do show run | inc logging buffered
no logging buffered
core-switch(config)#logging buffered 16384
core-switch(config)#
core-switch(config)#logging host 1.2.3.4 transport tcp port 514
core-switch(config)#logging host 1.2.3.5 transport tcp port 514
core-switch(config)#logging trap notifications
core-switch(config)#end
root@ubuntu:~/test#
With "logging buffered xyz" present
root@ubuntu:~/test# ./config-syslog-password.sh
spawn ssh -o StrictHostKeyChecking=no admin@192.168.1.2
(admin@192.168.1.2) Password:

core-switch#
core-switch#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
core-switch(config)#logging on
core-switch(config)#do show run | inc logging buffered
logging buffered 16384
core-switch(config)#
core-switch(config)#logging host 1.2.3.4 transport tcp port 514
core-switch(config)#logging host 1.2.3.5 transport tcp port 514
core-switch(config)#logging trap notifications
core-switch(config)#end
core-switch#wr mem
Building configuration...
[OK]
core-switch#root@ubuntu:~/test#
Now I know its something to do with the TCL but I can't figure out what. Any help will be appreciated.
Double (53 rep)
Aug 26, 2024, 09:39 PM • Last activity: Aug 27, 2024, 05:38 PM
0 votes
1 answers
99 views
expect - repeatably send "\n\r" before a match expect
I need to **netcat** a U-Boot and perform NAND script and I use below script ``` spawn netcat -lup 6666 expect "Hit any key to stop autoboot" #autoboot counts down from 3 to 0 and somehow one or two send won't stop autoboot for {set count 0} {$count " send "nand info" ``` Just need to repeatably sen...
I need to **netcat** a U-Boot and perform NAND script and I use below script
spawn netcat -lup 6666
expect "Hit any key to stop autoboot" 
#autoboot counts down from 3 to 0 and somehow one or two send won't stop autoboot
for {set count 0} {$count "
send "nand info"
Just need to repeatably send "\n\r" before "U-Boot >" shows up. Is there a better way to do this?
thomasyang (1 rep)
Jun 9, 2024, 02:42 PM • Last activity: Jun 10, 2024, 12:58 PM
0 votes
1 answers
43 views
Looking for assistance in expect
I have a bash script that has expect to login to a remote host and run list of commands. It works as expected. It has a flaw, if the user enters the wrong password, the expect script keeps trying the password for multiple times. Some host on network are now updated to block the newer ssh connection...
I have a bash script that has expect to login to a remote host and run list of commands. It works as expected. It has a flaw, if the user enters the wrong password, the expect script keeps trying the password for multiple times. Some host on network are now updated to block the newer ssh connection if there was multiple ssh failure in the recent for some minutes. I am trying to modify the script so the expect breaks/exits the code if the password is wrong on the first attempt.
echo "Enter the admin password:  "
read password
itmp=$(mktemp)

cat > $itmp << EOF
set timeout 5
spawn ssha -oStrictHostKeyChecking=no -oCheckHostIP=no admin@$hostname
set timeout 5
expect "password"
send "$password\r"
expect "$hostname"
send "command1\r"
expect "$hostname"
send "command2\r"
expect "$hostname"
send "command3\r"
expect "$hostname"
send "exit\r"
expect eof
EOF
chmod 600 $itmp

expect -f $itmp
rm $itmp
Vra (1 rep)
May 15, 2024, 03:05 AM • Last activity: May 15, 2024, 03:26 PM
1 votes
1 answers
33 views
Automate passing JBOSS installer arguments using Expect Command
I have a JBOSS Installer which when run manually it will prompt for input and below is the sample of the execution is as below [sp@sp baseInstaller]$ ./advStart.sh config Buildfile: /home/sp/jboss/sp/baseInstaller/build.xml init: config: [groovy] coa.install.props properties [groovy] [groovy] ? indi...
I have a JBOSS Installer which when run manually it will prompt for input and below is the sample of the execution is as below [sp@sp baseInstaller]$ ./advStart.sh config Buildfile: /home/sp/jboss/sp/baseInstaller/build.xml init: config: [groovy] coa.install.props properties [groovy] [groovy] ? indicates a response is required from you [groovy] [value] is the current value [groovy] to keep current value just press [groovy] or type new value and press [groovy] {value,value..} shows the allowed values [groovy] installer version 6.0.3.4 [groovy] [groovy] Jboss Server Config Options (changeable after initial config) [groovy] ------------------------------------------------------------- [groovy] ? host [sp.resource.com] resource.com [groovy] ip=10.50.55.90 [groovy] host changed to resource.com [groovy] ? bind to host only or all ports (all has security implications) [groovy] [host] [groovy] {host,all,custom} all [groovy] bind to host only or all ports (all has security implications) changed to all [groovy] ? min memory 64 I tried to frame an expect script to automate this process and below is my script [sp@sp baseInstaller]$ cat saba.sh #!/usr/bin/expect # Accessing command line arguments set arg1 [lindex $argv 0] set arg2 [lindex $argv 1] # Printing the arguments puts "Argument 1: $arg1" puts "Argument 2: $arg2" cd /home/sp/jboss/sp/baseInstaller spawn ./advStart.sh config # Expect the prompt for the argument with a timeout of 10 seconds expect { "? host [sp.resource.com]" { # Send the host value send "$arg1\r" exp_continue } "? bind to host only or all ports (all has security implications)" { # Send the argument value send "$arg2\r" } timeout { puts "Error: Timed out while waiting for the prompt." exit 1 } eof } -timeout 10 # Wait for the script to finish wait Now while executing this script, it is getting struck in the below place and not proceeding further. Can you please help me about this? [sp@sp baseInstaller]$ expect saba.sh resource.com all Argument 1: resource.com Argument 2: all spawn ./advStart.sh config Buildfile: /home/sp/jboss/sp/baseInstaller/build.xml init: config: [groovy] coa.install.props properties [groovy] [groovy] ? indicates a response is required from you [groovy] [value] is the current value [groovy] to keep current value just press [groovy] or type new value and press [groovy] {value,value..} shows the allowed values
sabarish jackson (628 rep)
Mar 13, 2024, 10:43 AM • Last activity: Mar 13, 2024, 03:34 PM
2 votes
2 answers
2302 views
Exiting an Expect Script
I have this `expect` script, that will log into a host, run a command, return it's contents and exit. What sort of cleanup/proper exiting needs to happen when I've finished running my command? This command will always return something, the larger problem or issue I think I should be handling is if e...
I have this expect script, that will log into a host, run a command, return it's contents and exit. What sort of cleanup/proper exiting needs to happen when I've finished running my command? This command will always return something, the larger problem or issue I think I should be handling is if either SSH hangs, or if logging in as the admin user hangs. I thought I might have been handling that via timeout, but I am unsure: #!/usr/bin/expect -f set timeout 10 match_max 100000 set hostname [lindex $argv 0] spawn /usr/bin/ssh -o "StrictHostKeyChecking no" admin@$hostname expect "*password:" send -- "redactedSSHpassword\r" expect "Username:" send -- "admin\r" expect "*password:" send -- "redacted\r" expect -- "*#*" send -- "show stat summary\r" expect -- "*#*" I also don't understand the proper way of exiting this script and making sure I'm not leaving a stale session around.
Kahn (1827 rep)
Dec 10, 2020, 07:44 PM • Last activity: Feb 22, 2024, 08:56 AM
4 votes
3 answers
9873 views
how to send special characters in expect script?
I'm trying to send special characters (password) which contains `#?!k;|C `. The password is dynamic and cannot be hardcoded in the script, so I'm passing it as variable as shown below $ expect ./passwordlessSSH.exp $hostname "$password" the code of this script is as below #!/usr/bin/expect -f set ho...
I'm trying to send special characters (password) which contains #?!k;|C . The password is dynamic and cannot be hardcoded in the script, so I'm passing it as variable as shown below $ expect ./passwordlessSSH.exp $hostname "$password" the code of this script is as below #!/usr/bin/expect -f set host [lindex $argv 0] set pass [lindex $argv 1] spawn ssh-copy-id -i /home/hdpsvcs/.ssh/id_rsa.pub hdpsvcs@$host expect { "yes" { send "yes\r" } "assword" { send "$pass \r" } exit 0 But still it is not running the script properly. I don't see any error but the variable is not passed correctly. The output is as shown below: $ expect ./passwordlessSSH.exp $hostname "$password" spawn ssh-copy-id -i /home/hdpsvcs/.ssh/id_rsa.pub test@otesthost /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/test/.ssh/id_rsa.pub" /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys \S Kernel \r on an \m test@test's password: $
user2925298 (41 rep)
Jul 17, 2019, 12:21 PM • Last activity: Feb 6, 2024, 01:49 AM
35 votes
12 answers
331268 views
Shell Script for logging into a ssh server
I tried writing a shell script which can do automatic login into a ssh server using password which is mentioned in the script. I have written the following code: set timeout 30 /usr/bin/ssh -p 8484 root@172.31.72.103 expect { "root@172.31.72.103's password" { send "password\r" } } This code is not r...
I tried writing a shell script which can do automatic login into a ssh server using password which is mentioned in the script. I have written the following code: set timeout 30 /usr/bin/ssh -p 8484 root@172.31.72.103 expect { "root@172.31.72.103's password" { send "password\r" } } This code is not running properly, still it is asking for the password. Can somebody please help me in solving this
pradeepchhetri (10147 rep)
Feb 7, 2012, 06:17 AM • Last activity: Jan 12, 2024, 05:12 PM
0 votes
1 answers
422 views
expect script sending password before prompt
I have written a small `expect` script for automatic ssh to remote server. But sometimes my `expect` script don't work. Actually it sends password before password prompt. Here is my script ```lang-expect #!/usr/bin/expect -f set password xyzzys # now connect using ssh set host [lindex $argv 0] spawn...
I have written a small expect script for automatic ssh to remote server. But sometimes my expect script don't work. Actually it sends password before password prompt. Here is my script
-expect
#!/usr/bin/expect -f
set password xyzzys
# now connect using ssh
set host [lindex $argv 0]
spawn ssh vivek.gagrani@$host
expect "*?assword:*"
send -- "$password\r"
send -- "\r"
interact
When running this script I see below output sometimes:
spawn ssh vivek.gagrani@bvdev429
xyzzys         ----> password sent before asking

vivek.gagrani@bvdev429's password:
How to fix this ?
sachu (3 rep)
Jan 11, 2024, 01:15 PM • Last activity: Jan 11, 2024, 02:43 PM
0 votes
1 answers
537 views
Correct way to set PS1 prompt over ssh
I am trying to set the PS1 prompt dynamically on the my remote machine. The idea is that when I will do ssh to the remote machine, I will also send the value which will set as a prompt for that remote machine. I already have an expect script which do automatic ssh login to the remote machine. Now I...
I am trying to set the PS1 prompt dynamically on the my remote machine. The idea is that when I will do ssh to the remote machine, I will also send the value which will set as a prompt for that remote machine. I already have an expect script which do automatic ssh login to the remote machine. Now I am trying to modifying the script in such a way that it will do automatic ssh along with setting the PS1 prompt with the value passed over ssh. Below is my script #!/usr/bin/expect -f set password Mayy@029Mayy@029 # now connect using ssh spawn ssh -t vivek.gagrani@10.128.31.29 "'export PS1='bvdev@429[\w]#'; exec bash -l" expect "*?assword:*" send -- "$password\r" send -- "\r" interact On executing the above script I am getting error. Inavalid command name w while executing \w. I am not sure if I am approaching it in correct way? Is my script correct for this task. Please help me
sachu (3 rep)
Dec 10, 2023, 06:32 PM • Last activity: Dec 13, 2023, 04:10 PM
Showing page 1 of 20 total questions