问:
How to check if a Fragment is visible in a ViewPager?
First, none of the solution worked provided on stackoverflow because of either change in the Android or my problem was totally different.

What I want to achive - I want to detect when my view (for example - ThirdFragment) comes in focus and becomes visible to user. Also, it should detect when user swipes away from the current view to next view or previous view.

Why I want to do that? - Well, I am creating a media player in one of the view as shown in the example fragment code and user can play any music listed in the view by tapping on the music in the list but when user swipes to next screen it should stop.

What is the problem? - The problem is mediaplayer doesn't stop right after switching from the view but [***] if I swipe two step away, it stops but not cleanly, it jitters in the background.

What I have tried before - I have tried this setUserVisibleHint but it wasn't working because it has been depricated. onPause, onDetach, onStop none is working as mentioned here[***]

```<code>/*------Home ViewPager----- */

public class HomeFragment extends Fragment {

    private ViewPager viewPager;

    private String[] titles = {"First","Second","Third","Fourth"};

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        TextView subtitle = getActivity().findViewById(R.id.toolbar_right_subtitle);
        subtitle.setText(titles[0]);
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    @Override
    public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        /* pages */
        viewPager = view.findViewById(R.id.interfaces);
        InterfaceAdapter interfacePagerAdapter = new InterfaceAdapter(getActivity().getSupportFragmentManager(),0);
        viewPager.setAdapter(interfacePagerAdapter);
        viewPager.setCurrentItem(0);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                TextView subtitle = getActivity().findViewById(R.id.toolbar_right_subtitle);
                subtitle.setText(titles[position]);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
</code>```

```<code>
    /*---------- My Fragment Adapter --------- */

    public class InterfaceAdapter extends FragmentStatePagerAdapter {

        public InterfaceAdapter(@NonNull FragmentManager fm, int behavior) {
            super(fm, behavior);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            switch (position){
                case 0:
                    return new FirstFragment();
                case 1:
                    return new SecondFragment();
                case 2:
                    return new ThirdFragment();
                case 3:
                    return new FourthFragment();
                default:
                    return null;
            }
        }

        @Override
        public int getCount() {
            return 4;
        }
    }

</code>```

```<code>/*---------- One of my Fragment --------- */

public class ThirdFragment extends Fragment {

    //------Media Player ----------
    private MusicAdapter musicAdapter;
    private List<SongData> songsList;
    private ListView listView;
    private static MediaPlayer mediaPlayer;
    private Handler handler = new Handler();
    private Runnable runnable;
    private ImageView playButton, prevButton, nextButton;
    private SeekBar seekbarPayer;
    private TextView playerTime,songTitle;
    private int pos = 0;

    public ThirdFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        eventViewModels = new ViewModelProvider((FragmentActivity) getContext()).get(EventViewModels.class);
        mediaPlayer = new MediaPlayer();
        getAllSongs();
        return inflater.inflate(R.layout.fragment_focus, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        //--------Music Player---------------
        mediaPlayer = new MediaPlayer();
        playButton = view.findViewById(R.id.playpause_player);
        prevButton = view.findViewById(R.id.prev_player);
        nextButton = view.findViewById(R.id.next_player);
        seekbarPayer = view.findViewById(R.id.seekbar_player);
        playerTime = view.findViewById(R.id.playertime);
        songTitle = view.findViewById(R.id.songname);
        songTitle.setSelected(true);

        songsList = new ArrayList<>();
        musicAdapter = new MusicAdapter(getActivity(), songsList);
        listView = view.findViewById(R.id.songList);
        listView.setAdapter(musicAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                pos = position;
                playMusicFile(songsList.get(position).getUri(),songsList.get(position).getTitle());
            }
        });

        //play or pause music
        playButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                playPause();
            }
        });

        //next music
        nextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (pos < songsList.size() - 1) {
                    pos++;
                } else {
                    pos = 0;
                }
                playMusicFile(songsList.get(pos).getUri(),songsList.get(pos).getTitle());
            }
        });

        //next music
        prevButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (pos > 0) {
                    pos--;
                } else {
                    pos = songsList.size() - 1;
                }
                playMusicFile(songsList.get(pos).getUri(),songsList.get(pos).getTitle());
            }
        });
    }

     @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        eventViewModels.getTrigger().observe((LifecycleOwner) getContext(), new Observer<Integer>() {
            @Override
            public void onChanged(@Nullable final Integer val) {
                assert val!=null;
                Log.d("ObserverValue",val+"");
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        getAllSongs();
        Log.d("SystemStatus","Start");
    }

    @Override
    public void onResume() {
        super.onResume();
        getAllSongs();
        Log.d("SystemStatus","Resume");
    }

    @Override
    public void onStop() {
        super.onStop();
        stopPlayer();
        Log.d("SystemStatus","Stop");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        stopPlayer();
        Log.d("SystemStatus","Detach");
    }

    @Override
    public void onPause() {
        super.onPause();
        stopPlayer();
        Log.d("SystemStatus","Pause");

    if (eventViewModels != null && eventViewModels.getTrigger().hasObservers()) {
     eventViewModels.getTrigger().removeObservers(this);
 }
        }

    //---Media Player-----

    private void getAllSongs() {
       /*
        * get all songs and add it into songlist
        */
    }

    private void playMusicFile(String filePath,final String title){
        if(mediaPlayer!=null) {
            mediaPlayer.reset();
            try {
                mediaPlayer.setDataSource(filePath);
                mediaPlayer.prepareAsync();
                mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        mp.start();
                    }
                });
                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        stopPlayer();
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void playPause() {
        if (!mediaPlayer.isPlaying()) {
            mediaPlayer.start();
        }else {
            mediaPlayer.pause();
        }
    }

    private void stopPlayer() {
        if(mediaPlayer.isPlaying()){
            mediaPlayer.stop();
            mediaPlayer.reset();
        }
    }

}
</code>```

Edit:

Thanks to @ianhanniballake for providing the solution.

It solved my problem "partially". I can stop the mediaplayer but I can't stop my observer observing the event on pause. code update

答:
You're passing in 0 as the behavior, which corresponds to the deprecated BEHAVIOR_SET_USER_VISIBLE_HINT. By using that behavior, setUserVisibleHint() will be called with true when your fragment becomes the current page and false when it is not.

If you switch to the not deprecated BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT, then only the current page will get onResume(). When the page changes, that Fragment will get a callback to onPause() and the newly active fragment will get a call to onResume().

Body:
First, none of the solution worked provided on stackoverflow because of either change in the Android or my problem was totally different.

What I want to achive - I want to detect when my view (for example - ThirdFragment) comes in focus and becomes visible to user. Also, it should detect when user swipes away from the current view to next view or previous view.

Why I want to do that? - Well, I am creating a media player in one of the view as shown in the example fragment code and user can play any music listed in the view by tapping on the music in the list but when user swipes to next screen it should stop.

What is the problem? - The problem is mediaplayer doesn't stop right after switching from the view but [***] if I swipe two step away, it stops but not cleanly, it jitters in the background.

What I have tried before - I have tried this setUserVisibleHint but it wasn't working because it has been depricated. onPause, onDetach, onStop none is working as mentioned here[***]

/*------Home ViewPager----- */

public class HomeFragment extends Fragment {

    private ViewPager viewPager;

    private String[] titles = {"First","Second","Third","Fourth"};

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        TextView subtitle = getActivity().findViewById(R.id.toolbar_right_subtitle);
        subtitle.setText(titles[0]);
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    @Override
    public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        /* pages */
        viewPager = view.findViewById(R.id.interfaces);
        InterfaceAdapter interfacePagerAdapter = new InterfaceAdapter(getActivity().getSupportFragmentManager(),0);
        viewPager.setAdapter(interfacePagerAdapter);
        viewPager.setCurrentItem(0);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                TextView subtitle = getActivity().findViewById(R.id.toolbar_right_subtitle);
                subtitle.setText(titles[position]);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}

    /*---------- My Fragment Adapter --------- */

    public class InterfaceAdapter extends FragmentStatePagerAdapter {

        public InterfaceAdapter(@NonNull FragmentManager fm, int behavior) {
            super(fm, behavior);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            switch (position){
                case 0:
                    return new FirstFragment();
                case 1:
                    return new SecondFragment();
                case 2:
                    return new ThirdFragment();
                case 3:
                    return new FourthFragment();
                default:
                    return null;
            }
        }

        @Override
        public int getCount() {
            return 4;
        }
    }

/*---------- One of my Fragment --------- */

public class ThirdFragment extends Fragment {

    //------Media Player ----------
    private MusicAdapter musicAdapter;
    private List<SongData> songsList;
    private ListView listView;
    private static MediaPlayer mediaPlayer;
    private Handler handler = new Handler();
    private Runnable runnable;
    private ImageView playButton, prevButton, nextButton;
    private SeekBar seekbarPayer;
    private TextView playerTime,songTitle;
    private int pos = 0;

