Regular Expression In Expect Script To Prevent Printing To Screen

Hello, I have an expect script where I ssh to a remote host to determine the network configuration and get from the user the network interface card that should be used. From their response, I determine the subnet mask and save the information to a text file that is later transmitted back to my local host. This is all so that I can set up virtual IP aliasing and verify that the physical IP address of the local and remote host are on the same subnet prior to continuing with the setup. I am running the script on Linux, with expect version 5.45.

The code itelf works just fine, but I'm having some issues with how it displays on the screen. As you'll see below in the example, the default system prompt displays, as does the user input command that I'm sending to the shell from the expect script.

Is there a regular expression or something that I can write to prevent the prompt and command that I'm sending from printing to the screen? I know that it should be suppressed if I have an expect command following the Code:
send -s "\nread n_card?'Enter the network interface card number for this server (i.e. eth0):   '\r

command, but everything I have tried for strings and regular expressions to expect causes the netstat -rn output to not show up all of a sudden. I'm new to expect, so I'm not really sure why this is happening.

I would really appreciate any help/suggestions. Thanks for your time!

Part of the Script Code:
Code:
expect {
   -re $prompt {   ;# Send individual commands and get user input
        set timeout -1
        
        # Get partner hostname and put in vipsetup.txt file
        send -s "hostname > vipsetup.txt\r"  
        expect -re $prompt
        
        # Display the network routing info for the user and prompt for 
        # network interface card number
        send -s "print \"The network routing table for the $ptner server is displayed below:\n\" ; netstat -rn \r"
        
        expect -re "\r(.*):\r"
        send -s "\nread n_card?'Enter the network interface card number for this server (i.e. eth0):   '\r"
        interact "\r" return    ;# Wait for user input from read command
        send -- "\r"
        send -s "echo \$n_card >> vipsetup.txt\r"  
        
        # Obtain subnet mask information for partner based on network 
        # interface card number being used
        send -s "msk=\$(cat /etc/sysconfig/network-scripts/ifcfg-\$n_card | grep NETMASK)\r"
        send -s "msk=\$(echo \${msk#NETMASK=})\r"
        send -s "echo \$msk >> vipsetup.txt\r"
    }
    timeout {
        send_user "Connection to host $hostip timed out."
        exit 6 
    }
    eof {
        send_user "Connection to host $hostip failed."
        exit
    }
}


Script Output:
Code:
The network routing table for the PRIMARY server is displayed below:
 
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
10.105.65.0     0.0.0.0         255.255.255.0   U         0 0          0 eth0
0.0.0.0         10.105.65.1     0.0.0.0         UG        0 0          0 eth0
 [root@remotehost root]$
[root@remotehost root]$ ber for this server (i.e. eth0):   '              < 
Enter the network interface card number for this server (i.e. eth0):   eth0



Similar Content



How To Return From Shell 'read' Command Passed In Expect Script?

I have a shell script that calls an expect script I wrote to ssh login to another host and get user input regarding that host's network configuration. I pass four arguments to the expect script: the remote host ip address, the username, the password, and the list of commands to run. My expect script is below:

#!/usr/bin/expect
# Usage: expectssh <host> <ssh user> <ssh password> <script>

set timeout 60
set prompt "(%|#|\\$) $"
set commands [lindex $argv 3];

spawn ssh [lindex $argv 1]@[lindex $argv 0]

expect {
"*assword:" {
send -- "[lindex $argv 2]\r"
expect -re "$prompt"
send -- "$commands\r"
}

"you sure you want to continue connecting" {
send -- "yes\r"
expect "*assword:"
send -- "[lindex $argv 2]\r"
expect -re "$prompt"
send -- "$commands\r"
}

timeout {
exit }

expect -re $prompt
send -- "exit\r"
}

The script runs well, except that if I send a command such as 'read' that requires user input, the script does not continue or exit after the user presses enter. It just hangs.

The commands I pass to the expect script and it's call are as follows:
SCRIPT='hostname > response.txt;netstat -rn;read net_card?"What is the network interface card number? " >> response.txt; read net_mask?"What is the subnet mask? " >> response.txt'

/usr/bin/expect ./expectssh.exp $hostip $usr $pswd "$SCRIPT"

Any suggestions on how I can pass a command to my expect script that requires user input without it hanging?

On a side note because I know it will come up - I am not allowed to do key-based automatic SSH login. I have to prompt for a username and password, which is done from my main shell script.

Thanks for any suggestions and help you can provide!

How To Copy File From Remote Host To Local Host Then Delete From Remote Host

