自动执行SSH和运行命令

时间:2015-12-10 18:24:46

标签: bash shell ssh sudo

我正在尝试将<script> function initialize() { var openedInfoWindow = []; var locations = [ ['Oddział', 52.846190, 17.723237, 3], ['Oddział', 52.812224, 17.201023, 2], ['Zakład Poligraficzny - Siedziba', 52.847942, 17.757889, 1] ]; var cityCircle; var map = new google.maps.Map(document.getElementById('map-canvas'), { mapTypeId: google.maps.MapTypeId.ROADMAP, zoomControl: true, zoomControlOptions: { style: google.maps.ZoomControlStyle.SMALL } }); var bounds = new google.maps.LatLngBounds(); for (i = 0; i < locations.length; i++) { var marker = new google.maps.Marker({ position: new google.maps.LatLng(locations[i][1], locations[i][2], locations[i][3]), map: map, content: locations[i][0] }); bounds.extend(marker.position); var infowindow = new google.maps.InfoWindow(); google.maps.event.addListener(marker, 'click', (function(marker, i, infowindow) { return function () { if(openedInfoWindow[i] != null){ openedInfoWindow[i].close(); openedInfoWindow[i] = null; }else{ infowindow.setContent(this.content); infowindow.open(map, this); openedInfoWindow[i] = infowindow; google.maps.event.addListener(infowindow, 'closeclick', function() { openedInfoWindow[i] = null; }); } } })(marker, i, infowindow)); google.maps.event.trigger(marker, 'click'); } map.fitBounds(bounds); var listener = google.maps.event.addListener(map, "idle", function () { map.setZoom(9); google.maps.event.removeListener(listener); }); } function loadScript() { var script = document.createElement('script'); script.src = 'https://maps.googleapis.com/maps/api/js?key=AIzaSyADTnbl7e9y2o13cXkUFO8RZpXFJI-yzp4&' + 'callback=initialize'; document.body.appendChild(script); } window.onload = loadScript; </script>` 自动化到一台机器中,然后运行一些命令。但是,我陷入了SSH部分:

SSH

一旦我运行此脚本,我就可以通过SSH进入,但自动停止在此消息:

for h in ${hosts[*]}; do
        ssh -i foo.pem bc2-user@$h
        echo "here"
        sudo bash
        cd /data/kafka/tmp/kafka-logs/
        exit 
        exit
done

当我按******************************************************************************** This is a private computer system containing information that is proprietary and confidential to the owner of the system. Only individuals or entities authorized by the owner of the system are allowed to access or use the system. Any unauthorized access or use of the system or information is strictly prohibited. All violators will be prosecuted to the fullest extent permitted by law. ******************************************************************************** Last login: Thu Dec 10 10:19:23 2015 from 10.81.120.55 -bash: ulimit: open files: cannot modify limit: Operation not permitted 时,我的control + d命令会执行,然后我的脚本会退出。不执行其余命令。

我阅读并尝试了这个但是我收到了这种语法错误:

echo "here"

脚本:

./kafka_prefill_count.sh: line 38: warning: here-document at line 26 delimited by end-of-file (wanted `EOF')
./kafka_prefill_count.sh: line 39: syntax error: unexpected end of file

2 个答案:

答案 0 :(得分:3)

您当前的脚本实际上并不是如何使用ssh远程执行命令。

看起来应该更像这个

EOF

(hostname命令只是证明其在其他机器上运行的示例。

Good resource

刚看到你的编辑:

ssh -i foo.pem bc2-user@$h << EOF echo "here" sudo bash cd /data/kafka/tmp/kafka-logs/ ls | grep "$dir_name" exit exit bash -l EOF 需要一直到左边。

document.getElementById("trackdetails").innerHTML = albumarray;

答案 1 :(得分:3)

尝试使用两个here documents,一个用于ssh,一个用于bash:

ssh -i foo.pem bc2-user@$h << EOF1
  echo "remote"
  sudo bash << EOF2
    cd /data/kafka/tmp/kafka-logs/
    ls | grep "$dir_name"
EOF2
EOF1