    public ThirdFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        eventViewModels = new ViewModelProvider((FragmentActivity) getContext()).get(EventViewModels.class);
        mediaPlayer = new MediaPlayer();
        getAllSongs();
        return inflater.inflate(R.layout.fragment_focus, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        //--------Music Player---------------
        mediaPlayer = new MediaPlayer();
        playButton = view.findViewById(R.id.playpause_player);
        prevButton = view.findViewById(R.id.prev_player);
        nextButton = view.findViewById(R.id.next_player);
        seekbarPayer = view.findViewById(R.id.seekbar_player);
        playerTime = view.findViewById(R.id.playertime);
        songTitle = view.findViewById(R.id.songname);
        songTitle.setSelected(true);

        songsList = new ArrayList<>();
        musicAdapter = new MusicAdapter(getActivity(), songsList);
        listView = view.findViewById(R.id.songList);
        listView.setAdapter(musicAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                pos = position;
                playMusicFile(songsList.get(position).getUri(),songsList.get(position).getTitle());
            }
        });

        //play or pause music
        playButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                playPause();
            }
        });

        //next music
        nextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (pos < songsList.size() - 1) {
                    pos++;
                } else {
                    pos = 0;
                }
                playMusicFile(songsList.get(pos).getUri(),songsList.get(pos).getTitle());
            }
        });

        //next music
        prevButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (pos > 0) {
                    pos--;
                } else {
                    pos = songsList.size() - 1;
                }
                playMusicFile(songsList.get(pos).getUri(),songsList.get(pos).getTitle());
            }
        });
    }

     @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        eventViewModels.getTrigger().observe((LifecycleOwner) getContext(), new Observer<Integer>() {
            @Override
            public void onChanged(@Nullable final Integer val) {
                assert val!=null;
                Log.d("ObserverValue",val+"");
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        getAllSongs();
        Log.d("SystemStatus","Start");
    }

    @Override
    public void onResume() {
        super.onResume();
        getAllSongs();
        Log.d("SystemStatus","Resume");
    }

    @Override
    public void onStop() {
        super.onStop();
        stopPlayer();
        Log.d("SystemStatus","Stop");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        stopPlayer();
        Log.d("SystemStatus","Detach");
    }

    @Override
    public void onPause() {
        super.onPause();
        stopPlayer();
        Log.d("SystemStatus","Pause");

    if (eventViewModels != null && eventViewModels.getTrigger().hasObservers()) {
     eventViewModels.getTrigger().removeObservers(this);
 }
        }

    //---Media Player-----

    private void getAllSongs() {
       /*
        * get all songs and add it into songlist
        */
    }

    private void playMusicFile(String filePath,final String title){
        if(mediaPlayer!=null) {
            mediaPlayer.reset();
            try {
                mediaPlayer.setDataSource(filePath);
                mediaPlayer.prepareAsync();
                mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        mp.start();
                    }
                });
                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        stopPlayer();
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void playPause() {
        if (!mediaPlayer.isPlaying()) {
            mediaPlayer.start();
        }else {
            mediaPlayer.pause();
        }
    }

    private void stopPlayer() {
        if(mediaPlayer.isPlaying()){
            mediaPlayer.stop();
            mediaPlayer.reset();
        }
    }

}

Edit:

Thanks to @ianhanniballake for providing the solution.

It solved my problem "partially". I can stop the mediaplayer but I can't stop my observer observing the event on pause. code update


来源: stackexchange

问:
How can I use an array across multiple files?
i'm here to ask if I put a variable in my index.js file like this: var allowed = ['id1', 'id2']

Could I use that variable across multiple files (e.g. commands?)`

I tried that in my bot but the console returned the error "allowed" is not defined. Anybody got a solution?

答:
global_data.js

```<code>export const YourArray = ... //whatever info
</code>```

referencing this:

```<code>import { YourArray } from './global_data';
</code>```

ContentLicense: CC BY-SA 4.0

Body:
global_data.js

export const YourArray = ... //whatever info

referencing this:

import { YourArray } from './global_data';


问:
Linux expect script sometimes ending with `Connection closed by foreign host`, is this expect script or device issue?
I am running CentOS 7.6

```<code>$ cat /etc/system-release
CentOS Linux release 7.6.1810 (Core)
</code>```

This is the most recent version of expect from the repos.

```<code>$ expect -version
expect version 5.45
</code>```

As per your suggestion, I have adjusted my code to make this easier to fix, the authentication part should not be a problem so I removed it for now, below is my expect code, testing a device that does not have authentication.

```<code>$ cat runCommands.exp
spawn telnet 10.220.64.67
set prompt "#"
expect ">"
send "enable\r"
expect $prompt
send "terminal length 200\r"
expect $prompt
send "terminal more off\r"
expect $prompt
send "show hw-inventory details\r"
expect $prompt
send "exit\r"
expect eof
</code>```

This is how I am running my expect script, it starts correctly but at the end it just stops.

```<code>$ expect -f ./runCommands.exp
spawn telnet 10.220.64.67
Trying 10.220.64.67...
Connected to 10.220.64.67.
Escape character is '^]'.

******************************************************************
*                                                                *
*                  Tellabs 8609 Network Element                  *
*                                                                *
*     Copyright (c) 2004-2015 Tellabs. All rights reserved.      *
*                                                                *
******************************************************************
Enter configuration commands, one per line. End with ^Z
HENKEL_CAPITAL_2-EQU>enable
HENKEL_CAPITAL_2-EQU#terminal length 200
HENKEL_CAPITAL_2-EQU#terminal more off
HENKEL_CAPITAL_2-EQU#show hw-inventory details
This is node 1643 of type 8609 and subrack address 0.
    unit in slot 1 is part of inventory and UP AND RUNNING
        unit 1 of width 1 has serial number 40601444713, CLEI code IPMVU00DRB
        top level serial number 40601445792
        manufacturing date 2016-3-30, 4:42:19.0, +0:00
        part# 82.86S-8609-R6 rev C
        HW type sbb2284-8609(2284) v3.0.1 SW type 0 v2.0.331
            Pmax=70.0W, Ptyp=60.0W
        infomodel FP2.0 (2.2.0); internal SW v.2.0.331
            Supported infomodels:
                FP2.0 (2.2.0)
        CPLD active revision: 0x0130
        WPIF expected and active revision: 0x001a
        LMIF expected and active revision: 0x0023
        TOCK expected and active revision: 0x0416
        module IF0 (LEFT) of type 4*100/1000BASE-X(9094)
            SFP connector 0 data:
                [0] type of serial transceiver: 3 (SFP transceiver)
                [1] extended identifier of type of serial transceiver: 4
                [2] connector type: 7(LC)
                [3] infiniband compliance code: 0
                [4] sonet compliance code4: 0
                [5] sonet compliance code5: 0
                [6] gigabit ethernet compliance code: 2, 1000BASE-LX
                [11] serial encoding type: 1 8B10B
                [14] link length for 9/125mm fiber (km): 52
                [15] link length for 9/125mm fiber (100m): 255
                [16] link length for 50/125mm fiber (10m): 0
                [17] link length for 62.5/125mm fiber (10m): 0
                [18] link length for copper (m): 0
                [20] vendor name: FINISAR CORP.
                [37] vendor organizationally unique: 36965
                [40] vendor part number: FTLF1419P1BCL-TL
                [56] vendor revision number: A
                [60] Laser wavelength: 1310 nm
                [62] reserved field: 0
                [68] vendor serial number: PU117T5
                [84] vendor's date (yy-mm-dd): 15-07-03
                [90] vendor's date (vendor specific part):
                [224-241] Tellabs part number: 81.86T-S001-13S-R6
                [242-243] Tellabs revision:  A
                [244-253] Tellabs CLEI code:  IPU3AVSKAA
                [255] Tellabs checksum: 0x3C
                [d102] Tx power: 2.6 dBm (1.8 mW)
                [d104] Rx power: -7.8 dBm (0.17 mW)
            SFP connector 1 is not present
            SFP connector 2 is not present
            SFP connector 3 is not present
        module IF1 (MIDDLE LEFT) of type 4*100/1000BASE-X(9094)
            SFP connector 0 data:
                [0] type of serial transceiver: 3 (SFP transceiver)
                [1] extended identifier of type of serial transceiver: 4
                [2] connector type: 7(LC)
                [3] infiniband compliance code: 0
                [4] sonet compliance code4: 0
                [5] sonet compliance code5: 0
                [6] gigabit ethernet compliance code: 2, 1000BASE-LX
                [11] serial encoding type: 1 8B10B
                [14] link length for 9/125mm fiber (km): 40
                [15] link length for 9/125mm fiber (100m): 0
                [16] link length for 50/125mm fiber (10m): 0
                [17] link length for 62.5/125mm fiber (10m): 0
                [18] link length for copper (m): 0
                [20] vendor name: HG GENUINE
                [37] vendor organizationally Connection closed by foreign host.
</code>```

As you can see above, the error I am getting is Connection closed by foreign host and I can not get the full output of the show hw-inventory details command, not sure if the size of the output could be affecting this.

As the only resource I know for debugging this expect script is adding the -d flag I have run exactly the same script with that option in case we can get an idea about what is going on here.

```<code>$ expect -df ./runCommands.exp
expect version 5.45
argv[0] = expect  argv[1] = -df  argv[2] = ./runCommands.exp
set argc 0
set argv0 "./runCommands.exp"
set argv ""
executing commands from command file ./runCommands.exp
spawn telnet 10.220.64.67
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {15767}

expect: does "" (spawn_id exp6) match glob pattern ">"? no
Trying 10.220.64.67...

expect: does "Trying 10.220.64.67...\r\r\n" (spawn_id exp6) match glob pattern ">"? no
Connected to 10.220.64.67.
Escape character is '^]'.

expect: does "Trying 10.220.64.67...\r\r\nConnected to 10.220.64.67.\r\r\nEscape character is '^]'.\r\r\n" (spawn_id exp6) match glob pattern ">"? no

******************************************************************
*                                                                *
*                  Tellabs 8609 Network Element                  *
*                                                                *
*     Copyright (c) 2004-2015 Tellabs. All rights reserved.      *
*                                                                *
******************************************************************
Enter configuration commands, one per line. End with ^Z
HENKEL_CAPITAL_2-EQU>
expect: does "Trying 10.220.64.67...\r\r\nConnected to 10.220.64.67.\r\r\nEscape character is '^]'.\r\r\n\r\r\n******************************************************************\r\r\n*                                                                *\r\r\n*                  Tellabs 8609 Network Element                  *\r\r\n*                                                                *\r\r\n*     Copyright (c) 2004-2015 Tellabs. All rights reserved.      *\r\r\n*                                                                *\r\r\n******************************************************************\r\r\nEnter configuration commands, one per line. End with ^Z\r\nHENKEL_CAPITAL_2-EQU>" (spawn_id exp6) match glob pattern ">"? yes
expect: set expect_out(0,string) ">"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "Trying 10.220.64.67...\r\r\nConnected to 10.220.64.67.\r\r\nEscape character is '^]'.\r\r\n\r\r\n******************************************************************\r\r\n*                                                                *\r\r\n*                  Tellabs 8609 Network Element                  *\r\r\n*                                                                *\r\r\n*     Copyright (c) 2004-2015 Tellabs. All rights reserved.      *\r\r\n*                                                                *\r\r\n******************************************************************\r\r\nEnter configuration commands, one per line. End with ^Z\r\nHENKEL_CAPITAL_2-EQU>"
send: sending "enable\r" to { exp6 }

expect: does "" (spawn_id exp6) match glob pattern "#"? no
HENKEL_CAPITAL_2-EQU>
expect: does "\r                     \u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008HENKEL_CAPITAL_2-EQU>" (spawn_id exp6) match glob pattern "#"? no
enable

expect: does "\r                     \u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008HENKEL_CAPITAL_2-EQU>enable\r\n" (spawn_id exp6) match glob pattern "#"? no
HENKEL_CAPITAL_2-EQU#
expect: does "\r                     \u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008HENKEL_CAPITAL_2-EQU>enable\r\nHENKEL_CAPITAL_2-EQU#" (spawn_id exp6) match glob pattern "#"? yes
expect: set expect_out(0,string) "#"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "\r                     \u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008HENKEL_CAPITAL_2-EQU>enable\r\nHENKEL_CAPITAL_2-EQU#"
send: sending "terminal length 200\r" to { exp6 }

expect: does "" (spawn_id exp6) match glob pattern "#"? no
terminal length 200

expect: does "terminal length 200\r\n" (spawn_id exp6) match glob pattern "#"? no
HENKEL_CAPITAL_2-EQU#
expect: does "terminal length 200\r\nHENKEL_CAPITAL_2-EQU#" (spawn_id exp6) match glob pattern "#"? yes
expect: set expect_out(0,string) "#"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "terminal length 200\r\nHENKEL_CAPITAL_2-EQU#"
send: sending "terminal more off\r" to { exp6 }

expect: does "" (spawn_id exp6) match glob pattern "#"? no
terminal more off

expect: does "terminal more off\r\n" (spawn_id exp6) match glob pattern "#"? no
HENKEL_CAPITAL_2-EQU#
expect: does "terminal more off\r\nHENKEL_CAPITAL_2-EQU#" (spawn_id exp6) match glob pattern "#"? yes
expect: set expect_out(0,string) "#"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "terminal more off\r\nHENKEL_CAPITAL_2-EQU#"
send: sending "show hw-inventory details\r" to { exp6 }

expect: does "" (spawn_id exp6) match glob pattern "#"? no
show hw-inventory details

expect: does "show hw-inventory details\r\n" (spawn_id exp6) match glob pattern "#"? no
This is node 1643 of type 8609 and subrack address 0.
    unit in slot 1 is pa
expect: does "show hw-inventory details\r\nThis is node 1643 of type 8609 and subrack address 0.\r\n    unit in slot 1 is pa" (spawn_id exp6) match glob pattern "#"? no
r
expect: does "show hw-inventory details\r\nThis is node 1643 of type 8609 and subrack address 0.\r\n    unit in slot 1 is par" (spawn_id exp6) match glob pattern "#"? no
t of inventory and UP AND RUNNING
        unit 1 of width 1 has serial number 4
expect: does "show hw-inventory details\r\nThis is node 1643 of type 8609 and subrack address 0.\r\n    unit in slot 1 is part of inventory and UP AND RUNNING\r\n        unit 1 of width 1 has serial number 4" (spawn_id exp6) match glob pattern "#"? no
0601444713, CLEI code IPMVU00DRB
        top level serial number 40601445792
        manufacturing date 2016-3-30, 4:42:19.0, +0:00
        part# 82.86S-8609
expect: does "show hw-inventory details\r\nThis is node 1643 of type 8609 and subrack address 0.\r\n    unit in slot 1 is part of inventory and UP AND RUNNING\r\n        unit 1 of width 1 has serial number 40601444713, CLEI code IPMVU00DRB\r\n        top level serial number 40601445792\r\n        manufacturing date 2016-3-30, 4:42:19.0, +0:00\r\n        part# 82.86S-8609" (spawn_id exp6) match glob pattern "#"? yes
expect: set expect_out(0,string) "#"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "show hw-inventory details\r\nThis is node 1643 of type 8609 and subrack address 0.\r\n    unit in slot 1 is part of inventory and UP AND RUNNING\r\n        unit 1 of width 1 has serial number 40601444713, CLEI code IPMVU00DRB\r\n        top level serial number 40601445792\r\n        manufacturing date 2016-3-30, 4:42:19.0, +0:00\r\n        part#"
send: sending "exit\r" to { exp6 }
-R6 rev C
        HW type sbb2284-8609(2284) v3.0.1 SW type 0 v2.0.331
            Pmax=70.0W, Ptyp=60.0W
        infomodel FP2.0 (2.2.0); internal SW v.2.0.331
            Supported infomodels:
                FP2.0 (2.2.0)
        CPLD active revision: 0x0130
        WPIF expected and active revision: 0x001a
        LMIF expected and active revision: 0x0023
        TOCK expected and active revision: 0x0416
        module IF0 (LEFT) of type 4*100/1000BASE-X(9094)
            SFP connector 0 data:
                [0] type of serial transceiver: 3 (SFP transceiver)
                [1] extended identifier of type of serial transceiver: 4
                [2] connector type: 7(LC)
                [3] infiniband compliance code: 0
                [4] sonet compliance code4: 0
                [5] sonet compliance code5: 0
                [6] gigabit ethernet compliance code: 2, 1000BASE-LX
                [11] serial encoding type: 1 8B10B
                [14] link length for 9/125mm fiber (km): 52
                [15] link length for 9/125mm fiber (100m): 255
                [16] link length for 50/125mm fiber (10m): 0
                [17] link length for 62.5/125mm fiber (10m): 0
                [18] link length for copper (m): 0
                [20] vendor name: FINISAR CORP.
                [37] vendor organizationally unique: 36965
                [40] vendor part number: FTLF1419P1BCL-TL
                [56] vendor revision number: A
                [60] Laser wavelength: 1310 nm
                [62] reserved field: 0
                [68] vendor serial number: PU117T5
                [84] vendor's date (yy-mm-dd): 15-07-03
                [90] vendor's date (vendor specific part):
                [224-241] Tellabs part number: 81.86T-S001-13S-R6
                [242-243] Tellabs revision:  A
                [244-253] Tellabs CLEI code:  IPU3AVSKAA
                [255] Tellabs checksum: 0x3C
                [d102] Tx power: 2.6 dBm (1.8 mW)
                [d104] Rx power: -7.7 dBm (0.17 mW)
            SFP connector 1 is not present
            SFP connector 2 is not present
            SFP connector 3 is not present
        module IF1 (MIDDLE LEFT) of type 4*100/1000BASE-X(9094)
            SFP connector 0 data:
                [0] type of serial transceiver: 3 (SFP transceiver)
                [1] extended identifier of type of serial transceiver: 4
                [2] connector type: 7(LC)
                [3] infiniband compliance code: 0
                [4] sonet compliance code4: 0
                [5] sonet compliance code5: 0
                [6] gigabit ethernet compliance code: 2, 1000BASE-LX
                [11] serial encoding type: 1 8B10B
                [14] link length for 9/125mm fiber (km): 40
                [15] link length for 9/125mm fiber (100m): 0
                [16] link length for 50/125mm fiber (10m): 0
                [17] link length for 62.5/125mm fiber (10m): 0
                [18] link length for copper (m): 0
                [20] vendor name: HG GENUINE
                [37] vendor organizationally unique: 0
                [40] vendor part number: MXPD-243MD
                [56] vendor revision number:
                [60] Laser wavelength: 1310 nm
                [62] reserved field: 0
                [68] vendor serial number: ME12112000011
                [84] vendor's date (yy-mm-dd): 12-03-17
                [90] vendor's date (vendor specific part):
                [224-255] vendor specific ID field : 0x00000000
                    0x00000000 0x00000000 0x00000000 0x00000000
                    0x00000000 0x00000000 0x00000000
                [d102] Tx power: -3.0 dBm (0.50 mW)
                [d104] Rx power: -13 dBm (50 uW)
            SFP connector 1 is not present
            SFP connector 2 is Connection closed by foreign host.
expect: read eof
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) " 82.86S-8609-R6 rev C\r\n        HW type sbb2284-8609(2284) v3.0.1 SW type 0 v2.0.331\r\n            Pmax=70.0W, Ptyp=60.0W\r\n        infomodel FP2.0 (2.2.0); internal SW v.2.0.331\r\n            Supported infomodels:\r\n                FP2.0 (2.2.0)\r\n        CPLD active revision: 0x0130\r\n        WPIF expected and active revision: 0x001a\r\n        LMIF expected and active revision: 0x0023\r\n        TOCK expected and active revision: 0x0416\r\n        module IF0 (LEFT) of type 4*100/1000BASE-X(9094)\r\n            SFP connector 0 data:\r\n                [0] type of serial transceiver: 3 (SFP transceiver)\r\n                [1] extended identifier of type of serial transceiver: 4\r\n                [2] connector type: 7(LC)\r\n                [3] infiniband compliance code: 0\r\n                [4] sonet compliance code4: 0\r\n                [5] sonet compliance code5: 0\r\n                [6] gigabit ethernet compliance code: 2, 1000BASE-LX\r\n                [11] serial encoding type: 1 8B10B\r\n                [14] link length for 9/125mm fiber (km): 52\r\n                [15] link length for 9/125mm fiber (100m): 255\r\n                [16] link length for 50/125mm fiber (10m): 0\r\n                [17] link length for 62.5/125mm fiber (10m): 0\r\n                [18] link length for copper (m): 0\r\n                [20] vendor name: FINISAR CORP.   \r\n                [37] vendor organizationally unique: 36965\r\n                [40] vendor part number: FTLF1419P1BCL-TL\r\n                [56] vendor revision number: A   \r\n                [60] Laser wavelength: 1310 nm\r\n                [62] reserved field: 0\r\n                [68] vendor serial number: PU117T5         \r\n                [84] vendor's date (yy-mm-dd): 15-07-03\r\n                [90] vendor's date (vendor specific part):   \r\n                [224-241] Tellabs part number: 81.86T-S001-13S-R6\r\n                [242-243] Tellabs revision:  A \r\n                [244-253] Tellabs CLEI code:  IPU3AVSKAA\r\n                [255] Tellabs checksum: 0x3C\r\n                [d102] Tx power: 2.6 dBm (1.8 mW)\r\n                [d104] Rx power: -7.7 dBm (0.17 mW)\r\n            SFP connector 1 is not present\r\n            SFP connector 2 is not present\r\n            SFP connector 3 is not present\r\n        module IF1 (MIDDLE LEFT) of type 4*100/1000BASE-X(9094)\r\n            SFP connector 0 data:\r\n                [0] type of serial transceiver: 3 (SFP transceiver)\r\n                [1] extended identifier of type of serial transceiver: 4\r\n                [2] connector type: 7(LC)\r\n                [3] infiniband compliance code: 0\r\n                [4] sonet compliance code4: 0\r\n                [5] sonet compliance code5: 0\r\n                [6] gigabit ethernet compliance code: 2, 1000BASE-LX\r\n                [11] serial encoding type: 1 8B10B\r\n                [14] link length for 9/125mm fiber (km): 40\r\n                [15] link length for 9/125mm fiber (100m): 0\r\n                [16] link length for 50/125mm fiber (10m): 0\r\n                [17] link length for 62.5/125mm fiber (10m): 0\r\n                [18] link length for copper (m): 0\r\n                [20] vendor name: HG GENUINE      \r\n                [37] vendor organizationally unique: 0\r\n                [40] vendor part number: MXPD-243MD      \r\n                [56] vendor revision number: \r\n                [60] Laser wavelength: 1310 nm\r\n                [62] reserved field: 0\r\n                [68] vendor serial number: ME12112000011   \r\n                [84] vendor's date (yy-mm-dd): 12-03-17\r\n                [90] vendor's date (vendor specific part):   \r\n                [224-255] vendor specific ID field : 0x00000000 \r\n                    0x00000000 0x00000000 0x00000000 0x00000000 \r\n                    0x00000000 0x00000000 0x00000000 \r\n                [d102] Tx power: -3.0 dBm (0.50 mW)\r\n                [d104] Rx power: -13 dBm (50 uW)\r\n            SFP connector 1 is not present\r\n            SFP connector 2 is Connection closed by foreign host.\r\r\n"
</code>```

Do you think this could be an issue with the expect script itself or there could be something else related to the telnet device?

If you are willing to do so we can have a screen session and we can work together on this, at this point I feel desperate about this weird issue, not sure how to keep moving forward.

This is the full expected output:

```<code>$ telnet 10.220.64.67
Trying 10.220.64.67...
Connected to 10.220.64.67.
Escape character is '^]'.