I have an expect script to SSH to a remote host and obtain some user inputs and information about the server/network configuration. The responses are saved in a text file that I then need to copy to my local host so that I can read the lines into variables for use in the parent shell script.

Is there a way to do this without needing to enter the username and password for the local host to use function scp? I have tried the following in my expect script to no avail:
Code:
spawn scp $usr@$host:$flnm .
expect {
	-re "(.*)assword:" { 
		send -s "$pswd\r"
	}
}

I have also tried to directly scp the file and enter the username and password to try to debug the issue, and that doesn't work either:
Code:
spawn scp file.txt user@host:file.txt
expect {
	-re "(.*)assword:" {
		send -s "password\r"
	}
	"you sure you want to continue connecting" {
		send -s "yes\r"
		exp_continue
	}
}

In both scenarios I have used exp_internal 1, and there are no errors. But I do not end up with the file on my local host.

Following the copy, I would like to delete the file from the remote host. Any suggestions on how to accomplish this?

Python FTP Send Command

hello Experts!
need some help here with python FTP. so what i'm trying to do is, from a linux machine, ill be sending over a python script to a windows machine via python ftp. that works fine with no problem. now, i want to execute that python script i sent remotely. im having trouble with that. here's my code to just execute the script:

p.s : please dont suggest other options such as python paramiko due to many network security reasons

Code:
ftp = ftplib.FTP("windows machine name")
ftp.login("username", "password")
ftp.cwd("where\\python\\script\\is kept\\")

ftp.sendcmd("python myscript.py")

but looks like it doesn't recognize that command. how do i accomplish this please?

Network Configuration For Redhat 7

I just installed Redhat 7 and try to config the network , add the IP address to network card , but I got the problem.

I modify the file /etc/sysconfig/network-scripts/ifcfg-enp0s3 , the configuration is as below , and then reboot .

DEVICE="enp0s3"
NETBOOT="yes"
HWADDR="08:00:27:15:38:B7"
TYPE="Ethernet"
BOOTPROTO="none"
NAME="enp0s3"
UUID="462f4834-4fe7-43a7-84e7-83b2722e94c1"
ONBOOT="yes"
IPADDR="192.168.1.1"
NETMASK="255.255.255.0"

After reboot , I use "ip addr show" still not shown the ip address , the ip address is still blank , would advise what is wrong ? thanks

Misbehaving 'echo' Command On Cygwin

Hi,

I've used unix before, but have just started to play with CYGWIN (1.1.3) on windows XP (32 bit). I'm having trouble getting 'echo' to behave as I would expect.

I've created a script called runstuff which contains these lines....
Code:
#!/bin/bash
# This line works as expected....
echo "the user is <$USER> and the hostname is <$HOSTNAME>"
MYVAR1=bill
MYVAR2=fred
# This lines doesn't.....
echo "my first variable is<$MYVAR1> and my second is <$MYVAR2>"

The output I get is this...
Quote:
$ runstuff
the user is <Mike> and the hostname is <MikesLaptop>
> and my second is <fredl

In the first echo statement it is just echoing a couple of external variables - this works as expected. In the 2nd echo example the two variables are internal, but the displayed output isn't what I'd expect, my guess is that the first part of the output "my first variable is <bill" has somehow been overwritten by the remainder of the string. Or am I missing something?

I have tried exporting the variables, and I've experimented with wrapping them in quotes, but the results are always the same.

Any help gratefully received.

Detect NIC Interface In Linux

Dear Friends ,

One of My linux server , I have 4 NIC card . I plugged a network cable one of NIC card . Now How can I understand which interface (eth0 or eth1 or eth2 or eth3) is connected with the cable ?

Which command I need to use to find it ?


My second question is :
using 'ethtool ' output I got a line : "Link detected : yes"
what does it means ?

Dhcpd On Opensuse 12.1 Does Not Hand Out Addresses

Hi all

I am trying out this dhcp server setup on opensuse 12.1 but it does not seem to be working.

I have eth0 configured to be 192.168.10.1 in a /24 network.

I want to hand out the rest of the address in the same subnet as dhcp addresses. (So everything from 10.2 to 10.255). The default gw is defined as 192.168.10.1, and routing table looks correct. and I only have eth0.


When I start the dhcp, and connect it to the client machine, I see nothing on the wireshark trace. no DHCP discovery or ack messages. And the client machine just keeps trying boot from the network via the network interface. I know I got the right interface (there were blinky options in the bios that lets you identify the correct interface) and the cable is not a problem. (If the cable were a problem the client boot message would say "media fault ... please check media...") instead.