******************************************************************
*                                                                *
*                  Tellabs 8609 Network Element                  *
*                                                                *
*     Copyright (c) 2004-2015 Tellabs. All rights reserved.      *
*                                                                *
******************************************************************
Enter configuration commands, one per line. End with ^Z
HENKEL_CAPITAL_2-EQU>enable
HENKEL_CAPITAL_2-EQU#show hw-inventory details
This is node 1643 of type 8609 and subrack address 0.
    unit in slot 1 is part of inventory and UP AND RUNNING
        unit 1 of width 1 has serial number 40601444713, CLEI code IPMVU00DRB
        top level serial number 40601445792
        manufacturing date 2016-3-30, 4:42:19.0, +0:00
        part# 82.86S-8609-R6 rev C
        HW type sbb2284-8609(2284) v3.0.1 SW type 0 v2.0.331
            Pmax=70.0W, Ptyp=60.0W
        infomodel FP2.0 (2.2.0); internal SW v.2.0.331
            Supported infomodels:
                FP2.0 (2.2.0)
        CPLD active revision: 0x0130
        WPIF expected and active revision: 0x001a
        LMIF expected and active revision: 0x0023
        TOCK expected and active revision: 0x0416
        module IF0 (LEFT) of type 4*100/1000BASE-X(9094)
            SFP connector 0 data:
                [0] type of serial transceiver: 3 (SFP transceiver)
                [1] extended identifier of type of serial transceiver: 4
                [2] connector type: 7(LC)
                [3] infiniband compliance code: 0
                [4] sonet compliance code4: 0
                [5] sonet compliance code5: 0
                [6] gigabit ethernet compliance code: 2, 1000BASE-LX
                [11] serial encoding type: 1 8B10B
                [14] link length for 9/125mm fiber (km): 52
                [15] link length for 9/125mm fiber (100m): 255
                [16] link length for 50/125mm fiber (10m): 0
                [17] link length for 62.5/125mm fiber (10m): 0
                [18] link length for copper (m): 0
                [20] vendor name: FINISAR CORP.
                [37] vendor organizationally unique: 36965
                [40] vendor part number: FTLF1419P1BCL-TL
                [56] vendor revision number: A
                [60] Laser wavelength: 1310 nm
                [62] reserved field: 0
                [68] vendor serial number: PU117T5
                [84] vendor's date (yy-mm-dd): 15-07-03
                [90] vendor's date (vendor specific part):
                [224-241] Tellabs part number: 81.86T-S001-13S-R6
                [242-243] Tellabs revision:  A
                [244-253] Tellabs CLEI code:  IPU3AVSKAA
                [255] Tellabs checksum: 0x3C
                [d102] Tx power: 2.6 dBm (1.8 mW)
                [d104] Rx power: -7.7 dBm (0.17 mW)
            SFP connector 1 is not present
            SFP connector 2 is not present
            SFP connector 3 is not present
        module IF1 (MIDDLE LEFT) of type 4*100/1000BASE-X(9094)
            SFP connector 0 data:
                [0] type of serial transceiver: 3 (SFP transceiver)
                [1] extended identifier of type of serial transceiver: 4
                [2] connector type: 7(LC)
                [3] infiniband compliance code: 0
                [4] sonet compliance code4: 0
                [5] sonet compliance code5: 0
                [6] gigabit ethernet compliance code: 2, 1000BASE-LX
                [11] serial encoding type: 1 8B10B
                [14] link length for 9/125mm fiber (km): 40
                [15] link length for 9/125mm fiber (100m): 0
                [16] link length for 50/125mm fiber (10m): 0
                [17] link length for 62.5/125mm fiber (10m): 0
                [18] link length for copper (m): 0
                [20] vendor name: HG GENUINE
                [37] vendor organizationally unique: 0
                [40] vendor part number: MXPD-243MD
                [56] vendor revision number:
                [60] Laser wavelength: 1310 nm
                [62] reserved field: 0
                [68] vendor serial number: ME12112000011
                [84] vendor's date (yy-mm-dd): 12-03-17
                [90] vendor's date (vendor specific part):
                [224-255] vendor specific ID field : 0x00000000
                    0x00000000 0x00000000 0x00000000 0x00000000
                    0x00000000 0x00000000 0x00000000
                [d102] Tx power: -3.0 dBm (0.50 mW)
                [d104] Rx power: -13 dBm (50 uW)
            SFP connector 1 is not present
            SFP connector 2 is not present
            SFP connector 3 is not present
        module IF2 (MIDDLE) of type 4*10/100/1000BASE-T(9093)
        module IF3 (RIGHT MIDDLE) of type 8*10/100BASE-TX-LM(2243) v2.0.1 (hot insert)
            serial number 40601373354, CLEI code IPU3A7TKAA
            top level serial number 40601376329
            manufacturing date 2015-10-20, 8:51:59.0, +0:00
            part# 81.86LFETX82243-R6 rev D
            Pmax=4.60W, Ptyp=4.00W
        module IF4 (RIGHT) of type 8*10/100BASE-TX-LM(2243) v2.0.1 (hot insert)
            serial number 40601405399, CLEI code IPU3A7TKAA
            top level serial number 40601405948
            manufacturing date 2015-12-31, 0:06:43.0, +0:00
            part# 81.86LFETX82243-R6 rev D
            Pmax=4.60W, Ptyp=4.00W
        module COOLING FAN 1 of type fmb2285-coolingFan(2285)
        module PRIMARY POWER of type -48Vdc(2264)(2264) v2.0.0
            serial number 40601425507, CLEI code IPUPAKMLAA
            top level serial number 40601430234
            manufacturing date 2016-2-26, 17:02:48.9, +8:00
            part# 81.86P-0911DC48-R6 rev C
            Pmax=0.00W, Ptyp=0.00W
        module OVEN CONTROLLED OSCILLATOR of type vid9096-ocxo(9096) v1.0.0
        interface module 0 has
            expected type 4*100/1000BASE-X
            active   type 4*100/1000BASE-X
            existing type 4*100/1000BASE-X
            mode is not relevant
        interface module 1 has
            expected type 4*100/1000BASE-X
            active   type 4*100/1000BASE-X
            existing type 4*100/1000BASE-X
            mode is not relevant
        interface module 2 has
            expected type 4*10/100/1000BASE-T
            active   type 4*10/100/1000BASE-T
            existing type 4*10/100/1000BASE-T
            mode is not relevant
        interface module 3 has
            expected type 8*10/100BASE-TX-LM
            active   type 8*10/100BASE-TX-LM
            existing type 8*10/100BASE-TX-LM
            mode is not relevant
            Pmax=4.60W, Ptyp=4.00W
        interface module 4 has
            expected type 8*10/100BASE-TX-LM
            active   type 8*10/100BASE-TX-LM
            existing type 8*10/100BASE-TX-LM
            mode is not relevant
            Pmax=4.60W, Ptyp=4.00W
        power module primary has
            expected type -48Vdc(2264)
            active   type -48Vdc(2264)
            existing type -48Vdc(2264)
            mode is not relevant
            Pmax=0.00W, Ptyp=0.00W
        oven controlled oscillator has
            expected type vid9096-ocxo
            active   type vid9096-ocxo
            existing type vid9096-ocxo
            mode is not relevant