Here is my dhcpd.conf file. I went through man dhcpd already, and cleaned out everything that I apparently don't need. (The original file was copied from a more complicated setup that had multiple subnets and dhcp relays.)

Code:
###################simplfied 
linux-kzy1:/var/lib/dhcp/db # cat /etc/dhcpd.conf
authoritative;

ddns-update-style none;
ddns-updates off;

#Information about the host
subnet 192.168.10.0 netmask 255.255.255.0 {
  max-lease-time 600;
  default-lease-time 600;
  range 192.168.10.2 192.168.10.255;
}

group esx_gep{
  filename "pxelinux.0";
  next-server 192.168.10.1; 
  host testserver1 {hardware ethernet a0:d3:c1:f7:f2:64;}
}

this is what /var/log/message and /var/log/rc.dhcpd.log says:
Code:
**************var log message
Mar 19 18:42:17 linux-kzy1 dhcpd: For info, please visit https://www.isc.org/software/dhcp/
Mar 19 18:42:17 linux-kzy1 dhcpd: Not searching LDAP since ldap-server, ldap-port and ldap-base-dn were not specified in the config file
Mar 19 18:42:17 linux-kzy1 dhcpd: Wrote 0 group decls to leases file.
Mar 19 18:42:17 linux-kzy1 dhcpd: Wrote 0 deleted host decls to leases file.
Mar 19 18:42:17 linux-kzy1 dhcpd: Wrote 0 new dynamic host decls to leases file.
Mar 19 18:42:17 linux-kzy1 dhcpd: Wrote 0 leases to leases file.
Mar 19 18:42:17 linux-kzy1 dhcpd: Listening on LPF/eth0/84:8f:69:cf:7c:41/192.168.10.0/24
Mar 19 18:42:17 linux-kzy1 dhcpd: Sending on   LPF/eth0/84:8f:69:cf:7c:41/192.168.10.0/24
Mar 19 18:42:17 linux-kzy1 dhcpd: Sending on   Socket/fallback/fallback-net
Mar 19 18:42:17 linux-kzy1 dhcpd[12233]: Starting ISC DHCPv4 4.x Server [chroot]..done
linux-kzy1:/home/test/Documents #


*****************var log rc.dhcpd.log
Mar 19 18:42:17 linux-kzy1 dhcpd: Internet Systems Consortium DHCP Server 4.2.2
Mar 19 18:42:17 linux-kzy1 dhcpd: Copyright 2004-2011 Internet Systems Consortium.
Mar 19 18:42:17 linux-kzy1 dhcpd: All rights reserved.
Mar 19 18:42:17 linux-kzy1 dhcpd: For info, please visit https://www.isc.org/software/dhcp/
Mar 19 18:42:17 linux-kzy1 dhcpd: Not searching LDAP since ldap-server, ldap-port and ldap-base-dn were not specified in the config file
Mar 19 18:42:17 linux-kzy1 dhcpd: Wrote 0 group decls to leases file.
Mar 19 18:42:17 linux-kzy1 dhcpd: Wrote 0 deleted host decls to leases file.
Mar 19 18:42:17 linux-kzy1 dhcpd: Wrote 0 new dynamic host decls to leases file.
Mar 19 18:42:17 linux-kzy1 dhcpd: Wrote 0 leases to leases file.
Mar 19 18:42:17 linux-kzy1 dhcpd: Listening on LPF/eth0/84:8f:69:cf:7c:41/192.168.10.0/24
Mar 19 18:42:17 linux-kzy1 dhcpd: Sending on   LPF/eth0/84:8f:69:cf:7c:41/192.168.10.0/24
Mar 19 18:42:17 linux-kzy1 dhcpd: Sending on   Socket/fallback/fallback-net
Mar 19 18:42:17 linux-kzy1 dhcpd[12233]: Starting ISC DHCPv4 4.x Server [chroot]..done
linux-kzy1:/home/test/Documents #

not very interesting stuff or useful, but I found some other messages that is very interesting:

Code:
**********
#no free lease