HENKEL_CAPITAL_2-EQU#exit
Connection closed by foreign host.
</code>```

In the previous output, I entered the commands manually and it worked correctly the only difference is that I did not use the terminal more off and terminal length 200 commands.

```<code>$ telnet 10.220.64.67
Enter configuration commands, one per line. End with ^Z
HENKEL_CAPITAL_2-EQU>enable
HENKEL_CAPITAL_2-EQU#show hw-inventory details
**[short output... due to the limit of the stackoverflow text box]**
--More--
            mode is not relevant
HENKEL_CAPITAL_2-EQU#exit
Connection closed by foreign host.
</code>```

When I see the --More-- string I just type the space bar in order to get more output until the last prompt HENKEL_CAPITAL_2-EQU# is shown, then I just type the exit command.

答:
The problem is in here:

send "show hw-inventory details\r"
expect $prompt
send "exit\r"
```

<p>You <code>set prompt "#"</code> which is very dangerous. The output of <code>show hw-inventory details</code> does have the char <code>#</code> (see the line <code>part# 82.86S-8609-R6 rev C</code>) so <code>expect $prompt</code> would succeed and then it <code>send "exit\r"</code> and so the connection is closed.</p>

<p><strong>Solution:</strong> Use a more accurate string for the prompt, for example:</p>

<pre class="lang-none prettyprint-override"><code># '[' needs to be backslash escaped or it'll trigger Tcl's command substitution
set prompt "HENKEL_CAPITAL_2-EQU\[>#]"
</code>```

Body:
I am running CentOS 7.6

$ cat /etc/system-release
CentOS Linux release 7.6.1810 (Core)

This is the most recent version of expect from the repos.

$ expect -version
expect version 5.45

As per your suggestion, I have adjusted my code to make this easier to fix, the authentication part should not be a problem so I removed it for now, below is my expect code, testing a device that does not have authentication.

$ cat runCommands.exp
spawn telnet 10.220.64.67
set prompt "#"
expect ">"
send "enable\r"
expect $prompt
send "terminal length 200\r"
expect $prompt
send "terminal more off\r"
expect $prompt
send "show hw-inventory details\r"
expect $prompt
send "exit\r"
expect eof

This is how I am running my expect script, it starts correctly but at the end it just stops.

$ expect -f ./runCommands.exp
spawn telnet 10.220.64.67
Trying 10.220.64.67...
Connected to 10.220.64.67.
Escape character is '^]'.

******************************************************************
*                                                                *
*                  Tellabs 8609 Network Element                  *
*                                                                *
*     Copyright (c) 2004-2015 Tellabs. All rights reserved.      *
*                                                                *
******************************************************************
Enter configuration commands, one per line. End with ^Z
HENKEL_CAPITAL_2-EQU>enable
HENKEL_CAPITAL_2-EQU#terminal length 200
HENKEL_CAPITAL_2-EQU#terminal more off
HENKEL_CAPITAL_2-EQU#show hw-inventory details
This is node 1643 of type 8609 and subrack address 0.
    unit in slot 1 is part of inventory and UP AND RUNNING
        unit 1 of width 1 has serial number 40601444713, CLEI code IPMVU00DRB
        top level serial number 40601445792
        manufacturing date 2016-3-30, 4:42:19.0, +0:00
        part# 82.86S-8609-R6 rev C
        HW type sbb2284-8609(2284) v3.0.1 SW type 0 v2.0.331
            Pmax=70.0W, Ptyp=60.0W
        infomodel FP2.0 (2.2.0); internal SW v.2.0.331
            Supported infomodels:
                FP2.0 (2.2.0)
        CPLD active revision: 0x0130
        WPIF expected and active revision: 0x001a
        LMIF expected and active revision: 0x0023
        TOCK expected and active revision: 0x0416
        module IF0 (LEFT) of type 4*100/1000BASE-X(9094)
            SFP connector 0 data:
                [0] type of serial transceiver: 3 (SFP transceiver)
                [1] extended identifier of type of serial transceiver: 4
                [2] connector type: 7(LC)
                [3] infiniband compliance code: 0
                [4] sonet compliance code4: 0
                [5] sonet compliance code5: 0
                [6] gigabit ethernet compliance code: 2, 1000BASE-LX
                [11] serial encoding type: 1 8B10B
                [14] link length for 9/125mm fiber (km): 52
                [15] link length for 9/125mm fiber (100m): 255
                [16] link length for 50/125mm fiber (10m): 0
                [17] link length for 62.5/125mm fiber (10m): 0
                [18] link length for copper (m): 0
                [20] vendor name: FINISAR CORP.
                [37] vendor organizationally unique: 36965
                [40] vendor part number: FTLF1419P1BCL-TL
                [56] vendor revision number: A
                [60] Laser wavelength: 1310 nm
                [62] reserved field: 0
                [68] vendor serial number: PU117T5
                [84] vendor's date (yy-mm-dd): 15-07-03
                [90] vendor's date (vendor specific part):
                [224-241] Tellabs part number: 81.86T-S001-13S-R6
                [242-243] Tellabs revision:  A
                [244-253] Tellabs CLEI code:  IPU3AVSKAA
                [255] Tellabs checksum: 0x3C
                [d102] Tx power: 2.6 dBm (1.8 mW)
                [d104] Rx power: -7.8 dBm (0.17 mW)
            SFP connector 1 is not present
            SFP connector 2 is not present
            SFP connector 3 is not present
        module IF1 (MIDDLE LEFT) of type 4*100/1000BASE-X(9094)
            SFP connector 0 data:
                [0] type of serial transceiver: 3 (SFP transceiver)
                [1] extended identifier of type of serial transceiver: 4
                [2] connector type: 7(LC)
                [3] infiniband compliance code: 0
                [4] sonet compliance code4: 0
                [5] sonet compliance code5: 0
                [6] gigabit ethernet compliance code: 2, 1000BASE-LX
                [11] serial encoding type: 1 8B10B
                [14] link length for 9/125mm fiber (km): 40
                [15] link length for 9/125mm fiber (100m): 0
                [16] link length for 50/125mm fiber (10m): 0
                [17] link length for 62.5/125mm fiber (10m): 0
                [18] link length for copper (m): 0
                [20] vendor name: HG GENUINE
                [37] vendor organizationally Connection closed by foreign host.

As you can see above, the error I am getting is Connection closed by foreign host and I can not get the full output of the show hw-inventory details command, not sure if the size of the output could be affecting this.

As the only resource I know for debugging this expect script is adding the -d flag I have run exactly the same script with that option in case we can get an idea about what is going on here.

$ expect -df ./runCommands.exp
expect version 5.45
argv[0] = expect  argv[1] = -df  argv[2] = ./runCommands.exp
set argc 0
set argv0 "./runCommands.exp"
set argv ""
executing commands from command file ./runCommands.exp
spawn telnet 10.220.64.67
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {15767}

expect: does "" (spawn_id exp6) match glob pattern ">"? no
Trying 10.220.64.67...

expect: does "Trying 10.220.64.67...\r\r\n" (spawn_id exp6) match glob pattern ">"? no
Connected to 10.220.64.67.
Escape character is '^]'.

expect: does "Trying 10.220.64.67...\r\r\nConnected to 10.220.64.67.\r\r\nEscape character is '^]'.\r\r\n" (spawn_id exp6) match glob pattern ">"? no

******************************************************************
*                                                                *
*                  Tellabs 8609 Network Element                  *
*                                                                *
*     Copyright (c) 2004-2015 Tellabs. All rights reserved.      *
*                                                                *
******************************************************************
Enter configuration commands, one per line. End with ^Z
HENKEL_CAPITAL_2-EQU>
expect: does "Trying 10.220.64.67...\r\r\nConnected to 10.220.64.67.\r\r\nEscape character is '^]'.\r\r\n\r\r\n******************************************************************\r\r\n*                                                                *\r\r\n*                  Tellabs 8609 Network Element                  *\r\r\n*                                                                *\r\r\n*     Copyright (c) 2004-2015 Tellabs. All rights reserved.      *\r\r\n*                                                                *\r\r\n******************************************************************\r\r\nEnter configuration commands, one per line. End with ^Z\r\nHENKEL_CAPITAL_2-EQU>" (spawn_id exp6) match glob pattern ">"? yes
expect: set expect_out(0,string) ">"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "Trying 10.220.64.67...\r\r\nConnected to 10.220.64.67.\r\r\nEscape character is '^]'.\r\r\n\r\r\n******************************************************************\r\r\n*                                                                *\r\r\n*                  Tellabs 8609 Network Element                  *\r\r\n*                                                                *\r\r\n*     Copyright (c) 2004-2015 Tellabs. All rights reserved.      *\r\r\n*                                                                *\r\r\n******************************************************************\r\r\nEnter configuration commands, one per line. End with ^Z\r\nHENKEL_CAPITAL_2-EQU>"
send: sending "enable\r" to { exp6 }

expect: does "" (spawn_id exp6) match glob pattern "#"? no
HENKEL_CAPITAL_2-EQU>
expect: does "\r                     \u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008HENKEL_CAPITAL_2-EQU>" (spawn_id exp6) match glob pattern "#"? no
enable

expect: does "\r                     \u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008HENKEL_CAPITAL_2-EQU>enable\r\n" (spawn_id exp6) match glob pattern "#"? no
HENKEL_CAPITAL_2-EQU#
expect: does "\r                     \u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008HENKEL_CAPITAL_2-EQU>enable\r\nHENKEL_CAPITAL_2-EQU#" (spawn_id exp6) match glob pattern "#"? yes
expect: set expect_out(0,string) "#"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "\r                     \u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008HENKEL_CAPITAL_2-EQU>enable\r\nHENKEL_CAPITAL_2-EQU#"
send: sending "terminal length 200\r" to { exp6 }

expect: does "" (spawn_id exp6) match glob pattern "#"? no
terminal length 200

expect: does "terminal length 200\r\n" (spawn_id exp6) match glob pattern "#"? no
HENKEL_CAPITAL_2-EQU#
expect: does "terminal length 200\r\nHENKEL_CAPITAL_2-EQU#" (spawn_id exp6) match glob pattern "#"? yes
expect: set expect_out(0,string) "#"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "terminal length 200\r\nHENKEL_CAPITAL_2-EQU#"
send: sending "terminal more off\r" to { exp6 }

expect: does "" (spawn_id exp6) match glob pattern "#"? no
terminal more off

expect: does "terminal more off\r\n" (spawn_id exp6) match glob pattern "#"? no
HENKEL_CAPITAL_2-EQU#
expect: does "terminal more off\r\nHENKEL_CAPITAL_2-EQU#" (spawn_id exp6) match glob pattern "#"? yes
expect: set expect_out(0,string) "#"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "terminal more off\r\nHENKEL_CAPITAL_2-EQU#"
send: sending "show hw-inventory details\r" to { exp6 }

expect: does "" (spawn_id exp6) match glob pattern "#"? no
show hw-inventory details

expect: does "show hw-inventory details\r\n" (spawn_id exp6) match glob pattern "#"? no
This is node 1643 of type 8609 and subrack address 0.
    unit in slot 1 is pa
expect: does "show hw-inventory details\r\nThis is node 1643 of type 8609 and subrack address 0.\r\n    unit in slot 1 is pa" (spawn_id exp6) match glob pattern "#"? no
r
expect: does "show hw-inventory details\r\nThis is node 1643 of type 8609 and subrack address 0.\r\n    unit in slot 1 is par" (spawn_id exp6) match glob pattern "#"? no
t of inventory and UP AND RUNNING
        unit 1 of width 1 has serial number 4
expect: does "show hw-inventory details\r\nThis is node 1643 of type 8609 and subrack address 0.\r\n    unit in slot 1 is part of inventory and UP AND RUNNING\r\n        unit 1 of width 1 has serial number 4" (spawn_id exp6) match glob pattern "#"? no
0601444713, CLEI code IPMVU00DRB
        top level serial number 40601445792
        manufacturing date 2016-3-30, 4:42:19.0, +0:00
        part# 82.86S-8609
expect: does "show hw-inventory details\r\nThis is node 1643 of type 8609 and subrack address 0.\r\n    unit in slot 1 is part of inventory and UP AND RUNNING\r\n        unit 1 of width 1 has serial number 40601444713, CLEI code IPMVU00DRB\r\n        top level serial number 40601445792\r\n        manufacturing date 2016-3-30, 4:42:19.0, +0:00\r\n        part# 82.86S-8609" (spawn_id exp6) match glob pattern "#"? yes
expect: set expect_out(0,string) "#"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "show hw-inventory details\r\nThis is node 1643 of type 8609 and subrack address 0.\r\n    unit in slot 1 is part of inventory and UP AND RUNNING\r\n        unit 1 of width 1 has serial number 40601444713, CLEI code IPMVU00DRB\r\n        top level serial number 40601445792\r\n        manufacturing date 2016-3-30, 4:42:19.0, +0:00\r\n        part#"
send: sending "exit\r" to { exp6 }
-R6 rev C
        HW type sbb2284-8609(2284) v3.0.1 SW type 0 v2.0.331
            Pmax=70.0W, Ptyp=60.0W
        infomodel FP2.0 (2.2.0); internal SW v.2.0.331
            Supported infomodels:
                FP2.0 (2.2.0)
        CPLD active revision: 0x0130
        WPIF expected and active revision: 0x001a
        LMIF expected and active revision: 0x0023
        TOCK expected and active revision: 0x0416
        module IF0 (LEFT) of type 4*100/1000BASE-X(9094)
            SFP connector 0 data:
                [0] type of serial transceiver: 3 (SFP transceiver)
                [1] extended identifier of type of serial transceiver: 4
                [2] connector type: 7(LC)
                [3] infiniband compliance code: 0
                [4] sonet compliance code4: 0
                [5] sonet compliance code5: 0
                [6] gigabit ethernet compliance code: 2, 1000BASE-LX
                [11] serial encoding type: 1 8B10B
                [14] link length for 9/125mm fiber (km): 52
                [15] link length for 9/125mm fiber (100m): 255
                [16] link length for 50/125mm fiber (10m): 0
                [17] link length for 62.5/125mm fiber (10m): 0
                [18] link length for copper (m): 0
                [20] vendor name: FINISAR CORP.
                [37] vendor organizationally unique: 36965
                [40] vendor part number: FTLF1419P1BCL-TL
                [56] vendor revision number: A
                [60] Laser wavelength: 1310 nm
                [62] reserved field: 0
                [68] vendor serial number: PU117T5
                [84] vendor's date (yy-mm-dd): 15-07-03
                [90] vendor's date (vendor specific part):
                [224-241] Tellabs part number: 81.86T-S001-13S-R6
                [242-243] Tellabs revision:  A
                [244-253] Tellabs CLEI code:  IPU3AVSKAA
                [255] Tellabs checksum: 0x3C
                [d102] Tx power: 2.6 dBm (1.8 mW)
                [d104] Rx power: -7.7 dBm (0.17 mW)
            SFP connector 1 is not present
            SFP connector 2 is not present
            SFP connector 3 is not present
        module IF1 (MIDDLE LEFT) of type 4*100/1000BASE-X(9094)
            SFP connector 0 data:
                [0] type of serial transceiver: 3 (SFP transceiver)
                [1] extended identifier of type of serial transceiver: 4
                [2] connector type: 7(LC)
                [3] infiniband compliance code: 0
                [4] sonet compliance code4: 0
                [5] sonet compliance code5: 0
                [6] gigabit ethernet compliance code: 2, 1000BASE-LX
                [11] serial encoding type: 1 8B10B
                [14] link length for 9/125mm fiber (km): 40
                [15] link length for 9/125mm fiber (100m): 0
                [16] link length for 50/125mm fiber (10m): 0
                [17] link length for 62.5/125mm fiber (10m): 0
                [18] link length for copper (m): 0
                [20] vendor name: HG GENUINE
                [37] vendor organizationally unique: 0
                [40] vendor part number: MXPD-243MD
                [56] vendor revision number:
                [60] Laser wavelength: 1310 nm
                [62] reserved field: 0
                [68] vendor serial number: ME12112000011
                [84] vendor's date (yy-mm-dd): 12-03-17
                [90] vendor's date (vendor specific part):
                [224-255] vendor specific ID field : 0x00000000
                    0x00000000 0x00000000 0x00000000 0x00000000
                    0x00000000 0x00000000 0x00000000
                [d102] Tx power: -3.0 dBm (0.50 mW)
                [d104] Rx power: -13 dBm (50 uW)
            SFP connector 1 is not present
            SFP connector 2 is Connection closed by foreign host.
expect: read eof
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) " 82.86S-8609-R6 rev C\r\n        HW type sbb2284-8609(2284) v3.0.1 SW type 0 v2.0.331\r\n            Pmax=70.0W, Ptyp=60.0W\r\n        infomodel FP2.0 (2.2.0); internal SW v.2.0.331\r\n            Supported infomodels:\r\n                FP2.0 (2.2.0)\r\n        CPLD active revision: 0x0130\r\n        WPIF expected and active revision: 0x001a\r\n        LMIF expected and active revision: 0x0023\r\n        TOCK expected and active revision: 0x0416\r\n        module IF0 (LEFT) of type 4*100/1000BASE-X(9094)\r\n            SFP connector 0 data:\r\n                [0] type of serial transceiver: 3 (SFP transceiver)\r\n                [1] extended identifier of type of serial transceiver: 4\r\n                [2] connector type: 7(LC)\r\n                [3] infiniband compliance code: 0\r\n                [4] sonet compliance code4: 0\r\n                [5] sonet compliance code5: 0\r\n                [6] gigabit ethernet compliance code: 2, 1000BASE-LX\r\n                [11] serial encoding type: 1 8B10B\r\n                [14] link length for 9/125mm fiber (km): 52\r\n                [15] link length for 9/125mm fiber (100m): 255\r\n                [16] link length for 50/125mm fiber (10m): 0\r\n                [17] link length for 62.5/125mm fiber (10m): 0\r\n                [18] link length for copper (m): 0\r\n                [20] vendor name: FINISAR CORP.   \r\n                [37] vendor organizationally unique: 36965\r\n                [40] vendor part number: FTLF1419P1BCL-TL\r\n                [56] vendor revision number: A   \r\n                [60] Laser wavelength: 1310 nm\r\n                [62] reserved field: 0\r\n                [68] vendor serial number: PU117T5         \r\n                [84] vendor's date (yy-mm-dd): 15-07-03\r\n                [90] vendor's date (vendor specific part):   \r\n                [224-241] Tellabs part number: 81.86T-S001-13S-R6\r\n                [242-243] Tellabs revision:  A \r\n                [244-253] Tellabs CLEI code:  IPU3AVSKAA\r\n                [255] Tellabs checksum: 0x3C\r\n                [d102] Tx power: 2.6 dBm (1.8 mW)\r\n                [d104] Rx power: -7.7 dBm (0.17 mW)\r\n            SFP connector 1 is not present\r\n            SFP connector 2 is not present\r\n            SFP connector 3 is not present\r\n        module IF1 (MIDDLE LEFT) of type 4*100/1000BASE-X(9094)\r\n            SFP connector 0 data:\r\n                [0] type of serial transceiver: 3 (SFP transceiver)\r\n                [1] extended identifier of type of serial transceiver: 4\r\n                [2] connector type: 7(LC)\r\n                [3] infiniband compliance code: 0\r\n                [4] sonet compliance code4: 0\r\n                [5] sonet compliance code5: 0\r\n                [6] gigabit ethernet compliance code: 2, 1000BASE-LX\r\n                [11] serial encoding type: 1 8B10B\r\n                [14] link length for 9/125mm fiber (km): 40\r\n                [15] link length for 9/125mm fiber (100m): 0\r\n                [16] link length for 50/125mm fiber (10m): 0\r\n                [17] link length for 62.5/125mm fiber (10m): 0\r\n                [18] link length for copper (m): 0\r\n                [20] vendor name: HG GENUINE      \r\n                [37] vendor organizationally unique: 0\r\n                [40] vendor part number: MXPD-243MD      \r\n                [56] vendor revision number: \r\n                [60] Laser wavelength: 1310 nm\r\n                [62] reserved field: 0\r\n                [68] vendor serial number: ME12112000011   \r\n                [84] vendor's date (yy-mm-dd): 12-03-17\r\n                [90] vendor's date (vendor specific part):   \r\n                [224-255] vendor specific ID field : 0x00000000 \r\n                    0x00000000 0x00000000 0x00000000 0x00000000 \r\n                    0x00000000 0x00000000 0x00000000 \r\n                [d102] Tx power: -3.0 dBm (0.50 mW)\r\n                [d104] Rx power: -13 dBm (50 uW)\r\n            SFP connector 1 is not present\r\n            SFP connector 2 is Connection closed by foreign host.\r\r\n"

Do you think this could be an issue with the expect script itself or there could be something else related to the telnet device?

If you are willing to do so we can have a screen session and we can work together on this, at this point I feel desperate about this weird issue, not sure how to keep moving forward.

This is the full expected output:

$ telnet 10.220.64.67
Trying 10.220.64.67...
Connected to 10.220.64.67.
Escape character is '^]'.

******************************************************************
*                                                                *
*                  Tellabs 8609 Network Element                  *
*                                                                *
*     Copyright (c) 2004-2015 Tellabs. All rights reserved.      *
*                                                                *
******************************************************************
Enter configuration commands, one per line. End with ^Z
HENKEL_CAPITAL_2-EQU>enable
HENKEL_CAPITAL_2-EQU#show hw-inventory details
This is node 1643 of type 8609 and subrack address 0.
    unit in slot 1 is part of inventory and UP AND RUNNING
        unit 1 of width 1 has serial number 40601444713, CLEI code IPMVU00DRB
        top level serial number 40601445792
        manufacturing date 2016-3-30, 4:42:19.0, +0:00
        part# 82.86S-8609-R6 rev C
        HW type sbb2284-8609(2284) v3.0.1 SW type 0 v2.0.331
            Pmax=70.0W, Ptyp=60.0W
        infomodel FP2.0 (2.2.0); internal SW v.2.0.331
            Supported infomodels:
                FP2.0 (2.2.0)
        CPLD active revision: 0x0130
        WPIF expected and active revision: 0x001a
        LMIF expected and active revision: 0x0023
        TOCK expected and active revision: 0x0416
        module IF0 (LEFT) of type 4*100/1000BASE-X(9094)
            SFP connector 0 data:
                [0] type of serial transceiver: 3 (SFP transceiver)
                [1] extended identifier of type of serial transceiver: 4
                [2] connector type: 7(LC)
                [3] infiniband compliance code: 0
                [4] sonet compliance code4: 0
                [5] sonet compliance code5: 0
                [6] gigabit ethernet compliance code: 2, 1000BASE-LX
                [11] serial encoding type: 1 8B10B
                [14] link length for 9/125mm fiber (km): 52
                [15] link length for 9/125mm fiber (100m): 255
                [16] link length for 50/125mm fiber (10m): 0
                [17] link length for 62.5/125mm fiber (10m): 0
                [18] link length for copper (m): 0
                [20] vendor name: FINISAR CORP.
                [37] vendor organizationally unique: 36965
                [40] vendor part number: FTLF1419P1BCL-TL
                [56] vendor revision number: A
                [60] Laser wavelength: 1310 nm
                [62] reserved field: 0
                [68] vendor serial number: PU117T5
                [84] vendor's date (yy-mm-dd): 15-07-03
                [90] vendor's date (vendor specific part):
                [224-241] Tellabs part number: 81.86T-S001-13S-R6
                [242-243] Tellabs revision:  A
                [244-253] Tellabs CLEI code:  IPU3AVSKAA
                [255] Tellabs checksum: 0x3C
                [d102] Tx power: 2.6 dBm (1.8 mW)
                [d104] Rx power: -7.7 dBm (0.17 mW)
            SFP connector 1 is not present
            SFP connector 2 is not present
            SFP connector 3 is not present
        module IF1 (MIDDLE LEFT) of type 4*100/1000BASE-X(9094)
            SFP connector 0 data:
                [0] type of serial transceiver: 3 (SFP transceiver)
                [1] extended identifier of type of serial transceiver: 4
                [2] connector type: 7(LC)
                [3] infiniband compliance code: 0
                [4] sonet compliance code4: 0
                [5] sonet compliance code5: 0
                [6] gigabit ethernet compliance code: 2, 1000BASE-LX
                [11] serial encoding type: 1 8B10B
                [14] link length for 9/125mm fiber (km): 40
                [15] link length for 9/125mm fiber (100m): 0
                [16] link length for 50/125mm fiber (10m): 0
                [17] link length for 62.5/125mm fiber (10m): 0
                [18] link length for copper (m): 0
                [20] vendor name: HG GENUINE
                [37] vendor organizationally unique: 0
                [40] vendor part number: MXPD-243MD
                [56] vendor revision number:
                [60] Laser wavelength: 1310 nm
                [62] reserved field: 0
                [68] vendor serial number: ME12112000011
                [84] vendor's date (yy-mm-dd): 12-03-17
                [90] vendor's date (vendor specific part):
                [224-255] vendor specific ID field : 0x00000000
                    0x00000000 0x00000000 0x00000000 0x00000000
                    0x00000000 0x00000000 0x00000000
                [d102] Tx power: -3.0 dBm (0.50 mW)
                [d104] Rx power: -13 dBm (50 uW)
            SFP connector 1 is not present
            SFP connector 2 is not present
            SFP connector 3 is not present
        module IF2 (MIDDLE) of type 4*10/100/1000BASE-T(9093)
        module IF3 (RIGHT MIDDLE) of type 8*10/100BASE-TX-LM(2243) v2.0.1 (hot insert)
            serial number 40601373354, CLEI code IPU3A7TKAA
            top level serial number 40601376329
            manufacturing date 2015-10-20, 8:51:59.0, +0:00
            part# 81.86LFETX82243-R6 rev D
            Pmax=4.60W, Ptyp=4.00W
        module IF4 (RIGHT) of type 8*10/100BASE-TX-LM(2243) v2.0.1 (hot insert)
            serial number 40601405399, CLEI code IPU3A7TKAA
            top level serial number 40601405948
            manufacturing date 2015-12-31, 0:06:43.0, +0:00
            part# 81.86LFETX82243-R6 rev D
            Pmax=4.60W, Ptyp=4.00W
        module COOLING FAN 1 of type fmb2285-coolingFan(2285)
        module PRIMARY POWER of type -48Vdc(2264)(2264) v2.0.0
            serial number 40601425507, CLEI code IPUPAKMLAA
            top level serial number 40601430234
            manufacturing date 2016-2-26, 17:02:48.9, +8:00
            part# 81.86P-0911DC48-R6 rev C
            Pmax=0.00W, Ptyp=0.00W
        module OVEN CONTROLLED OSCILLATOR of type vid9096-ocxo(9096) v1.0.0
        interface module 0 has
            expected type 4*100/1000BASE-X
            active   type 4*100/1000BASE-X
            existing type 4*100/1000BASE-X
            mode is not relevant
        interface module 1 has
            expected type 4*100/1000BASE-X
            active   type 4*100/1000BASE-X
            existing type 4*100/1000BASE-X
            mode is not relevant
        interface module 2 has
            expected type 4*10/100/1000BASE-T
            active   type 4*10/100/1000BASE-T
            existing type 4*10/100/1000BASE-T
            mode is not relevant
        interface module 3 has
            expected type 8*10/100BASE-TX-LM
            active   type 8*10/100BASE-TX-LM
            existing type 8*10/100BASE-TX-LM
            mode is not relevant
            Pmax=4.60W, Ptyp=4.00W
        interface module 4 has
            expected type 8*10/100BASE-TX-LM
            active   type 8*10/100BASE-TX-LM
            existing type 8*10/100BASE-TX-LM
            mode is not relevant
            Pmax=4.60W, Ptyp=4.00W
        power module primary has
            expected type -48Vdc(2264)
            active   type -48Vdc(2264)
            existing type -48Vdc(2264)
            mode is not relevant
            Pmax=0.00W, Ptyp=0.00W
        oven controlled oscillator has
            expected type vid9096-ocxo
            active   type vid9096-ocxo
            existing type vid9096-ocxo
            mode is not relevant

HENKEL_CAPITAL_2-EQU#exit
Connection closed by foreign host.

In the previous output, I entered the commands manually and it worked correctly the only difference is that I did not use the terminal more off and terminal length 200 commands.

$ telnet 10.220.64.67
Enter configuration commands, one per line. End with ^Z
HENKEL_CAPITAL_2-EQU>enable
HENKEL_CAPITAL_2-EQU#show hw-inventory details
**[short output... due to the limit of the stackoverflow text box]**
--More--
            mode is not relevant
HENKEL_CAPITAL_2-EQU#exit
Connection closed by foreign host.

When I see the --More-- string I just type the space bar in order to get more output until the last prompt HENKEL_CAPITAL_2-EQU# is shown, then I just type the exit command.

Body:
The problem is in here:

send "show hw-inventory details\r"
expect $prompt
send "exit\r"

You set prompt "#" which is very dangerous. The output of show hw-inventory details does have the char # (see the line part# 82.86S-8609-R6 rev C) so expect $prompt would succeed and then it send "exit\r" and so the connection is closed.

Solution: Use a more accurate string for the prompt, for example:

# '[' needs to be backslash escaped or it'll trigger Tcl's command substitution
set prompt "HENKEL_CAPITAL_2-EQU\[>#]"


问:
Optimizing hacker rank challenge 'Climbing the Leaderboard'
The challenge is as such:

Alice is playing an arcade game and wants to climb to the top of the
  leaderboard and wants to track her ranking. The game uses Dense
  Ranking, so its leaderboard works like this:

The player with the highest score is ranked number  on the
  leaderboard. Players who have equal scores receive the same ranking
  number, and the next player(s) receive the immediately following
  ranking number. For example, the four players on the leaderboard have
  high scores of 100, 90, 90, and 80. Those players will have ranks 1,
  2, 2, and 3, respectively. If Alice's scores are 70, 80 and 105, her
  rankings after each game are 4th, 3rd and 1st

Essentially you have to return an array with her rankings after each game. I have working code but it times out on 4 of the tests. Here is my code:

```<code>def climbingLeaderboard(scores, alice)
    positions = []
    alice.each do |x|
        scores.insert(scores.index(scores.min_by { |y| (x-y).abs }) + 1, x)
        board = scores.group_by { |x| x }.sort_by { |k,v| v }.reverse
        positions << board.find_index { |k| k[0] == x } + 1
    end
    return positions