linux-kzy1:/home/test/Documents # cat /var/log/messages | grep "free lease"
Mar 19 15:53:59 linux-kzy1 dhcpd: DHCPDISCOVER from a0:d3:c1:f7:f2:64 via eth0: network 192.168.10.0/24: no free leases
Mar 19 15:54:03 linux-kzy1 dhcpd: DHCPDISCOVER from a0:d3:c1:f7:f2:64 via eth0: network 192.168.10.0/24: no free leases
Mar 19 15:54:11 linux-kzy1 dhcpd: DHCPDISCOVER from a0:d3:c1:f7:f2:64 via eth0: network 192.168.10.0/24: no free leases
......
Mar 19 17:01:06 linux-kzy1 dhcpd: DHCPDISCOVER from a0:d3:c1:f7:f2:64 via eth0: network 192.168.10.0/24: no free leases
Mar 19 17:01:38 linux-kzy1 dhcpd: DHCPDISCOVER from a0:d3:c1:f7:f2:64 via eth0: network 192.168.10.0/24: no free leases
linux-kzy1:/home/test/Documents #

Which ties into my first question: dhcp no free lease: I googled a bit, I found a post from a guy on ubuntu who has the same error message and the suggested course of action is to change ownership of the lease file to dhcpd and give it 777 for permission. Which I thought is weird, because the lease file is automatically created by the dhcpd itself, so it really shouldn't be a permission issue shouldn't it? (Anyway, tried that didn't do a thing.) right now it is owned by root/root and has this permission: -rw-r--r--.

2nd question: once the client gets a reply from my dhcp server saying no free lease, does it remember this dhcp server as no free lease and does it persist throughout reboots? Because I tried rebooting the client a number of times and I don't see anything on the wireshark at all. You will notice the time stamp on the last "no free lease" message is not as late as the other messages from the var/log/messages or rc.dhcpd.log and I rebooted the client and the dhcp plenty of times since 17:01:38.



Thanks for all your help in advance everyone.

Need Help To Get The Available Hosts Among Many Hosts

I have many hosts as following. But those hosts are dual operating system (Linux and Windows). I always run program background. If someone is using Linux, it is fine. But if someone is using windows or the host is offline. Then, I cannot use ssh. The way I do preiviously is first ssh one by one. And then find the hosts which are offline or windows. And write them down one by one. And then ssh to hosts except them. Let us assume the number of programs is less than the total available hosts. Can someone write a shell script to output the all available hosts to a file like "host_available"?
Here is the host file.
https://www.dropbox.com/s/vbz6w864y3...tlist.txt?dl=0
I am using ssh to connect the computers on campus. If the computer I am trying to connect is offline or using windows, the ssh will take long time, and finally it will failed. I write a shell script to generate the hostlist
Code:
#!/bin/bash
for i in `seq -w 1 28`
do
echo "c15-0330-$i.ad.mtu.edu"
# I would like to add a command here to see whether ssh c15-0330-$i.ad.mtu.edu it succeeds or not. And then output a #file which contains the all available host.
done
for i in `seq -w 1 20`
do
echo "c28-0112-$i.ad.mtu.edu"
done
for i in `seq -w 1 20`
do
echo "c28-112a-$i.ad.mtu.edu"
done

I do not how to set a certain time to see whether connection is successful or not (see the comment in the shell script).

Rsync Script Problem

Hi all,

I am trying to write a script that syncs files from source to destination. I have one centralized server that can ssh to any servers without pw. Now when I run the script, it can ssh to source server perfectly fine, but you need to enter password for destination server. Was wondering how I can clean this up before I start using case statements

Below is a sample I wrote

#!/bin/bash

#This scripts syncs shit

echo "Type in ID: "
read ID

echo "Type in Server : " #source server
read S

echo "Type in Destination Server: "
read DS

if [ $S == 9 ]; then
ssh -t "root@"$S"webserver1" "rsync -av /home/rlui/"$ID "root@"$DS"webserver2:/home/rlui/";
ssh -t "root@"$S"webserver1" "rsync -av /home/rlui/tmp/"$ID "root@sl"$DS"webserver2:/home/rlui/tmp/"
exit 1

where S and DS are cluster numbers

I apologize in advance if I am not clear on anything

Mutt Does Not Show Full Subject

I use following shell script to send email. But the subject of email always shows "This" instead of "This is L_1.R is finished". You may refer http://www.linuxquestions.org/questions/linux-newbie-8/how-to-define-variable-in-%60ssh%60-4175540566/
Code:
host_list=("c15-0330-01.ad.mtu.edu" "c15-0330-02.ad.mtu.edu" "c15-0330-03.ad.mtu.edu" "c15-0330-04.ad.mtu.edu")
program=("L_1" "L_2" "L_3" "L_4")
subject="The job is finished"
ssh -f c15-0330-01.ad.mtu.edu 'echo' "the job ${program[0]} is finished" '|' 'mutt "zwang10@mtu.edu" -s' "This is "${program[0]}".R is finished";