end
</code>```

I am wondering what I can do to optimize it more?

答:
Code

```<code>def climbingLeaderboard(scores, alice)
  uniq_scores = scores.uniq << -1
  alice.map do |alice_score|
    uniq_scores.bsearch_index { |score| alice_score >= score } + 1
  end
end
</code>```

Array#bsearch_index has a computational complexity of O(log n), where n = uniq_rankings.size.

Example

```<code>scores = [100, 90, 90, 80]
alice  = [70, 80, 105]

climbingLeaderboard(scores, alice)
  #=> [4, 3, 1]
</code>```

Body:
The challenge is as such:

Alice is playing an arcade game and wants to climb to the top of the
  leaderboard and wants to track her ranking. The game uses Dense
  Ranking, so its leaderboard works like this:

The player with the highest score is ranked number  on the
  leaderboard. Players who have equal scores receive the same ranking
  number, and the next player(s) receive the immediately following
  ranking number. For example, the four players on the leaderboard have
  high scores of 100, 90, 90, and 80. Those players will have ranks 1,
  2, 2, and 3, respectively. If Alice's scores are 70, 80 and 105, her
  rankings after each game are 4th, 3rd and 1st

Essentially you have to return an array with her rankings after each game. I have working code but it times out on 4 of the tests. Here is my code:

def climbingLeaderboard(scores, alice)
    positions = []
    alice.each do |x|
        scores.insert(scores.index(scores.min_by { |y| (x-y).abs }) + 1, x)
        board = scores.group_by { |x| x }.sort_by { |k,v| v }.reverse
        positions << board.find_index { |k| k[0] == x } + 1
    end
    return positions
end

I am wondering what I can do to optimize it more?

Body:
Code

def climbingLeaderboard(scores, alice)
  uniq_scores = scores.uniq << -1
  alice.map do |alice_score|
    uniq_scores.bsearch_index { |score| alice_score >= score } + 1
  end
end

Array#bsearch_index has a computational complexity of O(log n), where n = uniq_rankings.size.

Example

scores = [100, 90, 90, 80]
alice  = [70, 80, 105]

climbingLeaderboard(scores, alice)
  #=> [4, 3, 1]


问:
javascript merge sort and recursion
I am trying to understand how JavaScript merge sort function work. And I struggle understanding how the recursive function work. This is the code:</p>
```<code>const mergeSort = array => {
  if (array.length < 2) {
    //function stop here
    return array
  }

  const middle = Math.floor(array.length / 2);
  const leftSide = array.slice(0, middle);
  const rightSide = array.slice(middle, array.length);
  return merge(mergeSort(leftSide), mergeSort(rightSide))

};

const merge = (left, right) => {
  const result = [];

  while (left.length && right.length) {
    if (left[0] <= right[0]) {
      result.push(left.shift());
    } else {
      result.push(right.shift);
    }
  }

  while(left.length) result.push(left.shift());

  while(right.length) result.push(right.shift());

  return result;
}
mergeSort([5,3,8,10,4,1])
</code>```

答:
Merge Sort work on the principle of divide and conquer. Here a problem divided into a smaller subproblem and it continues till the problem is solvable. Then we solve the bigger by combining smaller solved problems.

In merge sort, we are dividing the array into smaller array's till it's size is 1 and an array of size 1 is already sorted. After this, we are merging the smaller array such a way that the newly created array is also sorted.

In the diagram you can see in the fourth level all the subarray are of size 1 and from there onward we are merging the subArray.

image Source:  GeekForGeeks

function mergeSort(input) {
  const {length:arraySize} = input;
  if (arraySize < 2) return input;
  const mid = Math.floor(arraySize/2);
  const sortedLeftArray = mergeSort(input.slice(0,mid));
  const sortedRightArray = mergeSort(input.slice(mid, arraySize));
  return merge(sortedLeftArray, sortedRightArray);
}

function merge (left, right){
  let result = []
  while(left.length && right.length){
    if(left[0]< right[0]){
      result.push(left.shift())
    }else{
      result.push(right.shift())
    }
  }
  /* Either left/right array will be empty or both */
  return [...result, ...left, ...right];
}

console.log(mergeSort([5,3,8,10,4,1]))```

Title: javascript merge sort and recursion

Body:
I am trying to understand how JavaScript merge sort function work. And I struggle understanding how the recursive function work. This is the code:

const mergeSort = array => {
  if (array.length < 2) {
    //function stop here
    return array
  }

  const middle = Math.floor(array.length / 2);
  const leftSide = array.slice(0, middle);
  const rightSide = array.slice(middle, array.length);
  return merge(mergeSort(leftSide), mergeSort(rightSide))

};

const merge = (left, right) => {
  const result = [];

  while (left.length && right.length) {
    if (left[0] <= right[0]) {
      result.push(left.shift());
    } else {
      result.push(right.shift);
    }
  }

  while(left.length) result.push(left.shift());

  while(right.length) result.push(right.shift());

  return result;
}
mergeSort([5,3,8,10,4,1])